Date Time.h 5.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /******************************************************************************
  2. Use 'DateTime' to handle time and date information.
  3. /******************************************************************************/
  4. struct DateTime
  5. {
  6. Byte second, // 0..59
  7. minute, // 0..59
  8. hour , // 0..23
  9. day , // 1..31
  10. month ; // 1..12
  11. Int year ;
  12. // get
  13. Bool valid ( )C; // if current date time is valid
  14. Int days ( )C; // get days since 1 January 0 year
  15. Long seconds ( )C; // get seconds since 1 January 0 year
  16. Long seconds1970( )C; // get seconds since 1 January 1970 year (Unix Time)
  17. Str asText (Bool include_seconds=true)C; // get date in text format "YYYY-MM-DD HH:MM:SS"
  18. Str asFileName (Bool include_seconds=true)C; // get date in file name format "YYYY-MM-DD HH,MM,SS"
  19. // set
  20. DateTime& zero ( ); // set date and time to zero
  21. DateTime& getLocal ( ); // set from current DateTime (local time zone)
  22. DateTime& getUTC ( ); // set from current DateTime (UTC time zone)
  23. DateTime& incMonth ( ); // increase by 1 month
  24. DateTime& decMonth ( ); // decrease by 1 month
  25. DateTime& incDay ( ); // increase by 1 day
  26. DateTime& decDay ( ); // decrease by 1 day
  27. DateTime& incHour ( ); // increase by 1 hour
  28. DateTime& decHour ( ); // decrease by 1 hour
  29. DateTime& incMinute ( ); // increase by 1 minute
  30. DateTime& decMinute ( ); // decrease by 1 minute
  31. DateTime& incSecond ( ); // increase by 1 second
  32. DateTime& decSecond ( ); // decrease by 1 second
  33. DateTime& toUTC ( ); // convert from local to UTC time zone
  34. DateTime& toLocal ( ); // convert from UTC to local time zone
  35. DateTime& fromSeconds( Long s); // set date from seconds since 1 January 0 year
  36. DateTime& from1970s (ULong s); // set date from seconds since 1 January 1970 year (Unix Time)
  37. DateTime& from1970ms (ULong ms); // set date from milliseconds since 1 January 1970 year
  38. DateTime& fromText (C Str &t); // set date from text format "YYYY-MM-DD HH:MM:SS" ("YYYY-MM-DD HH:MM" format is also supported), 'zero' method is called on fail
  39. #if EE_PRIVATE && APPLE
  40. DateTime& from (NSDate *date); // set date from milliseconds since 1 January 1970 year
  41. #endif
  42. // io
  43. Bool save(File &f)C; // false on fail
  44. Bool load(File &f) ; // false on fail
  45. };
  46. STRUCT(DateTimeMs , DateTime) // DateTime uncluding milliseconds
  47. //{
  48. UShort millisecond; // 0..999
  49. Long milliseconds1970()C; // get milliseconds since 1 January 1970 year
  50. DateTimeMs& zero ( ); // set date and time to zero
  51. DateTimeMs& getLocal ( ); // set from current DateTime (local time zone)
  52. DateTimeMs& getUTC ( ); // set from current DateTime (UTC time zone)
  53. DateTimeMs& fromSeconds( Long s); // set date from seconds since 1 January 0 year
  54. DateTimeMs& from1970s (ULong s); // set date from seconds since 1 January 1970 year (Unix Time)
  55. DateTimeMs& from1970ms (ULong ms); // set date from milliseconds since 1 January 1970 year
  56. };
  57. /******************************************************************************/
  58. // compare
  59. Int Compare (C DateTime &d0, C DateTime &d1 ); // compare
  60. Int Compare (C DateTime &d0, C DateTime &d1, Int epsilon); // compare using 'epsilon' for seconds tolerance
  61. inline Bool operator==(C DateTime &d0, C DateTime &d1 ) {return Compare(d0, d1)==0;} // if equal
  62. inline Bool operator!=(C DateTime &d0, C DateTime &d1 ) {return Compare(d0, d1)!=0;} // if not equal
  63. inline Bool operator>=(C DateTime &d0, C DateTime &d1 ) {return Compare(d0, d1)>=0;} // if greater or equal
  64. inline Bool operator<=(C DateTime &d0, C DateTime &d1 ) {return Compare(d0, d1)<=0;} // if smaller or equal
  65. inline Bool operator> (C DateTime &d0, C DateTime &d1 ) {return Compare(d0, d1)> 0;} // if greater
  66. inline Bool operator< (C DateTime &d0, C DateTime &d1 ) {return Compare(d0, d1)< 0;} // if smaller
  67. Long operator+(C DateTime &d0, C DateTime &d1); // return sum of DateTime seconds
  68. Long operator-(C DateTime &d0, C DateTime &d1); // return difference between DateTime seconds
  69. Bool LeapYear (Int year ); // check if 'year' is a leap year
  70. Int MonthDays(Int month ); // return number of days in a month, 'month'=1..12, -1 on fail
  71. Int MonthDays(Int month, Int year); // return number of days in a month, 'month'=1..12, -1 on fail, this makes additional check to the 'year' if it's a leap year
  72. CChar8* MonthNameShort(Int month); // get month short name (Jan , Feb , Mar , ..), 'month'=1..12, null on fail
  73. CChar8* MonthName (Int month); // get month full name (January, February, March, ..), 'month'=1..12, null on fail
  74. enum TIME_NAME
  75. {
  76. TIME_NAME_SHORT , // short names, lower case
  77. TIME_NAME_MED , // medium names, lower case
  78. TIME_NAME_MED_UP , // medium names, upper case
  79. TIME_NAME_LONG , // long names, lower case
  80. TIME_NAME_LONG_UP, // long names, upper case
  81. };
  82. Str TimeText (Long seconds, TIME_NAME name=TIME_NAME_SHORT, Int parts=-2); // convert seconds to string, Sample Usage: TimeText (61) -> "1m 1s"
  83. Str TimeTextHour(Long seconds, TIME_NAME name=TIME_NAME_SHORT, Int parts=-2); // convert seconds to string, Sample Usage: TimeTextHour(61) -> "1m 1s", this works like 'TimeText' but does not display "years months days", but only "hours minutes seconds"
  84. /******************************************************************************/