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 explantatio ...
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 get='_blank'>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 ToSeconds()269 {270 return this.Hour * 3600 + this.Minute * 60 + this.Second;271 }272 273 /// <summary>274 /// Convert DateTime object to seconds.275 /// </summary>276 /// <param name="dateTime"></param>277 /// <returns></returns>278 public static int ToSeconds(DateTime dateTime)279 {280 return dateTime.Hour * 3600 + dateTime.Minute * 60 + dateTime.Second;281 }282 283 /// <summary>284 /// Convert current time object to string.285 /// </summary>286 /// <returns></returns>287 public override string ToString()288 {289 return String.Format("{0:00}:{1:00}:{2:00}", Hour, Minute, Second);290 }291 292 /// <summary>293 /// Convert seconds to time object.294 /// </summary>295 /// <param name="seconds"></param>296 /// <returns></returns>297 public static Time GetTimeFromSeconds(int seconds)298 {299 int _mins = seconds / 60;300 seconds = seconds % 60;301 302 int _hours = _mins / 60;303 _mins = _mins % 60;304 305 return new Time(_hours, _mins, seconds);306 }307 308 309 /// <summary>310 /// Convert seconds to string time.311 /// </summary>312 /// <param name="seconds"></param>313 /// <returns></returns>314 private string GetStringTime(int seconds)315 {316 int _mins = seconds / 60;317 seconds = seconds % 60;318 319 int _hours = _mins / 60;320 _mins = _mins % 60;321 322 this.Hour = _hours;323 this.Minute = _mins;324 this.Second = seconds;325 326 return String.Format("{0:00}:{1:00}:{2:00}", _hours, _mins, seconds); ;327 }328 329 /// <summary>330 /// Parse string to time.331 /// </summary>332 /// <param name="value"></param>333 /// <returns></returns>334 public static Time Parse(string value)335 {336 try337 {338 return new Time(value);339 }340 catch341 {342 throw new ApplicationException("Error parsing time!");343 }344 }345 346 #endregion347 348 #region Subtract time objects349 350 public static Time operator +(Time t1, Time t2)351 {352 Time t3 = new Time(t1.Hour, t1.Minute, t1.Second);353 t3.Add(t2);354 return t3;355 }356 357 public static Time operator -(Time t1, Time t2)358 {359 return TimeDiff(t1, t2);360 }361 362 #endregion363 }364 }
原标题:C# Time Class using MySQL
关键词:C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。