你的位置:首页 > ASP.net教程

[ASP.net教程]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 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 }

用法:

 /// <summary>  ///   /// </summary>  public class AttendrecordInfo  {    int _Seq;    public int Seq    {      set { _Seq = value; }      get { return _Seq; }    }    string _Emp_no;    public string Emp_no    {      set { _Emp_no = value; }      get { return _Emp_no; }    }    DateTime _Rdate;    public DateTime Rdate    {      set { _Rdate = value; }      get { return _Rdate; }    }    Time _Ttime;    public Time Ttime    {      set { _Ttime = value; }      get { return _Ttime; }    }    string _Rdescription;    public string Rdescription    {      set { _Rdescription = value; }      get { return _Rdescription; }    }    string _Rdes_reasnon;    public string Rdes_reasnon    {      set { _Rdes_reasnon = value; }      get { return _Rdes_reasnon; }    }    string _Branch;    public string Branch    {      set { _Branch = value; }      get { return _Branch; }    }  }

   /// <summary>    ///     /// </summary>    /// <param name="seq"></param>    /// <returns></returns>    public AttendrecordInfo SelectAttendrecord(int seq)    {      AttendrecordInfo attendrecord = null;      try      {        MySqlParameter par = new MySqlParameter("?param1", MySqlDbType.Int32, 5);        par.Value = seq;        using (MySqlDataReader reader = MySqlHelpDu.GetReader("proc_Select_attendrecord", CommandType.StoredProcedure, par))        {          if (reader.Read())          {            attendrecord = new AttendrecordInfo();            attendrecord.Seq = (!object.Equals(reader["seq"], null)) ? (int)reader["seq"] : 0;            attendrecord.Branch = (!object.Equals(reader["branch"], null)) ? (string)reader["branch"] : "";            attendrecord.Emp_no = (!object.Equals(reader["emp_no"], null)) ? (string)reader["emp_no"] : "";            attendrecord.Rdate = (!object.Equals(reader["rdate"], null)) ? DateTime.Parse(reader["rdate"].ToString()): DateTime.Now;            attendrecord.Ttime = (!object.Equals(reader["rtime"], null)) ? Time.Parse(reader["rtime"].ToString()): Time.Now();            attendrecord.Rdes_reasnon = (!object.Equals(reader["rdes_reasnon"], null)) ? (string)reader["rdes_reasnon"] : "";            attendrecord.Rdescription = (!object.Equals(reader["rdescription"], null)) ? (string)reader["rdescription"] : "";          }        }      }      catch (MySqlException ex)      {        throw ex;      }      return attendrecord;    }

Mysql 表:

 1 create table attendrecord 2 ( 3   seq INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 4   emp_no varchar(20) null, 5   rdate datetime not null, 6   rtime time not null, 7   rdescription varchar(100), 8   rdes_reasnon varchar(100), 9   branch varchar(50)10 );11 12 insert into attendrecord(emp_no,rdate,rtime,rdescription,rdes_reasnon,branch) values('L00094','2015-03-10','10:45','geovindu','du','sz');