C # versions of the Timer class are accurate to microseconds, and a decimal point is retained to support output in units of year, month, day, hour, minute, second

Source: Internet
Author: User

 Class timecount <br/>{< br/> // temporary variable that stores the maximum year value that the current class can represent <br/> Private Static ulong maxyear = 0; </P> <p> /// <summary> <br/> // obtain the maximum number of copies per year in milliseconds <br/> /// </Summary> <br/> // <returns> maximum number of copies per year </returns> <br/> Public static ulong getmaxyearcount () <br/>{< br/> If (timecount. maxyear! = 0) <br/> return timecount. maxyear; <br/> else <br/> {<br/> double ulongmax = math. pow (2, 64); <br/> double yeartomiliseconds = (double) 12 * (double) 30 * (double) 24 * (double) 3600 * (double) 1000; <br/> timecount. maxyear = (ulong) (ulongmax/yeartomiliseconds); <br/> return timecount. maxyear; <br/>}</P> <p> // <summary> <br/> // converts the current system time to milliseconds. <br/> /// </Summary> <br/> /// <returns> current system time The corresponding number of milliseconds </returns> <br/> Public static ulong getcurrenttimebymilisec () <br/>{< br/> datetime T = datetime. now; <br/> ulong millisecond = (ulong) T. year * 12 + (ulong) T. month) * 30 + (ulong) T. day) * 24 + (ulong) T. hour) * 60 + (ulong) T. minute) * 60 <br/> + (ulong) T. second) * 1000 + (ulong) (T. millisecond); <br/> return millisecond; <br/>}</P> <p> /// <summary> <br/> // less practical function, given three inputs, get the time difference, in string form <Br/> /// </Summary> <br/> // <Param name = "str"> returns the prefix before the specified time, for example, "the time used to draw XX is: "</param> <br/> /// <Param name =" A "> first time </param> <br/> /// <Param name =" B "> second time, the absolute value </param> <br/> // <returns> </returns> <br/> Public static string gettimeinterval (string STR, ulong A, ulong B) <br/>{< br/> ulong interval = B-A; <br/> STR + = convert. tostring (math. ABS (double) interval)/1000); <br/> Str + = "Seconds"; <br/> return STR; <br/>}</P> <p> // <summary> <br/> // obtain the time difference between the two specified time (milliseconds, the final value will go to the absolute value <br/> /// </Summary> <br/> /// <Param name = "A"> earlier time (in milliseconds) </param> <br/> // <Param name = "B"> later time (MS) </param> <br/> // <returns> </returns> <br/> Public static double gettimeintervalbysec (ulong A, ulong B) <br/>{< br/> return math. ABS (double) B-(double) A)/(double) 1000); <br/>}</P> <p> // <summary> <br />/// Retain a decimal point <br/> /// </Summary> <br/> /// <Param name = "double1"> a decimal point </param> <br/> // <returns> one string </returns> <br/> Public static string remainonefigureafterdot (double double1) <br/>{< br/> string tempstr = double1.tostring (); <br/> string double1str; <br/> If (tempstr = "") <br/>{< br/> return "0.0"; <br/>}< br/> int Index = tempstr. indexof (". "); <br/> If (Index =-1) <br/> {<br/> Double1str = tempstr + ". 0 "; <br/> return double1str; <br/>}< br/> else <br/>{< br/> If (tempstr. length <index + 2) <br/> return tempstr + ". 0 "; <br/> else <br/> {<br/> double1str = tempstr. substring (0, index + 2); <br/> return double1str; <br/>}</P> <p> /// <summary> <br/> // converts the time to the second (year, month, and day). <br/> // </Summary> <br/> Public Enum ymdhms <br/> {<br/> yearsc = 365*24*3600, <br/> Monsc = 30*24*3600, <br/> daysc = 24*3600, <br/> housc = 3600, <br/> minsc = 60 <br/>}</P> <p> /// <summary> <br/> /// converts a string to seconds. unit time, converts the string to a time string in the unit of year, month, day, hour, minute, and second, and keep one decimal point in the last second <br/> /// </Summary> <br/> /// <Param name = "seconds"> seconds, string format </param> <br/> // <returns> </returns> <br/> Public static string secondstoyymmddhhmmss (string seconds) <br/>{< br/> double secondsdble = convert. todouble (SE Conds); <br/> return timecount. secondstoyymmddhhmmss (secondsdble ); <br/>}</P> <p> // <summary> <br/> // time in seconds in the int format, converts the string to a time string in the unit of year, month, day, hour, minute, and second, and keep one decimal point in the last second <br/> /// </Summary> <br/> /// <Param name = "seconds"> seconds, int format </param> <br/> // <returns> </returns> <br/> Public static string secondstoyymmddhhmmss (INT seconds) <br/>{< br/> double secondsdble = convert. todouble (seconds); <br/> return timecoun T. secondstoyymmddhhmmss (secondsdble ); <br/>}</P> <p> // <summary> <br/> // time in seconds in ulong format, converts the string to a time string in the unit of year, month, day, hour, minute, and second, and keep one decimal point in the last second <br/> /// </Summary> <br/> /// <Param name = "seconds"> seconds, ulong format </param> <br/> // <returns> </returns> <br/> Public static string secondstoyymmddhhmmss (ulong seconds) <br/>{< br/> double secondsdble = convert. todouble (seconds); <br/> return timecount. secondstoyymmddhhmmss (Secondsdble ); <br/>}</P> <p> // <summary> <br/> // time in seconds in double format, converts the string to a time string in the unit of year, month, day, hour, minute, and second, and retain one decimal point in the last second <br/> /// </Summary> <br/> /// <Param name = "secondsdble"> seconds, double format </param> <br/> // <returns> </returns> <br/> Public static string secondstoyymmddhhmmss (double secondsdble) <br/>{< br/> string returnstr = ""; <br/> If (secondsdble = 0) <br/>{< br/> return "0.0 seconds"; <br/>}< br/> int YY = 0, Mm = 0, DD = 0, HH = 0, Mm = 0; <br/> If (secondsdble> = (uint) ymdhms. yearsc) // year <br/>{< br/> while (secondsdble> 0) <br/>{< br/> secondsdble-= (uint) ymdhms. yearsc; <br/> YY ++; <br/>}< br/> secondsdble + = (uint) ymdhms. yearsc; <br/> YY --; <br/>}< br/> If (secondsdble> = (uint) ymdhms. monsc) // month <br/>{< br/> while (secondsdble> 0) <br/>{< br/> secondsdble-= (uint) ymdhms. monsc; <br/> MM ++; <BR/>}< br/> secondsdble + = (uint) ymdhms. monsc; <br/> MM --; <br/>}< br/> If (secondsdble> = (uint) ymdhms. daysc) // day <br/>{< br/> while (secondsdble> 0) <br/>{< br/> secondsdble-= (uint) ymdhms. daysc; <br/> dd ++; <br/>}< br/> secondsdble + = (uint) ymdhms. daysc; <br/> dd --; <br/>}< br/> If (secondsdble> = (uint) ymdhms. housc) // <br/>{< br/> while (secondsdble> 0) <br/>{< br/> secondsdble-= (Uint) ymdhms. housc; <br/> Hh ++; <br/>}< br/> secondsdble + = (uint) ymdhms. housc; <br/> hh --; <br/>}< br/> If (secondsdble> = (uint) ymdhms. minsc) // <br/>{< br/> while (secondsdble> 0) <br/>{< br/> secondsdble-= (uint) ymdhms. minsc; <br/> MM ++; <br/>}< br/> secondsdble + = (uint) ymdhms. minsc; <br/> MM --; <br/>}< br/> If (yy! = 0) <br/> returnstr + = yy. tostring () + "year"; <br/> If (Mm! = 0) <br/> returnstr + = mm. tostring () + "month"; <br/> If (DD! = 0) <br/> returnstr + = dd. tostring () + "day"; <br/> If (hh! = 0) <br/> returnstr + = HH. tostring () + "Hour"; <br/> If (Mm! = 0) <br/> returnstr + = mm. tostring () + "points"; <br/> If (secondsdble <10) <br/> {<br/> returnstr + = "0" + timecount. remainonefigureafterdot (secondsdble) + "second"; <br/>}< br/> else <br/>{< br/> returnstr + = timecount. remainonefigureafterdot (secondsdble) + "second"; <br/>}< br/> return returnstr; <br/>}</P> <p>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.