首页 > 学院 > 开发设计 > 正文

C# Time Class using MySQL

2019-11-17 02:30:50
字体:
来源:转载
供稿:网友

C# Time Class using MySQL

http://www.csharphelp.com/2007/09/c-time-class/

  1 /*  2  datatypes.  3   4 Time class is writen in C# and .NET 2.0.  5   6 Time class explantation.  7   8 This is simple class and is not much to explain.  9  10 1.Public fields:  11 .public int Hour,  12 .public int Minute and  13 .public int Second.  14 .public const char TIME_SEPERATOR = ':' 15  16 2.Constructors: 17 .current system time (public Time()),  18 .from string value (public Time(string value)),  19 .from parameters (public Time(int hour, int minute, int second)) and  20 .from seconds (public Time(int seconds)). 21  22 3. 23 Public method Add: 24  25 Example 1: 26  27 InDoc.Systems.Time time1 = new InDoc.Systems.Time("12:00:55"); 28 // calculate 12:00:55 + 14:55:20  29 time1.Add("14:55:20"); 30 // result: 26:56:15 31  32 4. To addition two times you can use + and to subtraction you can use -. 33 Example 2: 34  35 InDoc.Systems.Time time1 = new InDoc.Systems.Time("12:00:55") +  36 new InDoc.Systems.Time("14:55:20"); 37 // result: 26:56:15 38  39 InDoc.Systems.Time time2 = new InDoc.Systems.Time("14:55:20") .  40 new InDoc.Systems.Time("12:00:55"); 41 // result: 02:54:25 42  43 5. We have some convert methods: 44  45 .public int ToSeconds(), 46 .public override string ToString() 47  48 and static method that convert secontd to Time object: 49 .public static Time GetTimeFromSeconds(int seconds). 50  */ 51  52  53 using System; 54 using System.Collections.Generic; 55 using System.Linq; 56 using System.Text; 57  58 namespace MysqlBig 59 { 60  61     /// <summary> 62     ///  63     /// </summary> 64     public class Time 65     { 66         #region Public constants 67  68         public const char TIME_SEPERATOR = ':'; 69  70         #endregion 71  72         #region Declarations 73  74         public int Hour; 75         public int Minute; 76         public int Second; 77  78         #endregion 79  80         #region Constructors 81  82         /// <summary> 83         /// Create time object from current system time. 84         /// </summary> 85         public Time() 86         { 87             Hour = DateTime.Now.Hour; 88             Minute = DateTime.Now.Minute; 89             Second = DateTime.Now.Second; 90         } 91  92         /// <summary> 93         /// Create time object from string value must be seperated as TIME_SEPERATOR constant. 94         /// </summary> 95         /// <param name="value"></param> 96         public Time(string value) 97         { 98             string[] vals = value.Split(TIME_SEPERATOR); //new char[] { ':' }); 99             Hour = int.Parse(vals[0]);100             Minute = int.Parse(vals[1]);101 102             if (vals.Length > 2)103                 Second = int.Parse(vals[2]);104 105             new Time(this.ToSeconds());106         }107 108         /// <summary>109         /// Create time object from parameters hour, minute and seconds.110         /// </summary>111         /// <param name="hour"></param>112         /// <param name="minute"></param>113         /// <param name="second"></param>114         public Time(int hour, int minute, int second)115         {116             Hour = hour;117             Minute = minute;118             Second = second;119             new Time(this.ToSeconds());120         }121 122         /// <summary>123         /// Create time object from seconds.124         /// </summary>125         /// <param name="seconds"></param>126         public Time(int seconds)127         {128             Minute = seconds / 60;129             Second = seconds % 60;130 131             Hour = Minute / 60;132             Minute = Minute % 60;133         }134 135         #endregion136 137         #region Public methods138 139         /// <summary>140         /// Add new time object and addition (+) it to PRevius time object.141         /// </summary>142         /// <param name="time"></param>143         /// <returns></returns>144         public Time Add(Time time)145         {146             this.Hour += time.Hour;147             this.Minute += time.Minute;148             this.Second += time.Second;149 150             return new Time(GetStringTime(this.ToSeconds()));151         }152 153         /// <summary>154         /// Add new string value and addition (+) it to previus time object.155         /// </summary>156         /// <param name="value"></param>157         /// <returns></returns>158         public Time Add(string value)159         {160             return Add(new Time(value));161         }162 163         #endregion164 165         #region Public static methods166 167         /// <summary>168         /// Get current system time.169         /// </summary>170         /// <returns></returns>171         public static Time Now()172         {173             DateTime dt = DateTime.Now;174             return GetTimeFromSeconds(ToSeconds(dt));175         }176 177         /// <summary>178         /// Calculate time difference between two time objects.179         /// </summary>180         /// <param name="time1"></param>181         /// <param name="time2"></param>182         /// <returns></returns>183         public static Time TimeDiff(Time time1, Time time2)184         {185             try186             {187                 int _secs1 = time1.ToSeconds();188                 int _secs2 = time2.ToSeconds();189 190                 int _secs = _secs1 - _secs2;191 192                 return GetTimeFromSeconds(_secs);193             }194             catch195             {196                 return new Time(0, 0, 0);197             }198 199         }200 201         /// <summary>202         /// Calculate time difference between two string values.203         /// </summary>204         /// <param name="time1"></param>205         /// <param name="time2"></param>206         /// <returns></returns>207         public static Time TimeDiff(string time1, string time2)208         {209             try210             {211                 Time t1 = new Time(time1);212                 Time t2 = new Time(time2);213                 return TimeDiff(t1, t2);214             }215             catch216             {217                 return new Time(0, 0, 0);218             }219         }220 221         /// <summary>222         /// Calculate time difference between two DateTime objects.223         /// </summary>224         /// <param name="dateTime1"></param>225         /// <param name="dateTime2"></param>226         /// <returns></returns>227         public static Time TimeDiff(DateTime dateTime1, DateTime dateTime2)228         {229             try230             {231                 TimeSpan span = dateTime1 - dateTime2;232                 return new Time(span.Seconds);233             }234             catch235             {236                 return new Time(0, 0, 0);237             }238         }239 240         /// <summary>241         /// Calculate time difference between two second values.242         /// </summary>243         /// <param name="seconds1"></param>244         /// <param name="seconds2"></param>245         /// <returns></returns>246         public static Time TimeDiff(int seconds1, int seconds2)247         {248             try249             {250                 Time t1 = new Time(seconds1);251                 Time t2 = new Time(seconds2);252                 return TimeDiff(t1, t2);253             }254             catch255             {256                 return new Time(0, 0, 0);257             }258         }259 260         #endregion261 262         #region Convert methods263 264         /// <summary>265         /// Convert current time object to seconds.266         /// </summary>267         /// <returns></returns>268         public int ToSe
上一篇:c#

下一篇:C#迭代器

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表