[Quantproject-developers] QuantProject/b1_ADT/Timing Time.cs, 1.1, 1.2
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-11-14 15:57:53
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Timing In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv5589/b1_ADT/Timing Modified Files: Time.cs Log Message: Added a new constructor, from a time represented by a formatted string; moved constructor's code out from region hashCode (Glauco, check please ...); added some static and instance method Index: Time.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Timing/Time.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Time.cs 12 Nov 2008 20:31:53 -0000 1.1 --- Time.cs 14 Nov 2008 15:57:46 -0000 1.2 *************** *** 30,33 **** --- 30,89 ---- public struct Time : IEquatable<Time> , IComparable<Time> { + #region IsValidTimeFormat + public static bool IsValidTimeFormat ( string stringRepresentingTime ) + { + bool returnValue = false; + char[] chars = stringRepresentingTime.ToCharArray(); + if( + //first - hour + (chars[0]=='0' || chars[0]=='1' || chars[0]=='2') && + //second - hour + ( (chars[0]=='0' || chars[0]=='1') && + (chars[1]=='0' || chars[1]=='1' || chars[1]=='2' || + chars[1]=='3' || chars[1]=='4' || chars[1]=='5' || + chars[1]=='6' || chars[1]=='7' || chars[1]=='8' || chars[1]=='9') || + chars[0] == '2' && + (chars[1]=='0' || chars[1]=='1' || chars[1]=='2' || + chars[1]=='3' || chars[1]=='4') ) && + //third - separator + (chars[2]==':') && + //fourth - minute + (chars[3]=='0' || chars[3]=='1' || chars[3]=='2' || + chars[3]=='3' || chars[3]=='4' || chars[3]=='5') && + //fifth - minute + (chars[4]=='0' || chars[4]=='1' || chars[4]=='2' || + chars[4]=='3' || chars[4]=='4' || chars[4]=='5' || + chars[4]=='6' || chars[4]=='7' || chars[4]=='8' || chars[4]=='9') && + //sixth - separator + (chars[5]==':') && + //seventh - second + (chars[6]=='0' || chars[6]=='1' || chars[6]=='2' || + chars[6]=='3' || chars[6]=='4' || chars[6]=='5') && + //eighth - second + (chars[7]=='0' || chars[7]=='1' || chars[7]=='2' || + chars[7]=='3' || chars[7]=='4' || chars[7]=='5' || + chars[7]=='6' || chars[7]=='7' || chars[7]=='8' || chars[7]=='9') + ) + returnValue = true; + + return returnValue; + } + #endregion isValidTimeFormat + + + /// <summary> + /// Returns a new DateTime, having the date as the given dateTime + /// and the time as the given time + /// </summary> + /// <param name="dateTime">dateTime containing the date to merge with the given time in a new DateTime</param> + /// /// <param name="time">time containing the hour, minute and second to merge with the given dateTime in a new DateTime</param> + /// <returns></returns> + public static DateTime GetDateTimeFromMerge ( DateTime dateTime, Time time ) + { + return new DateTime( dateTime.Year , dateTime.Month , dateTime.Day, + time.Hour , time.Minute , time.Second ); + } + + private DateTime standardDateTime; *************** *** 47,56 **** } - - - #region Equals and GetHashCode implementation - // The code in this region is useful if you want to use this structure in collections. - // If you don't need it, you can just remove the region and the ": IEquatable<Time>" declaration. - public Time( int hour , int minute , int second ) { --- 103,106 ---- *************** *** 66,69 **** --- 116,150 ---- } + private DateTime time_getStandardTimeFromString ( string stringRepresentingTime ) + { + int hour = Convert.ToInt32(stringRepresentingTime.Substring(0,2)); + int minute = Convert.ToInt32(stringRepresentingTime.Substring(3,2)); + int second = Convert.ToInt32(stringRepresentingTime.Substring(6,2)); + return new DateTime(1900, 1, 1, hour, minute, second); + } + + private void time_checkParameter ( string stringRepresentingTime ) + { + if( stringRepresentingTime.Length != 8 || + !Time.IsValidTimeFormat( stringRepresentingTime ) ) + throw new Exception("string is not in the requested time-format hh:mm:ss"); + } + + /// <summary> + /// Represent a time (for any possible date) + /// </summary> + /// <param name="stringRepresentingTime">String representing time in format: hh:mm:ss</param> + public Time( string stringRepresentingTime ) + { + this.standardDateTime = new DateTime(1900,1,1,0,0,0); + //just for compiling next two lines + this.time_checkParameter( stringRepresentingTime ); + this.standardDateTime = + this.time_getStandardTimeFromString( stringRepresentingTime ); + } + + #region Equals and GetHashCode implementation + // The code in this region is useful if you want to use this structure in collections. + // If you don't need it, you can just remove the region and the ": IEquatable<Time>" declaration. public override bool Equals(object obj) { *************** *** 134,137 **** --- 215,233 ---- return compareTo; } + + /// <summary> + /// True iff the time of the given dateTime is equal to + /// the current instance + /// </summary> + /// <param name="dateTime">dateTime containing time to compare to the current instance</param> + /// <returns></returns> + public bool HasTheSameTime( DateTime dateTime ) + { + bool returnValue = + ( this.Hour == dateTime.Hour ) && + ( this.Minute == dateTime.Minute ) && + ( this.Second == dateTime.Second ); + return returnValue; + } } } |