EADateTime.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  3. <title>EADateTime</title>
  4. <link type="text/css" rel="stylesheet" href="UTFDoc.css">
  5. <meta name="author" content="Paul Pedriana">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8. <h1>EADateTime</h1>
  9. <h2>Introduction</h2>
  10. <p>EADateTime provides a unified implementation of date, time, and calendar functionality.
  11. The DateTime class represents date and time in a single class. Unlike other
  12. date/time systems, this class doesn't have a strictly limited resolution, due
  13. to its use of 64 bits (instead of 32 bits) of storage for the date/time value.
  14. </p>
  15. <p> EADateTime supports the modern Gregorian calendar correctly, including such
  16. concepts as leap years. You can, for example, use EADateTime to implement proper
  17. calendar functionality for your application.</p>
  18. <p>The C standard library provides some time and date functionality, but it is
  19. slow, has some limitations, is not very easy to use, and isn't supported by
  20. all C/C++ compilers. Writing a calendar application with the C time and date
  21. functions would be possible, but would be tedious and error-prone.</p>
  22. <p>EADateTime does not support the formatting of date and time strings for display
  23. to a users. Such functionality is outside the scope of this class, primarily
  24. due to the issue of multiple string encodings (e.g. ASCII and Unicode) and due
  25. to the existence of multiple locales (e.g. English and French) which use different
  26. formats for time and date strings. A separate module -- EALocale -- exists which
  27. implements string formatting of dates and times. You can use that module and
  28. EADateTime together to implement locale-savvy times, dates, and calendars.</p>
  29. <h2>Example usage </h2>
  30. <p>All examples presume a <font face="Courier New, Courier, mono" size="-1">#include
  31. &quot;Foundation/EADateTime.h&quot;</font> and assume the using of <font face="Courier New, Courier, mono" size="-1">namespace
  32. EA</font>.</p>
  33. <p>Basic usage of class DateTime:</p>
  34. <pre class="code-example">DateTime dt; // Sets dt to the current date and time.
  35. uint32_t year = dt.GetParameter(kParameterYear); // Get the current year.
  36. dt.SetParameter(kParameterMonth, kMonthAugust); // Set month to August.
  37. dt.AddTime(kParameterDayOfYear, 77); // Add 77 days.</pre>
  38. <p>Comparison of DateTime instances:</p>
  39. <pre class="code-example">DateTime dtD(2008, kMonthDecember, 25); // Sets dt to Christmas 2008.
  40. DateTime dtH(2008, kMonthOctober, 31); // Sets dt to Halloween 2008.
  41. if(dtH &lt; dtD)
  42. printf(&quot;Halloween occurs before Christmas.&quot;);
  43. else
  44. printf(&quot;Christmas occurs before Halloween.&quot;);</pre>
  45. <blockquote></blockquote>
  46. <p>How to save and restore a DateTime to and from disk:</p>
  47. <pre><span class="code-example">DateTime dt;
  48. uint64_t n = dt.GetSeconds(); // Get the absolute time in seconds. This alone fully represents a DateTime.
  49. WriteToDisk64(ToBigEndian(n)); // Hypthetical disk writing function and endian conversion function.
  50. n = FromBigEndian(ReadFromDisk64()); // Hypothetical disk reading function and endian conversion function.
  51. dt = DateTime(n); // Restore the saved value to a DateTime instance.</span></pre>
  52. <p>Usage of standalone EADateTime utility functions, which are independent of
  53. class DateTime: </p>
  54. <pre class="code-example">bool b = IsLeapYear(2015);
  55. uint32_t n = GetDaysInYear(1974);
  56. uint32_t d = GetDaysInMonth(kMonthMarch, 2007);
  57. int64_t nSeconds = GetDaylightSavingsBias();</pre>
  58. <h2></h2>
  59. Conversion of C time to DateTime:
  60. <pre class="code-example">#include &lt;time.h&gt;
  61. tm time;
  62. asctime(&amp;ascTime); // Get C time.
  63. DateTime dt;
  64. TmToDateTime(time, dt); // Convert to DateTime.</pre>
  65. <h2>Interface</h2>
  66. <p>The following is defined in EADateTime.h. The latest versions of these definitions
  67. can be found in the current version of EADateTime.h</p>
  68. <p>Enumerations</p>
  69. <pre class="code-example">enum Month
  70. {
  71. kMonthUnknown = 0,
  72. kMonthJanuary = 1,
  73. kMonthFebruary = 2,
  74. kMonthMarch = 3,
  75. kMonthApril = 4,
  76. kMonthMay = 5,
  77. kMonthJune = 6,
  78. kMonthJuly = 7,
  79. kMonthAugust = 8,
  80. kMonthSeptember = 9,
  81. kMonthOctober = 10,
  82. kMonthNovember = 11,
  83. kMonthDecember = 12
  84. };
  85. enum DayOfMonth
  86. {
  87. kDayOfMonthUnknown = 0,
  88. kDayOfMonthMin = 1,
  89. kDayOfMonthMax = 31 <font color="#999999">/// The actual max month is dependent on which month is being referred to.</font>
  90. };
  91. enum DayOfWeek
  92. {
  93. kDayOfWeekUnknown = 0,
  94. kDayOfWeekSunday = 1,
  95. kDayOfWeekMonday = 2,
  96. kDayOfWeekTuesday = 3,
  97. kDayOfWeekWednesday = 4,
  98. kDayOfWeekThursday = 5,
  99. kDayOfWeekFriday = 6,
  100. kDayOfWeekSaturday = 7
  101. };
  102. <span class="code-example-comment">/// TimeFrame
  103. </span>enum TimeFrame
  104. {
  105. kTimeFrameUnknown = 0, <font color="#999999">/// Unspecified time frame.</font>
  106. kTimeFrameUTC = 1, <font color="#999999">/// Universal Coordinated Time. This is the time based on the time zone at Greenwich, England.</font>
  107. kTimeFrameLocal = 2 <font color="#999999">/// Same time as current machine.</font>
  108. };
  109. <span class="code-example-comment">/// TimeZone
  110. /// Standard time zone definitions, with their values corresponding to the nmuber of hours they are
  111. /// off relative to UTC (Universal Coordinated Time, which is the time at Greenwich England).
  112. /// Note, for example, that kTimeZonePacific is -8 hours relative to Greenwich, England.
  113. </span>enum TimeZone
  114. {
  115. kTimeZoneEniwetok = -12,
  116. kTimeZoneSamoa = -11,
  117. kTimeZoneHawaii = -10,
  118. kTimeZoneAlaska = -9,
  119. kTimeZonePacific = -8,
  120. kTimeZoneMountain = -7,
  121. kTimeZoneCentral = -6,
  122. kTimeZoneEastern = -5,
  123. kTimeZoneAltantic = -4,
  124. kTimeZoneBrazilia = -3,
  125. kTimeZoneMidAtlantic = -2,
  126. kTimeZoneAzores = -1,
  127. kTimeZoneGreenwich = 0,
  128. kTimeZoneRome = +1,
  129. kTimeZoneIsrael = +2,
  130. kTimeZoneMoscow = +3,
  131. kTimeZoneBaku = +4,
  132. kTimeZoneNewDelhi = +5,
  133. kTimeZoneDhakar = +6,
  134. kTimeZoneBangkok = +7,
  135. kTimeZoneHongKong = +8,
  136. kTimeZoneTokyo = +9,
  137. kTimeZoneSydney = +10,
  138. kTimeZoneMagadan = +11,
  139. kTimeZoneWellington = +12
  140. };
  141. <span class="code-example-comment">/// Epoch
  142. /// The use of an epoch is to provide a timeframe with which to work.
  143. /// Most of the time you don't need to know about this.
  144. </span>enum Epoch
  145. {
  146. kEpochUnknown = 0,
  147. kEpochJulian = 1, <font color="#999999">/// Began at noon, January 1, 4712 BC.</font>
  148. kEpochModifiedJulian = 2, <font color="#999999">/// Began at midnight, November 17, 1858. Exactly 2,400,000.5 days after Julian epoch began.</font>
  149. kEpochGregorian = 3, <font color="#999999">/// Began at midnight, September 14, 1752.</font>
  150. kEpoch1900 = 4, <font color="#999999">/// Began at midnight, January 1, 1900.</font>
  151. kEpoch1950 = 5, <font color="#999999">/// Began at midnight, January 1, 1950.</font>
  152. kEpoch1970 = 6, <font color="#999999">/// Began at midnight, January 1, 1970. Same epoch used by C runtime library and Unix OS.</font>
  153. kEpoch2000 = 7 <font color="#999999">/// Began at midnight, January 1, 2000.</font>
  154. };
  155. enum Era
  156. {
  157. kEraUnknown = 0,
  158. kEraBC = 1,
  159. kEraAD = 2
  160. };
  161. <span class="code-example-comment">/// Parameter
  162. /// Defines a date or time parameter.
  163. </span>enum Parameter
  164. {
  165. kParameterUnknown = 0,
  166. kParameterYear = 1, <font color="#999999">/// Refers to full year value, such as 1994, 2006, etc. but not a two digit value such as 94 or 04. The valid range is 0-INT_MAX.</font>
  167. kParameterMonth = 2, <font color="#999999">/// Refers to month of year, starting with 1 for January. The valid range is 1-12.</font>
  168. kParameterWeekOfYear = 3, <font color="#999999">/// Refers to the week of the year, starting with 1 for the week of January 1. The valid range is 1-52.</font>
  169. kParameterWeekOfMonth = 4, <font color="#999999">/// Refers to the week of the month, starting with 1 for the first week. The valid range is 1-5.</font>
  170. kParameterDayOfYear = 5, <font color="#999999">/// Refers to a day of the year, starting with 1 for January 1st. The valid range is 1-366.</font>
  171. kParameterDayOfMonth = 6, <font color="#999999">/// Refers to the day of the month, starting with 1 for the first of the month. The valid range is 1-31.</font>
  172. kParameterDayOfWeek = 7, <font color="#999999">/// Refers to the day of the week, starting with 1 for Sunday. The valid range is 1-7.</font>
  173. kParameterHour = 8, <font color="#999999">/// Refers to the hour of a day in 24 hour format, starting with 0 for midnight. The valid range is 0-23.</font>
  174. kParameterMinute = 9, <font color="#999999">/// Refers to the minute of the hour, starting with 0 for the first minute. The valid range is 0-59.</font>
  175. kParameterSecond = 10, <font color="#999999">/// Refers to the second of the minute, starting with 0 for the first second. The valid range is 0-59.</font>
  176. };
  177. <span class="code-example-comment">/// Conversions
  178. /// Defines useful conversion multipliers between seconds, minutes, hours, and days.
  179. /// Conversions are not defined for some entities (e.g. days per year) because the
  180. /// value changes based on the particular year.
  181. </span>enum Conversions
  182. {
  183. kSecondsPerMinute = 60,
  184. kSecondsPerHour = 3600,
  185. kSecondsPerDay = 86400,
  186. kMinutesPerHour = 60,
  187. kMinutesPerDay = 1440,
  188. kHoursPerDay = 24,
  189. kDaysPerWeek = 7,
  190. kWeeksPerYear = 52,
  191. kMonthsPerYear = 12
  192. };
  193. </pre>
  194. <p>DateTime</p>
  195. <pre class="code-example">class DateTime
  196. {
  197. public:
  198. static const uint32_t kValueDefault = 0xffffffff;
  199. static const uint32_t kValueIgnored = 0xffffffff;
  200. public:
  201. DateTime(TimeFrame timeFrame = kTimeFrameLocal);
  202. DateTime(int64_t nSeconds) : mnSeconds(nSeconds);
  203. DateTime(const DateTime& dateTime) : mnSeconds(dateTime.mnSeconds);
  204. <span class="code-example-comment"> /// DateTime
  205. /// Constructs a DateTime class object from some standard parameters.
  206. /// To construct a DateTime class with a different set of parameter types,
  207. /// you'll need to use the Set function or in odd cases do manual calculations.
  208. </span> DateTime(uint32_t nYear, uint32_t nMonth, uint32_t nDayOfMonth,
  209. uint32_t nHour = 0, uint32_t nMinute = 0, uint32_t nSecond = 0);
  210. <span class="code-example-comment"> /// operator=
  211. </span> DateTime& operator=(const DateTime& dateTime);
  212. <span class="code-example-comment"> /// Compare
  213. /// This function compares this object with another DateTime object and
  214. /// returns an integer result.The return value is the same as with the
  215. /// strcmp string comparison function. If this object is less than the
  216. /// argument dateTime, the return value is < 0. Comparison operators are
  217. /// defined outside this class, though they use the Compare function to do their work.
  218. </span> int Compare(const DateTime& dateTime, bool bCompareDate = true, bool bCompareTime = true) const;
  219. <span class="code-example-comment"> /// GetParameter
  220. /// Gets the given parameter. If you want to get the year, you would call Get(kParameterYear).
  221. </span> uint32_t GetParameter(Parameter parameter) const;
  222. <span class="code-example-comment"> /// SetParameter
  223. /// Sets the given parameter. If you want to set the year to 1999, you would
  224. /// call Set(kParameterYear, 1999).
  225. </span> void SetParameter(Parameter parameter, uint32_t nValue);
  226. <span class="code-example-comment"> /// Set
  227. /// Sets the time based on the current time. If the timeFrame is kTimeFrameUTC,
  228. /// the time is set to what the current UTC time is, which will be a fixed number
  229. /// of hours off of what the current local time is.
  230. </span> void Set(TimeFrame timeFrame = kTimeFrameLocal);
  231. <span class="code-example-comment"> /// Set
  232. /// Sets the time based on various inputs. If any input is kValueIgnored (uint32_t)-1,
  233. /// then the input is ignored and the current value is used. If any of the cyclic
  234. /// inputs is beyond its valid range, the modulo of the value is used and the division
  235. /// of the value is added to the next higher bracket. For example, if the input nMinute
  236. /// is 65, then the minute used is 5 and 1 is added to the current hour value.
  237. </span> void Set(uint32_t nYear, uint32_t nMonth, uint32_t nDayOfMonth, uint32_t nHour,
  238. uint32_t nMinute, uint32_t nSecond);
  239. <span class="code-example-comment"> /// AddTime
  240. /// Allows you to increment (or decrement) the given parameter by the given amount.
  241. </span> void AddTime(Parameter parameter, int32_t nValue);
  242. <span class="code-example-comment"> /// IsDST
  243. /// Returns true if the time is daylight savings time. This function assumes that DST is valid
  244. /// for the given current locale; some locales within the United States don't observe DST.
  245. </span> bool IsDST() const;
  246. <span class="code-example-comment"> /// GetSeconds
  247. /// Returns the DateTime internal representation.
  248. </span> int64_t GetSeconds() const;
  249. };</pre>
  250. <blockquote></blockquote>
  251. <p>Utility</p>
  252. <pre class="code-example"><span class="code-example-comment">/// IsLeapYear
  253. /// Returns true if the given year is a leap year.
  254. </span>bool IsLeapYear(uint32_t nYear);
  255. <span class="code-example-comment">/// GetDaysInYear
  256. /// Returns the number of days in the given year. The value will vary based on whether
  257. /// the year is a leap year or not.
  258. </span>uint16_t GetDaysInYear(uint32_t nYear);
  259. <span class="code-example-comment">/// GetDaysInMonth
  260. /// Returns the number of days in the given month. The value will vary based on the
  261. /// month and based on whether the year is a leap year. The input nMonth takes one
  262. /// of enum Month or an integer equivalent.
  263. </span>uint8_t GetDaysInMonth(uint32_t nMonth, uint32_t nYear);
  264. <span class="code-example-comment">/// GetDayOfYear
  265. /// The input nMonth takes one of enum Month or an integer equivalent.
  266. /// The input nDayOfMonth takes an integer consistent with enum DayOfMonth.
  267. </span>uint32_t GetDayOfYear(uint32_t nMonth, uint32_t nDayOfMonth, uint32_t nYear);
  268. <span class="code-example-comment">/// Convert4DigitTo2DigitYear
  269. /// Note that two-digit years bring a number of problems; they are best used for text
  270. /// display only and not for any internal processing.
  271. </span>inline int Convert4DigitTo2DigitYear(int nYear4);
  272. <span class="code-example-comment">/// Convert2DigitTo4DigitYear
  273. /// This code returns a year in the 1900s if the input year is > 30 but returns
  274. /// a year in the 2000s if the year is &lt;= 30. This is merely a guess and in fact there
  275. /// really is no good way to reliably convert a two digit year to a four digit year.
  276. </span>inline int Convert2DigitTo4DigitYear(int nYear2);
  277. <span class="code-example-comment">/// GetCurrent
  278. /// Returns the current year, month, hour, etc.
  279. </span>uint32_t GetCurrent(Parameter parameter);
  280. <span class="code-example-comment">/// GetDaylightSavingsBias
  281. /// Returns the number of seconds that the current time is daylight savings adjusted from
  282. /// the conventional time. Adding this value to the conventional time yields the time when
  283. /// adjusted for daylight savings.
  284. </span>int64_t GetDaylightSavingsBias();
  285. <span class="code-example-comment">/// GetTimeZoneBias
  286. /// Returns the number of seconds that the local time zone is off of UTC.
  287. /// Adding this value to the current UTC yields the current local time.
  288. </span>int64_t GetTimeZoneBias();
  289. <span class="code-example-comment">/// DateTimeToTm
  290. /// Converts a DateTime to a C tm struct.
  291. </span>void DateTimeToTm(const DateTime& dateTime, tm& time);
  292. <span class="code-example-comment">/// TmToDateTime
  293. /// Converts a C tm struct to a DateTime.
  294. </span>void TmToDateTime(const tm& time, DateTime& dateTime);</pre>
  295. <blockquote></blockquote>
  296. <hr>
  297. <p>&nbsp;</p>
  298. <p>&nbsp;</p>
  299. <p>&nbsp;</p>
  300. <p>&nbsp;</p>
  301. <p>&nbsp;</p>
  302. <p>&nbsp;</p>
  303. <p>&nbsp;</p>
  304. <p> </p>
  305. </body></html>