Duration data structure : TimeSpan « Date Time « C# / C Sharp






Duration data structure

        
using System;

namespace ActionPack.Common
{
    public class Duration 
    {
        TimeSpan t = new TimeSpan();
        public  TimeSpan TimeSpan { get { return t; } }

        public int Days { get { return t.Days; } }
        public int Hours { get { return t.Hours; } }
        public int Milliseconds { get { return t.Milliseconds; } }
        public int Minutes { get { return t.Minutes; } }
        public int Seconds { get { return t.Seconds; } }
        public long Ticks { get { return t.Ticks; } }
        public double TotalDays { get { return t.TotalDays; } }
        public double TotalHours { get { return t.TotalHours; } }
        public double TotalMilliseconds { get { return t.TotalMilliseconds; } }
        public double TotalMinutes { get { return t.TotalMinutes; } }
        public double TotalSeconds { get { return t.TotalSeconds; } }

        public Duration(int days, int hours, int minutes, int seconds, int milliseconds)
        {
            t = new TimeSpan(days, hours, minutes, seconds, milliseconds);
        }
        public Duration(int days, int hours, int minutes, int seconds)
        {
            t = new TimeSpan(days, hours, minutes, seconds);
        }
        public Duration(int hours, int minutes, int seconds)
        {
            t = new TimeSpan(hours, minutes, seconds);
        }
        public Duration(long ticks)
        {
            t = new TimeSpan(ticks);
        }
        private Duration(TimeSpan ts)
        {
            t = ts;
        }
        public Duration Add(Duration d)
        {
            return new Duration(TimeSpan.Add(d.TimeSpan));
        }
        public int CompareTo(object o)
        {
            return t.CompareTo(o);
        }
        public int CompareTo(Duration d)
        {
            return t.CompareTo(d.TimeSpan);
        }
        public override int  GetHashCode()
        {
            return TimeSpan.GetHashCode();
        }
        public override bool Equals(object o)
        {
            return t.Equals(o);
        }
        public bool Equals(Duration d)
        {
            return t.Equals(d.TimeSpan);
        }
        public Duration Negate()
        {
            return new Duration(TimeSpan.Negate());
        }
        public Duration Subtract(Duration d)
        {
            return new Duration(TimeSpan.Subtract(d.TimeSpan));
        }
        public override string ToString()
        {
            return TimeSpan.ToString();
        }
        public static bool operator >(Duration l, Duration r)
        {
            return l.TimeSpan > r.TimeSpan;
        }
        public static bool operator <(Duration l, Duration r)
        {
            return l.TimeSpan < r.TimeSpan;
        }
        public static bool operator <=(Duration l, Duration r)
        {
            return l.TimeSpan <= r.TimeSpan;
        }
        public static bool operator >=(Duration l, Duration r)
        {
            return l.TimeSpan >= r.TimeSpan;
        }
        public static bool operator ==(Duration l, Duration r)
        {
            return l.Equals(r);
        }
        public static bool operator !=(Duration l, Duration r)
        {
            return !(l == r);
        }
        public static Duration operator +(Duration l, Duration r)
        {
            return new Duration(l.TimeSpan + r.TimeSpan);
        }
        public static Duration operator -(Duration l, Duration r)
        {
            return new Duration(l.TimeSpan + r.TimeSpan);
        }
        public static Duration operator *(Duration l, int r)
        {
            return new Duration(l.TimeSpan.Ticks * r);
        }
        public static Duration operator *(int l, Duration r)
        {
            return r*l;
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.new TimeSpan(2, 12, 0, 0)
2.TimeSpan.TicksPerDay
3.Initialize a time span to zero
4.Initialize a time span to 14 days
5.Initialize a time span to 1:02:03
6.Initialize a time span to 250 milliseconds
7.Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.9999999 seconds
8.Calculation based on the TimeSpanCalculation based on the TimeSpan
9.Subtract 15 minutes from the current TimeSpan and print the result
10.Measuring the Time Taken to Add Some Numbers
11.Use FromDays(), FromHours(), FromMinutes(), FromSeconds(), FromMilliseconds(), and FromTicks() methods to create new TimeSpan instances
12.Use the Parse() method to convert strings to TimeSpan instances
13.Use the Add() method to add a TimeSpan instance to another
14.Use the Subtract() method to subtract a TimeSpan instance from another
15.Use the Duration() method to add two TimeSpan instances
16.Use the Negate() method to add two TimeSpan instances
17.Create a TimeSpan instance, specifying the hours, minutes, and seconds
18.Create a TimeSpan instance, specifying the days, hours, minutes, and seconds
19.Create a TimeSpan instance, specifying the days, hours, minutes, seconds, and milliseconds
20.Create a TimeSpan instance, specifying the number of ticks
21.Display the properties for myTimeSpan
22.Initalize a timespan to 25 milliseconds
23.Custom TimeSpan Format Strings
24.Create TimeSpan value from seconds
25.TimeSpan calculations
26.Create a new TimeSpan to a specified number of hours, minutes, and seconds.
27.Create a new TimeSpan to a specified number of days, hours, minutes, and seconds.
28.Create a TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.
29.Create a new TimeSpan to the specified number of ticks.
30.Zero TimeSpan
31.Default output of ToString() method
32.Subtract one DateTime from another you get a TimeSpan
33.Elapsed Time Today with TimeSpan
34.Get properties of TimeSpan
35.Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
36.Example of the TimeSpan relational operators.
37.Get a TimeSpan that represents a specified number of days
38.Create a TimeSpan that represents a specified number of hours
39.Returns a TimeSpan that represents a specified number of milliseconds.
40.Returns a TimeSpan that represents a specified number of minutes
41.Create a TimeSpan that represents a specified number of seconds
42.Create a TimeSpan from units of ticks.
43.Predefined TimeSpan value:MaxValue, MinValue, Zero, TicksPerDay, TicksPersHour
44.Converts string to TimeSpan
45.Converts string to TimeSpan by using the specified format and culture-specific format information.
46.Convert TimeSpan in whole and fractional days
47.Gets the value of TimeSpan in whole and fractional hours.
48.Gets the value of TimeSpan in whole and fractional milliseconds.
49.Gets the value of TimeSpan in whole and fractional minutes.
50.Gets the value of TimeSpan in whole and fractional seconds.