datetime.h 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/datetime.h
  3. // Purpose: declarations of time/date related classes (wxDateTime,
  4. // wxTimeSpan)
  5. // Author: Vadim Zeitlin
  6. // Modified by:
  7. // Created: 10.02.99
  8. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_DATETIME_H
  12. #define _WX_DATETIME_H
  13. #include "wx/defs.h"
  14. #if wxUSE_DATETIME
  15. #ifdef __WXWINCE__
  16. #include "wx/msw/wince/time.h"
  17. #else
  18. #include <time.h>
  19. #endif // OS
  20. #include <limits.h> // for INT_MIN
  21. #include "wx/longlong.h"
  22. #include "wx/anystr.h"
  23. class WXDLLIMPEXP_FWD_BASE wxDateTime;
  24. class WXDLLIMPEXP_FWD_BASE wxTimeSpan;
  25. class WXDLLIMPEXP_FWD_BASE wxDateSpan;
  26. #ifdef __WINDOWS__
  27. struct _SYSTEMTIME;
  28. #endif
  29. #include "wx/dynarray.h"
  30. // not all c-runtimes are based on 1/1/1970 being (time_t) 0
  31. // set this to the corresponding value in seconds 1/1/1970 has on your
  32. // systems c-runtime
  33. #define WX_TIME_BASE_OFFSET 0
  34. /*
  35. * TODO
  36. *
  37. * + 1. Time zones with minutes (make TimeZone a class)
  38. * ? 2. getdate() function like under Solaris
  39. * + 3. text conversion for wxDateSpan
  40. * + 4. pluggable modules for the workdays calculations
  41. * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
  42. */
  43. /*
  44. The three (main) classes declared in this header represent:
  45. 1. An absolute moment in the time (wxDateTime)
  46. 2. A difference between two moments in the time, positive or negative
  47. (wxTimeSpan)
  48. 3. A logical difference between two dates expressed in
  49. years/months/weeks/days (wxDateSpan)
  50. The following arithmetic operations are permitted (all others are not):
  51. addition
  52. --------
  53. wxDateTime + wxTimeSpan = wxDateTime
  54. wxDateTime + wxDateSpan = wxDateTime
  55. wxTimeSpan + wxTimeSpan = wxTimeSpan
  56. wxDateSpan + wxDateSpan = wxDateSpan
  57. subtraction
  58. ------------
  59. wxDateTime - wxDateTime = wxTimeSpan
  60. wxDateTime - wxTimeSpan = wxDateTime
  61. wxDateTime - wxDateSpan = wxDateTime
  62. wxTimeSpan - wxTimeSpan = wxTimeSpan
  63. wxDateSpan - wxDateSpan = wxDateSpan
  64. multiplication
  65. --------------
  66. wxTimeSpan * number = wxTimeSpan
  67. number * wxTimeSpan = wxTimeSpan
  68. wxDateSpan * number = wxDateSpan
  69. number * wxDateSpan = wxDateSpan
  70. unitary minus
  71. -------------
  72. -wxTimeSpan = wxTimeSpan
  73. -wxDateSpan = wxDateSpan
  74. For each binary operation OP (+, -, *) we have the following operatorOP=() as
  75. a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
  76. as a synonym for it and another const method with the same name which returns
  77. the changed copy of the object and operatorOP() as a global function which is
  78. implemented in terms of the const version of OPEN. For the unary - we have
  79. operator-() as a method, Neg() as synonym for it and Negate() which returns
  80. the copy of the object with the changed sign.
  81. */
  82. // an invalid/default date time object which may be used as the default
  83. // argument for arguments of type wxDateTime; it is also returned by all
  84. // functions returning wxDateTime on failure (this is why it is also called
  85. // wxInvalidDateTime)
  86. class WXDLLIMPEXP_FWD_BASE wxDateTime;
  87. extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultDateTimeFormat[];
  88. extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultTimeSpanFormat[];
  89. extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime;
  90. #define wxInvalidDateTime wxDefaultDateTime
  91. // ----------------------------------------------------------------------------
  92. // conditional compilation
  93. // ----------------------------------------------------------------------------
  94. // if configure detected strftime(), we have it too
  95. #ifdef HAVE_STRFTIME
  96. #define wxHAS_STRFTIME
  97. // suppose everyone else has strftime except Win CE unless VC8 is used
  98. #elif !defined(__WXWINCE__) || defined(__VISUALC8__)
  99. #define wxHAS_STRFTIME
  100. #endif
  101. // ----------------------------------------------------------------------------
  102. // wxDateTime represents an absolute moment in the time
  103. // ----------------------------------------------------------------------------
  104. class WXDLLIMPEXP_BASE wxDateTime
  105. {
  106. public:
  107. // types
  108. // ------------------------------------------------------------------------
  109. // a small unsigned integer type for storing things like minutes,
  110. // seconds &c. It should be at least short (i.e. not char) to contain
  111. // the number of milliseconds - it may also be 'int' because there is
  112. // no size penalty associated with it in our code, we don't store any
  113. // data in this format
  114. typedef unsigned short wxDateTime_t;
  115. // constants
  116. // ------------------------------------------------------------------------
  117. // the timezones
  118. enum TZ
  119. {
  120. // the time in the current time zone
  121. Local,
  122. // zones from GMT (= Greenwich Mean Time): they're guaranteed to be
  123. // consequent numbers, so writing something like `GMT0 + offset' is
  124. // safe if abs(offset) <= 12
  125. // underscore stands for minus
  126. GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7,
  127. GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1,
  128. GMT0,
  129. GMT1, GMT2, GMT3, GMT4, GMT5, GMT6,
  130. GMT7, GMT8, GMT9, GMT10, GMT11, GMT12, GMT13,
  131. // Note that GMT12 and GMT_12 are not the same: there is a difference
  132. // of exactly one day between them
  133. // some symbolic names for TZ
  134. // Europe
  135. WET = GMT0, // Western Europe Time
  136. WEST = GMT1, // Western Europe Summer Time
  137. CET = GMT1, // Central Europe Time
  138. CEST = GMT2, // Central Europe Summer Time
  139. EET = GMT2, // Eastern Europe Time
  140. EEST = GMT3, // Eastern Europe Summer Time
  141. MSK = GMT3, // Moscow Time
  142. MSD = GMT4, // Moscow Summer Time
  143. // US and Canada
  144. AST = GMT_4, // Atlantic Standard Time
  145. ADT = GMT_3, // Atlantic Daylight Time
  146. EST = GMT_5, // Eastern Standard Time
  147. EDT = GMT_4, // Eastern Daylight Saving Time
  148. CST = GMT_6, // Central Standard Time
  149. CDT = GMT_5, // Central Daylight Saving Time
  150. MST = GMT_7, // Mountain Standard Time
  151. MDT = GMT_6, // Mountain Daylight Saving Time
  152. PST = GMT_8, // Pacific Standard Time
  153. PDT = GMT_7, // Pacific Daylight Saving Time
  154. HST = GMT_10, // Hawaiian Standard Time
  155. AKST = GMT_9, // Alaska Standard Time
  156. AKDT = GMT_8, // Alaska Daylight Saving Time
  157. // Australia
  158. A_WST = GMT8, // Western Standard Time
  159. A_CST = GMT13 + 1, // Central Standard Time (+9.5)
  160. A_EST = GMT10, // Eastern Standard Time
  161. A_ESST = GMT11, // Eastern Summer Time
  162. // New Zealand
  163. NZST = GMT12, // Standard Time
  164. NZDT = GMT13, // Daylight Saving Time
  165. // TODO add more symbolic timezone names here
  166. // Universal Coordinated Time = the new and politically correct name
  167. // for GMT
  168. UTC = GMT0
  169. };
  170. // the calendar systems we know about: notice that it's valid (for
  171. // this classes purpose anyhow) to work with any of these calendars
  172. // even with the dates before the historical appearance of the
  173. // calendar
  174. enum Calendar
  175. {
  176. Gregorian, // current calendar
  177. Julian // calendar in use since -45 until the 1582 (or later)
  178. // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
  179. };
  180. // the country parameter is used so far for calculating the start and
  181. // the end of DST period and for deciding whether the date is a work
  182. // day or not
  183. //
  184. // TODO move this to intl.h
  185. // Required for WinCE
  186. #ifdef USA
  187. #undef USA
  188. #endif
  189. enum Country
  190. {
  191. Country_Unknown, // no special information for this country
  192. Country_Default, // set the default country with SetCountry() method
  193. // or use the default country with any other
  194. // TODO add more countries (for this we must know about DST and/or
  195. // holidays for this country)
  196. // Western European countries: we assume that they all follow the same
  197. // DST rules (true or false?)
  198. Country_WesternEurope_Start,
  199. Country_EEC = Country_WesternEurope_Start,
  200. France,
  201. Germany,
  202. UK,
  203. Country_WesternEurope_End = UK,
  204. Russia,
  205. USA
  206. };
  207. // symbolic names for the months
  208. enum Month
  209. {
  210. Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
  211. };
  212. // symbolic names for the weekdays
  213. enum WeekDay
  214. {
  215. Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
  216. };
  217. // invalid value for the year
  218. enum Year
  219. {
  220. Inv_Year = SHRT_MIN // should hold in wxDateTime_t
  221. };
  222. // flags for GetWeekDayName and GetMonthName
  223. enum NameFlags
  224. {
  225. Name_Full = 0x01, // return full name
  226. Name_Abbr = 0x02 // return abbreviated name
  227. };
  228. // flags for GetWeekOfYear and GetWeekOfMonth
  229. enum WeekFlags
  230. {
  231. Default_First, // Sunday_First for US, Monday_First for the rest
  232. Monday_First, // week starts with a Monday
  233. Sunday_First // week starts with a Sunday
  234. };
  235. // helper classes
  236. // ------------------------------------------------------------------------
  237. // a class representing a time zone: basically, this is just an offset
  238. // (in seconds) from GMT
  239. class WXDLLIMPEXP_BASE TimeZone
  240. {
  241. public:
  242. TimeZone(TZ tz);
  243. // create time zone object with the given offset
  244. TimeZone(long offset = 0) { m_offset = offset; }
  245. static TimeZone Make(long offset)
  246. {
  247. TimeZone tz;
  248. tz.m_offset = offset;
  249. return tz;
  250. }
  251. long GetOffset() const { return m_offset; }
  252. private:
  253. // offset for this timezone from GMT in seconds
  254. long m_offset;
  255. };
  256. // standard struct tm is limited to the years from 1900 (because
  257. // tm_year field is the offset from 1900), so we use our own struct
  258. // instead to represent broken down time
  259. //
  260. // NB: this struct should always be kept normalized (i.e. mon should
  261. // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
  262. // instead of modifying the member fields directly!
  263. struct WXDLLIMPEXP_BASE Tm
  264. {
  265. wxDateTime_t msec, sec, min, hour,
  266. mday, // Day of the month in 1..31 range.
  267. yday; // Day of the year in 0..365 range.
  268. Month mon;
  269. int year;
  270. // default ctor inits the object to an invalid value
  271. Tm();
  272. // ctor from struct tm and the timezone
  273. Tm(const struct tm& tm, const TimeZone& tz);
  274. // check that the given date/time is valid (in Gregorian calendar)
  275. bool IsValid() const;
  276. // get the week day
  277. WeekDay GetWeekDay() // not const because wday may be changed
  278. {
  279. if ( wday == Inv_WeekDay )
  280. ComputeWeekDay();
  281. return (WeekDay)wday;
  282. }
  283. // add the given number of months to the date keeping it normalized
  284. void AddMonths(int monDiff);
  285. // add the given number of months to the date keeping it normalized
  286. void AddDays(int dayDiff);
  287. private:
  288. // compute the weekday from other fields
  289. void ComputeWeekDay();
  290. // the timezone we correspond to
  291. TimeZone m_tz;
  292. // This value can only be accessed via GetWeekDay() and not directly
  293. // because it's not always computed when creating this object and may
  294. // need to be calculated on demand.
  295. wxDateTime_t wday;
  296. };
  297. // static methods
  298. // ------------------------------------------------------------------------
  299. // set the current country
  300. static void SetCountry(Country country);
  301. // get the current country
  302. static Country GetCountry();
  303. // return true if the country is a West European one (in practice,
  304. // this means that the same DST rules as for EEC apply)
  305. static bool IsWestEuropeanCountry(Country country = Country_Default);
  306. // return the current year
  307. static int GetCurrentYear(Calendar cal = Gregorian);
  308. // convert the year as returned by wxDateTime::GetYear() to a year
  309. // suitable for BC/AD notation. The difference is that BC year 1
  310. // corresponds to the year 0 (while BC year 0 didn't exist) and AD
  311. // year N is just year N.
  312. static int ConvertYearToBC(int year);
  313. // return the current month
  314. static Month GetCurrentMonth(Calendar cal = Gregorian);
  315. // returns true if the given year is a leap year in the given calendar
  316. static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
  317. // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
  318. static int GetCentury(int year);
  319. // returns the number of days in this year (356 or 355 for Gregorian
  320. // calendar usually :-)
  321. static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
  322. // get the number of the days in the given month (default value for
  323. // the year means the current one)
  324. static wxDateTime_t GetNumberOfDays(Month month,
  325. int year = Inv_Year,
  326. Calendar cal = Gregorian);
  327. // get the full (default) or abbreviated month name in the current
  328. // locale, returns empty string on error
  329. static wxString GetMonthName(Month month,
  330. NameFlags flags = Name_Full);
  331. // get the standard English full (default) or abbreviated month name
  332. static wxString GetEnglishMonthName(Month month,
  333. NameFlags flags = Name_Full);
  334. // get the full (default) or abbreviated weekday name in the current
  335. // locale, returns empty string on error
  336. static wxString GetWeekDayName(WeekDay weekday,
  337. NameFlags flags = Name_Full);
  338. // get the standard English full (default) or abbreviated weekday name
  339. static wxString GetEnglishWeekDayName(WeekDay weekday,
  340. NameFlags flags = Name_Full);
  341. // get the AM and PM strings in the current locale (may be empty)
  342. static void GetAmPmStrings(wxString *am, wxString *pm);
  343. // return true if the given country uses DST for this year
  344. static bool IsDSTApplicable(int year = Inv_Year,
  345. Country country = Country_Default);
  346. // get the beginning of DST for this year, will return invalid object
  347. // if no DST applicable in this year. The default value of the
  348. // parameter means to take the current year.
  349. static wxDateTime GetBeginDST(int year = Inv_Year,
  350. Country country = Country_Default);
  351. // get the end of DST for this year, will return invalid object
  352. // if no DST applicable in this year. The default value of the
  353. // parameter means to take the current year.
  354. static wxDateTime GetEndDST(int year = Inv_Year,
  355. Country country = Country_Default);
  356. // return the wxDateTime object for the current time
  357. static inline wxDateTime Now();
  358. // return the wxDateTime object for the current time with millisecond
  359. // precision (if available on this platform)
  360. static wxDateTime UNow();
  361. // return the wxDateTime object for today midnight: i.e. as Now() but
  362. // with time set to 0
  363. static inline wxDateTime Today();
  364. // constructors: you should test whether the constructor succeeded with
  365. // IsValid() function. The values Inv_Month and Inv_Year for the
  366. // parameters mean take current month and/or year values.
  367. // ------------------------------------------------------------------------
  368. // default ctor does not initialize the object, use Set()!
  369. wxDateTime() { m_time = wxLongLong(wxINT32_MIN, 0); }
  370. // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
  371. #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
  372. // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
  373. inline wxDateTime(time_t timet);
  374. #endif
  375. // from broken down time/date (only for standard Unix range)
  376. inline wxDateTime(const struct tm& tm);
  377. // from broken down time/date (any range)
  378. inline wxDateTime(const Tm& tm);
  379. // from JDN (beware of rounding errors)
  380. inline wxDateTime(double jdn);
  381. // from separate values for each component, date set to today
  382. inline wxDateTime(wxDateTime_t hour,
  383. wxDateTime_t minute = 0,
  384. wxDateTime_t second = 0,
  385. wxDateTime_t millisec = 0);
  386. // from separate values for each component with explicit date
  387. inline wxDateTime(wxDateTime_t day, // day of the month
  388. Month month,
  389. int year = Inv_Year, // 1999, not 99 please!
  390. wxDateTime_t hour = 0,
  391. wxDateTime_t minute = 0,
  392. wxDateTime_t second = 0,
  393. wxDateTime_t millisec = 0);
  394. #ifdef __WINDOWS__
  395. wxDateTime(const struct _SYSTEMTIME& st)
  396. {
  397. SetFromMSWSysTime(st);
  398. }
  399. #endif
  400. // default copy ctor ok
  401. // no dtor
  402. // assignment operators and Set() functions: all non const methods return
  403. // the reference to this object. IsValid() should be used to test whether
  404. // the function succeeded.
  405. // ------------------------------------------------------------------------
  406. // set to the current time
  407. inline wxDateTime& SetToCurrent();
  408. #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
  409. // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
  410. // set to given time_t value
  411. inline wxDateTime& Set(time_t timet);
  412. #endif
  413. // set to given broken down time/date
  414. wxDateTime& Set(const struct tm& tm);
  415. // set to given broken down time/date
  416. inline wxDateTime& Set(const Tm& tm);
  417. // set to given JDN (beware of rounding errors)
  418. wxDateTime& Set(double jdn);
  419. // set to given time, date = today
  420. wxDateTime& Set(wxDateTime_t hour,
  421. wxDateTime_t minute = 0,
  422. wxDateTime_t second = 0,
  423. wxDateTime_t millisec = 0);
  424. // from separate values for each component with explicit date
  425. // (defaults for month and year are the current values)
  426. wxDateTime& Set(wxDateTime_t day,
  427. Month month,
  428. int year = Inv_Year, // 1999, not 99 please!
  429. wxDateTime_t hour = 0,
  430. wxDateTime_t minute = 0,
  431. wxDateTime_t second = 0,
  432. wxDateTime_t millisec = 0);
  433. // resets time to 00:00:00, doesn't change the date
  434. wxDateTime& ResetTime();
  435. // get the date part of this object only, i.e. the object which has the
  436. // same date as this one but time of 00:00:00
  437. wxDateTime GetDateOnly() const;
  438. // the following functions don't change the values of the other
  439. // fields, i.e. SetMinute() won't change either hour or seconds value
  440. // set the year
  441. wxDateTime& SetYear(int year);
  442. // set the month
  443. wxDateTime& SetMonth(Month month);
  444. // set the day of the month
  445. wxDateTime& SetDay(wxDateTime_t day);
  446. // set hour
  447. wxDateTime& SetHour(wxDateTime_t hour);
  448. // set minute
  449. wxDateTime& SetMinute(wxDateTime_t minute);
  450. // set second
  451. wxDateTime& SetSecond(wxDateTime_t second);
  452. // set millisecond
  453. wxDateTime& SetMillisecond(wxDateTime_t millisecond);
  454. // assignment operator from time_t
  455. wxDateTime& operator=(time_t timet) { return Set(timet); }
  456. // assignment operator from broken down time/date
  457. wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
  458. // assignment operator from broken down time/date
  459. wxDateTime& operator=(const Tm& tm) { return Set(tm); }
  460. // default assignment operator is ok
  461. // calendar calculations (functions which set the date only leave the time
  462. // unchanged, e.g. don't explicitly zero it): SetXXX() functions modify the
  463. // object itself, GetXXX() ones return a new object.
  464. // ------------------------------------------------------------------------
  465. // set to the given week day in the same week as this one
  466. wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday,
  467. WeekFlags flags = Monday_First);
  468. inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday,
  469. WeekFlags flags = Monday_First) const;
  470. // set to the next week day following this one
  471. wxDateTime& SetToNextWeekDay(WeekDay weekday);
  472. inline wxDateTime GetNextWeekDay(WeekDay weekday) const;
  473. // set to the previous week day before this one
  474. wxDateTime& SetToPrevWeekDay(WeekDay weekday);
  475. inline wxDateTime GetPrevWeekDay(WeekDay weekday) const;
  476. // set to Nth occurrence of given weekday in the given month of the
  477. // given year (time is set to 0), return true on success and false on
  478. // failure. n may be positive (1..5) or negative to count from the end
  479. // of the month (see helper function SetToLastWeekDay())
  480. bool SetToWeekDay(WeekDay weekday,
  481. int n = 1,
  482. Month month = Inv_Month,
  483. int year = Inv_Year);
  484. inline wxDateTime GetWeekDay(WeekDay weekday,
  485. int n = 1,
  486. Month month = Inv_Month,
  487. int year = Inv_Year) const;
  488. // sets to the last weekday in the given month, year
  489. inline bool SetToLastWeekDay(WeekDay weekday,
  490. Month month = Inv_Month,
  491. int year = Inv_Year);
  492. inline wxDateTime GetLastWeekDay(WeekDay weekday,
  493. Month month = Inv_Month,
  494. int year = Inv_Year);
  495. #if WXWIN_COMPATIBILITY_2_6
  496. // sets the date to the given day of the given week in the year,
  497. // returns true on success and false if given date doesn't exist (e.g.
  498. // numWeek is > 53)
  499. //
  500. // these functions are badly defined as they're not the reverse of
  501. // GetWeekOfYear(), use SetToTheWeekOfYear() instead
  502. wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek,
  503. WeekDay weekday = Mon,
  504. WeekFlags flags = Monday_First) );
  505. wxDEPRECATED( wxDateTime GetWeek(wxDateTime_t numWeek,
  506. WeekDay weekday = Mon,
  507. WeekFlags flags = Monday_First) const );
  508. #endif // WXWIN_COMPATIBILITY_2_6
  509. // returns the date corresponding to the given week day of the given
  510. // week (in ISO notation) of the specified year
  511. static wxDateTime SetToWeekOfYear(int year,
  512. wxDateTime_t numWeek,
  513. WeekDay weekday = Mon);
  514. // sets the date to the last day of the given (or current) month or the
  515. // given (or current) year
  516. wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
  517. int year = Inv_Year);
  518. inline wxDateTime GetLastMonthDay(Month month = Inv_Month,
  519. int year = Inv_Year) const;
  520. // sets to the given year day (1..365 or 366)
  521. wxDateTime& SetToYearDay(wxDateTime_t yday);
  522. inline wxDateTime GetYearDay(wxDateTime_t yday) const;
  523. // The definitions below were taken verbatim from
  524. //
  525. // http://www.capecod.net/~pbaum/date/date0.htm
  526. //
  527. // (Peter Baum's home page)
  528. //
  529. // definition: The Julian Day Number, Julian Day, or JD of a
  530. // particular instant of time is the number of days and fractions of a
  531. // day since 12 hours Universal Time (Greenwich mean noon) on January
  532. // 1 of the year -4712, where the year is given in the Julian
  533. // proleptic calendar. The idea of using this reference date was
  534. // originally proposed by Joseph Scalizer in 1582 to count years but
  535. // it was modified by 19th century astronomers to count days. One
  536. // could have equivalently defined the reference time to be noon of
  537. // November 24, -4713 if were understood that Gregorian calendar rules
  538. // were applied. Julian days are Julian Day Numbers and are not to be
  539. // confused with Julian dates.
  540. //
  541. // definition: The Rata Die number is a date specified as the number
  542. // of days relative to a base date of December 31 of the year 0. Thus
  543. // January 1 of the year 1 is Rata Die day 1.
  544. // get the Julian Day number (the fractional part specifies the time of
  545. // the day, related to noon - beware of rounding errors!)
  546. double GetJulianDayNumber() const;
  547. double GetJDN() const { return GetJulianDayNumber(); }
  548. // get the Modified Julian Day number: it is equal to JDN - 2400000.5
  549. // and so integral MJDs correspond to the midnights (and not noons).
  550. // MJD 0 is Nov 17, 1858
  551. double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
  552. double GetMJD() const { return GetModifiedJulianDayNumber(); }
  553. // get the Rata Die number
  554. double GetRataDie() const;
  555. // TODO algorithms for calculating some important dates, such as
  556. // religious holidays (Easter...) or moon/solar eclipses? Some
  557. // algorithms can be found in the calendar FAQ
  558. // Timezone stuff: a wxDateTime object constructed using given
  559. // day/month/year/hour/min/sec values is interpreted as this moment in
  560. // local time. Using the functions below, it may be converted to another
  561. // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
  562. //
  563. // These functions try to handle DST internally, but there is no magical
  564. // way to know all rules for it in all countries in the world, so if the
  565. // program can handle it itself (or doesn't want to handle it at all for
  566. // whatever reason), the DST handling can be disabled with noDST.
  567. // ------------------------------------------------------------------------
  568. // transform to any given timezone
  569. inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = false) const;
  570. wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = false);
  571. // interpret current value as being in another timezone and transform
  572. // it to local one
  573. inline wxDateTime FromTimezone(const TimeZone& tz, bool noDST = false) const;
  574. wxDateTime& MakeFromTimezone(const TimeZone& tz, bool noDST = false);
  575. // transform to/from GMT/UTC
  576. wxDateTime ToUTC(bool noDST = false) const { return ToTimezone(UTC, noDST); }
  577. wxDateTime& MakeUTC(bool noDST = false) { return MakeTimezone(UTC, noDST); }
  578. wxDateTime ToGMT(bool noDST = false) const { return ToUTC(noDST); }
  579. wxDateTime& MakeGMT(bool noDST = false) { return MakeUTC(noDST); }
  580. wxDateTime FromUTC(bool noDST = false) const
  581. { return FromTimezone(UTC, noDST); }
  582. wxDateTime& MakeFromUTC(bool noDST = false)
  583. { return MakeFromTimezone(UTC, noDST); }
  584. // is daylight savings time in effect at this moment according to the
  585. // rules of the specified country?
  586. //
  587. // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
  588. // the information is not available (this is compatible with ANSI C)
  589. int IsDST(Country country = Country_Default) const;
  590. // accessors: many of them take the timezone parameter which indicates the
  591. // timezone for which to make the calculations and the default value means
  592. // to do it for the current timezone of this machine (even if the function
  593. // only operates with the date it's necessary because a date may wrap as
  594. // result of timezone shift)
  595. // ------------------------------------------------------------------------
  596. // is the date valid?
  597. inline bool IsValid() const { return m_time != wxInvalidDateTime.m_time; }
  598. // get the broken down date/time representation in the given timezone
  599. //
  600. // If you wish to get several time components (day, month and year),
  601. // consider getting the whole Tm strcuture first and retrieving the
  602. // value from it - this is much more efficient
  603. Tm GetTm(const TimeZone& tz = Local) const;
  604. // get the number of seconds since the Unix epoch - returns (time_t)-1
  605. // if the value is out of range
  606. inline time_t GetTicks() const;
  607. // get the century, same as GetCentury(GetYear())
  608. int GetCentury(const TimeZone& tz = Local) const
  609. { return GetCentury(GetYear(tz)); }
  610. // get the year (returns Inv_Year if date is invalid)
  611. int GetYear(const TimeZone& tz = Local) const
  612. { return GetTm(tz).year; }
  613. // get the month (Inv_Month if date is invalid)
  614. Month GetMonth(const TimeZone& tz = Local) const
  615. { return (Month)GetTm(tz).mon; }
  616. // get the month day (in 1..31 range, 0 if date is invalid)
  617. wxDateTime_t GetDay(const TimeZone& tz = Local) const
  618. { return GetTm(tz).mday; }
  619. // get the day of the week (Inv_WeekDay if date is invalid)
  620. WeekDay GetWeekDay(const TimeZone& tz = Local) const
  621. { return GetTm(tz).GetWeekDay(); }
  622. // get the hour of the day
  623. wxDateTime_t GetHour(const TimeZone& tz = Local) const
  624. { return GetTm(tz).hour; }
  625. // get the minute
  626. wxDateTime_t GetMinute(const TimeZone& tz = Local) const
  627. { return GetTm(tz).min; }
  628. // get the second
  629. wxDateTime_t GetSecond(const TimeZone& tz = Local) const
  630. { return GetTm(tz).sec; }
  631. // get milliseconds
  632. wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
  633. { return GetTm(tz).msec; }
  634. // get the day since the year start (1..366, 0 if date is invalid)
  635. wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
  636. // get the week number since the year start (1..52 or 53, 0 if date is
  637. // invalid)
  638. wxDateTime_t GetWeekOfYear(WeekFlags flags = Monday_First,
  639. const TimeZone& tz = Local) const;
  640. // get the week number since the month start (1..5, 0 if date is
  641. // invalid)
  642. wxDateTime_t GetWeekOfMonth(WeekFlags flags = Monday_First,
  643. const TimeZone& tz = Local) const;
  644. // is this date a work day? This depends on a country, of course,
  645. // because the holidays are different in different countries
  646. bool IsWorkDay(Country country = Country_Default) const;
  647. // dos date and time format
  648. // ------------------------------------------------------------------------
  649. // set from the DOS packed format
  650. wxDateTime& SetFromDOS(unsigned long ddt);
  651. // pack the date in DOS format
  652. unsigned long GetAsDOS() const;
  653. // SYSTEMTIME format
  654. // ------------------------------------------------------------------------
  655. #ifdef __WINDOWS__
  656. // convert SYSTEMTIME to wxDateTime
  657. wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME& st);
  658. // convert wxDateTime to SYSTEMTIME
  659. void GetAsMSWSysTime(struct _SYSTEMTIME* st) const;
  660. // same as above but only take date part into account, time is always zero
  661. wxDateTime& SetFromMSWSysDate(const struct _SYSTEMTIME& st);
  662. void GetAsMSWSysDate(struct _SYSTEMTIME* st) const;
  663. #endif // __WINDOWS__
  664. // comparison (see also functions below for operator versions)
  665. // ------------------------------------------------------------------------
  666. // returns true if the two moments are strictly identical
  667. inline bool IsEqualTo(const wxDateTime& datetime) const;
  668. // returns true if the date is strictly earlier than the given one
  669. inline bool IsEarlierThan(const wxDateTime& datetime) const;
  670. // returns true if the date is strictly later than the given one
  671. inline bool IsLaterThan(const wxDateTime& datetime) const;
  672. // returns true if the date is strictly in the given range
  673. inline bool IsStrictlyBetween(const wxDateTime& t1,
  674. const wxDateTime& t2) const;
  675. // returns true if the date is in the given range
  676. inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
  677. // do these two objects refer to the same date?
  678. inline bool IsSameDate(const wxDateTime& dt) const;
  679. // do these two objects have the same time?
  680. inline bool IsSameTime(const wxDateTime& dt) const;
  681. // are these two objects equal up to given timespan?
  682. inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
  683. inline bool operator<(const wxDateTime& dt) const
  684. {
  685. wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") );
  686. return GetValue() < dt.GetValue();
  687. }
  688. inline bool operator<=(const wxDateTime& dt) const
  689. {
  690. wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") );
  691. return GetValue() <= dt.GetValue();
  692. }
  693. inline bool operator>(const wxDateTime& dt) const
  694. {
  695. wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") );
  696. return GetValue() > dt.GetValue();
  697. }
  698. inline bool operator>=(const wxDateTime& dt) const
  699. {
  700. wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") );
  701. return GetValue() >= dt.GetValue();
  702. }
  703. inline bool operator==(const wxDateTime& dt) const
  704. {
  705. wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") );
  706. return GetValue() == dt.GetValue();
  707. }
  708. inline bool operator!=(const wxDateTime& dt) const
  709. {
  710. wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") );
  711. return GetValue() != dt.GetValue();
  712. }
  713. // arithmetics with dates (see also below for more operators)
  714. // ------------------------------------------------------------------------
  715. // return the sum of the date with a time span (positive or negative)
  716. inline wxDateTime Add(const wxTimeSpan& diff) const;
  717. // add a time span (positive or negative)
  718. inline wxDateTime& Add(const wxTimeSpan& diff);
  719. // add a time span (positive or negative)
  720. inline wxDateTime& operator+=(const wxTimeSpan& diff);
  721. inline wxDateTime operator+(const wxTimeSpan& ts) const
  722. {
  723. wxDateTime dt(*this);
  724. dt.Add(ts);
  725. return dt;
  726. }
  727. // return the difference of the date with a time span
  728. inline wxDateTime Subtract(const wxTimeSpan& diff) const;
  729. // subtract a time span (positive or negative)
  730. inline wxDateTime& Subtract(const wxTimeSpan& diff);
  731. // subtract a time span (positive or negative)
  732. inline wxDateTime& operator-=(const wxTimeSpan& diff);
  733. inline wxDateTime operator-(const wxTimeSpan& ts) const
  734. {
  735. wxDateTime dt(*this);
  736. dt.Subtract(ts);
  737. return dt;
  738. }
  739. // return the sum of the date with a date span
  740. inline wxDateTime Add(const wxDateSpan& diff) const;
  741. // add a date span (positive or negative)
  742. wxDateTime& Add(const wxDateSpan& diff);
  743. // add a date span (positive or negative)
  744. inline wxDateTime& operator+=(const wxDateSpan& diff);
  745. inline wxDateTime operator+(const wxDateSpan& ds) const
  746. {
  747. wxDateTime dt(*this);
  748. dt.Add(ds);
  749. return dt;
  750. }
  751. // return the difference of the date with a date span
  752. inline wxDateTime Subtract(const wxDateSpan& diff) const;
  753. // subtract a date span (positive or negative)
  754. inline wxDateTime& Subtract(const wxDateSpan& diff);
  755. // subtract a date span (positive or negative)
  756. inline wxDateTime& operator-=(const wxDateSpan& diff);
  757. inline wxDateTime operator-(const wxDateSpan& ds) const
  758. {
  759. wxDateTime dt(*this);
  760. dt.Subtract(ds);
  761. return dt;
  762. }
  763. // return the difference between two dates
  764. inline wxTimeSpan Subtract(const wxDateTime& dt) const;
  765. inline wxTimeSpan operator-(const wxDateTime& dt2) const;
  766. wxDateSpan DiffAsDateSpan(const wxDateTime& dt) const;
  767. // conversion to/from text
  768. // ------------------------------------------------------------------------
  769. // all conversions functions return true to indicate whether parsing
  770. // succeeded or failed and fill in the provided end iterator, which must
  771. // not be NULL, with the location of the character where the parsing
  772. // stopped (this will be end() of the passed string if everything was
  773. // parsed)
  774. // parse a string in RFC 822 format (found e.g. in mail headers and
  775. // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
  776. bool ParseRfc822Date(const wxString& date,
  777. wxString::const_iterator *end);
  778. // parse a date/time in the given format (see strptime(3)), fill in
  779. // the missing (in the string) fields with the values of dateDef (by
  780. // default, they will not change if they had valid values or will
  781. // default to Today() otherwise)
  782. bool ParseFormat(const wxString& date,
  783. const wxString& format,
  784. const wxDateTime& dateDef,
  785. wxString::const_iterator *end);
  786. bool ParseFormat(const wxString& date,
  787. const wxString& format,
  788. wxString::const_iterator *end)
  789. {
  790. return ParseFormat(date, format, wxDefaultDateTime, end);
  791. }
  792. bool ParseFormat(const wxString& date,
  793. wxString::const_iterator *end)
  794. {
  795. return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end);
  796. }
  797. // parse a string containing date, time or both in ISO 8601 format
  798. //
  799. // notice that these functions are new in wx 3.0 and so we don't
  800. // provide compatibility overloads for them
  801. bool ParseISODate(const wxString& date)
  802. {
  803. wxString::const_iterator end;
  804. return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end();
  805. }
  806. bool ParseISOTime(const wxString& time)
  807. {
  808. wxString::const_iterator end;
  809. return ParseFormat(time, wxS("%H:%M:%S"), &end) && end == time.end();
  810. }
  811. bool ParseISOCombined(const wxString& datetime, char sep = 'T')
  812. {
  813. wxString::const_iterator end;
  814. const wxString fmt = wxS("%Y-%m-%d") + wxString(sep) + wxS("%H:%M:%S");
  815. return ParseFormat(datetime, fmt, &end) && end == datetime.end();
  816. }
  817. // parse a string containing the date/time in "free" format, this
  818. // function will try to make an educated guess at the string contents
  819. bool ParseDateTime(const wxString& datetime,
  820. wxString::const_iterator *end);
  821. // parse a string containing the date only in "free" format (less
  822. // flexible than ParseDateTime)
  823. bool ParseDate(const wxString& date,
  824. wxString::const_iterator *end);
  825. // parse a string containing the time only in "free" format
  826. bool ParseTime(const wxString& time,
  827. wxString::const_iterator *end);
  828. // this function accepts strftime()-like format string (default
  829. // argument corresponds to the preferred date and time representation
  830. // for the current locale) and returns the string containing the
  831. // resulting text representation
  832. wxString Format(const wxString& format = wxDefaultDateTimeFormat,
  833. const TimeZone& tz = Local) const;
  834. // preferred date representation for the current locale
  835. wxString FormatDate() const { return Format(wxS("%x")); }
  836. // preferred time representation for the current locale
  837. wxString FormatTime() const { return Format(wxS("%X")); }
  838. // returns the string representing the date in ISO 8601 format
  839. // (YYYY-MM-DD)
  840. wxString FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
  841. // returns the string representing the time in ISO 8601 format
  842. // (HH:MM:SS)
  843. wxString FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
  844. // return the combined date time representation in ISO 8601 format; the
  845. // separator character should be 'T' according to the standard but it
  846. // can also be useful to set it to ' '
  847. wxString FormatISOCombined(char sep = 'T') const
  848. { return FormatISODate() + sep + FormatISOTime(); }
  849. // backwards compatible versions of the parsing functions: they return an
  850. // object representing the next character following the date specification
  851. // (i.e. the one where the scan had to stop) or a special NULL-like object
  852. // on failure
  853. //
  854. // they're not deprecated because a lot of existing code uses them and
  855. // there is no particular harm in keeping them but you should still prefer
  856. // the versions above in the new code
  857. wxAnyStrPtr ParseRfc822Date(const wxString& date)
  858. {
  859. wxString::const_iterator end;
  860. return ParseRfc822Date(date, &end) ? wxAnyStrPtr(date, end)
  861. : wxAnyStrPtr();
  862. }
  863. wxAnyStrPtr ParseFormat(const wxString& date,
  864. const wxString& format = wxDefaultDateTimeFormat,
  865. const wxDateTime& dateDef = wxDefaultDateTime)
  866. {
  867. wxString::const_iterator end;
  868. return ParseFormat(date, format, dateDef, &end) ? wxAnyStrPtr(date, end)
  869. : wxAnyStrPtr();
  870. }
  871. wxAnyStrPtr ParseDateTime(const wxString& datetime)
  872. {
  873. wxString::const_iterator end;
  874. return ParseDateTime(datetime, &end) ? wxAnyStrPtr(datetime, end)
  875. : wxAnyStrPtr();
  876. }
  877. wxAnyStrPtr ParseDate(const wxString& date)
  878. {
  879. wxString::const_iterator end;
  880. return ParseDate(date, &end) ? wxAnyStrPtr(date, end)
  881. : wxAnyStrPtr();
  882. }
  883. wxAnyStrPtr ParseTime(const wxString& time)
  884. {
  885. wxString::const_iterator end;
  886. return ParseTime(time, &end) ? wxAnyStrPtr(time, end)
  887. : wxAnyStrPtr();
  888. }
  889. // In addition to wxAnyStrPtr versions above we also must provide the
  890. // overloads for C strings as we must return a pointer into the original
  891. // string and not inside a temporary wxString which would have been created
  892. // if the overloads above were used.
  893. //
  894. // And then we also have to provide the overloads for wxCStrData, as usual.
  895. // Unfortunately those ones can't return anything as we don't have any
  896. // sufficiently long-lived wxAnyStrPtr to return from them: any temporary
  897. // strings it would point to would be destroyed when this function returns
  898. // making it impossible to dereference the return value. So we just don't
  899. // return anything from here which at least allows to keep compatibility
  900. // with the code not testing the return value. Other uses of this method
  901. // need to be converted to use one of the new bool-returning overloads
  902. // above.
  903. void ParseRfc822Date(const wxCStrData& date)
  904. { ParseRfc822Date(wxString(date)); }
  905. const char* ParseRfc822Date(const char* date);
  906. const wchar_t* ParseRfc822Date(const wchar_t* date);
  907. void ParseFormat(const wxCStrData& date,
  908. const wxString& format = wxDefaultDateTimeFormat,
  909. const wxDateTime& dateDef = wxDefaultDateTime)
  910. { ParseFormat(wxString(date), format, dateDef); }
  911. const char* ParseFormat(const char* date,
  912. const wxString& format = wxDefaultDateTimeFormat,
  913. const wxDateTime& dateDef = wxDefaultDateTime);
  914. const wchar_t* ParseFormat(const wchar_t* date,
  915. const wxString& format = wxDefaultDateTimeFormat,
  916. const wxDateTime& dateDef = wxDefaultDateTime);
  917. void ParseDateTime(const wxCStrData& datetime)
  918. { ParseDateTime(wxString(datetime)); }
  919. const char* ParseDateTime(const char* datetime);
  920. const wchar_t* ParseDateTime(const wchar_t* datetime);
  921. void ParseDate(const wxCStrData& date)
  922. { ParseDate(wxString(date)); }
  923. const char* ParseDate(const char* date);
  924. const wchar_t* ParseDate(const wchar_t* date);
  925. void ParseTime(const wxCStrData& time)
  926. { ParseTime(wxString(time)); }
  927. const char* ParseTime(const char* time);
  928. const wchar_t* ParseTime(const wchar_t* time);
  929. // implementation
  930. // ------------------------------------------------------------------------
  931. // construct from internal representation
  932. wxDateTime(const wxLongLong& time) { m_time = time; }
  933. // get the internal representation
  934. inline wxLongLong GetValue() const;
  935. // a helper function to get the current time_t
  936. static time_t GetTimeNow() { return time(NULL); }
  937. // another one to get the current time broken down
  938. static struct tm *GetTmNow()
  939. {
  940. static struct tm l_CurrentTime;
  941. return GetTmNow(&l_CurrentTime);
  942. }
  943. // get current time using thread-safe function
  944. static struct tm *GetTmNow(struct tm *tmstruct);
  945. private:
  946. // the current country - as it's the same for all program objects (unless
  947. // it runs on a _really_ big cluster system :-), this is a static member:
  948. // see SetCountry() and GetCountry()
  949. static Country ms_country;
  950. // this constant is used to transform a time_t value to the internal
  951. // representation, as time_t is in seconds and we use milliseconds it's
  952. // fixed to 1000
  953. static const long TIME_T_FACTOR;
  954. // returns true if we fall in range in which we can use standard ANSI C
  955. // functions
  956. inline bool IsInStdRange() const;
  957. // the internal representation of the time is the amount of milliseconds
  958. // elapsed since the origin which is set by convention to the UNIX/C epoch
  959. // value: the midnight of January 1, 1970 (UTC)
  960. wxLongLong m_time;
  961. };
  962. // ----------------------------------------------------------------------------
  963. // This class contains a difference between 2 wxDateTime values, so it makes
  964. // sense to add it to wxDateTime and it is the result of subtraction of 2
  965. // objects of that class. See also wxDateSpan.
  966. // ----------------------------------------------------------------------------
  967. class WXDLLIMPEXP_BASE wxTimeSpan
  968. {
  969. public:
  970. // constructors
  971. // ------------------------------------------------------------------------
  972. // return the timespan for the given number of milliseconds
  973. static wxTimeSpan Milliseconds(wxLongLong ms) { return wxTimeSpan(0, 0, 0, ms); }
  974. static wxTimeSpan Millisecond() { return Milliseconds(1); }
  975. // return the timespan for the given number of seconds
  976. static wxTimeSpan Seconds(wxLongLong sec) { return wxTimeSpan(0, 0, sec); }
  977. static wxTimeSpan Second() { return Seconds(1); }
  978. // return the timespan for the given number of minutes
  979. static wxTimeSpan Minutes(long min) { return wxTimeSpan(0, min, 0 ); }
  980. static wxTimeSpan Minute() { return Minutes(1); }
  981. // return the timespan for the given number of hours
  982. static wxTimeSpan Hours(long hours) { return wxTimeSpan(hours, 0, 0); }
  983. static wxTimeSpan Hour() { return Hours(1); }
  984. // return the timespan for the given number of days
  985. static wxTimeSpan Days(long days) { return Hours(24 * days); }
  986. static wxTimeSpan Day() { return Days(1); }
  987. // return the timespan for the given number of weeks
  988. static wxTimeSpan Weeks(long days) { return Days(7 * days); }
  989. static wxTimeSpan Week() { return Weeks(1); }
  990. // default ctor constructs the 0 time span
  991. wxTimeSpan() { }
  992. // from separate values for each component, date set to 0 (hours are
  993. // not restricted to 0..24 range, neither are minutes, seconds or
  994. // milliseconds)
  995. inline wxTimeSpan(long hours,
  996. long minutes = 0,
  997. wxLongLong seconds = 0,
  998. wxLongLong milliseconds = 0);
  999. // default copy ctor is ok
  1000. // no dtor
  1001. // arithmetics with time spans (see also below for more operators)
  1002. // ------------------------------------------------------------------------
  1003. // return the sum of two timespans
  1004. inline wxTimeSpan Add(const wxTimeSpan& diff) const;
  1005. // add two timespans together
  1006. inline wxTimeSpan& Add(const wxTimeSpan& diff);
  1007. // add two timespans together
  1008. wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
  1009. inline wxTimeSpan operator+(const wxTimeSpan& ts) const
  1010. {
  1011. return wxTimeSpan(GetValue() + ts.GetValue());
  1012. }
  1013. // return the difference of two timespans
  1014. inline wxTimeSpan Subtract(const wxTimeSpan& diff) const;
  1015. // subtract another timespan
  1016. inline wxTimeSpan& Subtract(const wxTimeSpan& diff);
  1017. // subtract another timespan
  1018. wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); }
  1019. inline wxTimeSpan operator-(const wxTimeSpan& ts)
  1020. {
  1021. return wxTimeSpan(GetValue() - ts.GetValue());
  1022. }
  1023. // multiply timespan by a scalar
  1024. inline wxTimeSpan Multiply(int n) const;
  1025. // multiply timespan by a scalar
  1026. inline wxTimeSpan& Multiply(int n);
  1027. // multiply timespan by a scalar
  1028. wxTimeSpan& operator*=(int n) { return Multiply(n); }
  1029. inline wxTimeSpan operator*(int n) const
  1030. {
  1031. return wxTimeSpan(*this).Multiply(n);
  1032. }
  1033. // return this timespan with opposite sign
  1034. wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
  1035. // negate the value of the timespan
  1036. wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
  1037. // negate the value of the timespan
  1038. wxTimeSpan& operator-() { return Neg(); }
  1039. // return the absolute value of the timespan: does _not_ modify the
  1040. // object
  1041. inline wxTimeSpan Abs() const;
  1042. // there is intentionally no division because we don't want to
  1043. // introduce rounding errors in time calculations
  1044. // comparaison (see also operator versions below)
  1045. // ------------------------------------------------------------------------
  1046. // is the timespan null?
  1047. bool IsNull() const { return m_diff == 0l; }
  1048. // returns true if the timespan is null
  1049. bool operator!() const { return !IsNull(); }
  1050. // is the timespan positive?
  1051. bool IsPositive() const { return m_diff > 0l; }
  1052. // is the timespan negative?
  1053. bool IsNegative() const { return m_diff < 0l; }
  1054. // are two timespans equal?
  1055. inline bool IsEqualTo(const wxTimeSpan& ts) const;
  1056. // compare two timestamps: works with the absolute values, i.e. -2
  1057. // hours is longer than 1 hour. Also, it will return false if the
  1058. // timespans are equal in absolute value.
  1059. inline bool IsLongerThan(const wxTimeSpan& ts) const;
  1060. // compare two timestamps: works with the absolute values, i.e. 1
  1061. // hour is shorter than -2 hours. Also, it will return false if the
  1062. // timespans are equal in absolute value.
  1063. bool IsShorterThan(const wxTimeSpan& t) const;
  1064. inline bool operator<(const wxTimeSpan &ts) const
  1065. {
  1066. return GetValue() < ts.GetValue();
  1067. }
  1068. inline bool operator<=(const wxTimeSpan &ts) const
  1069. {
  1070. return GetValue() <= ts.GetValue();
  1071. }
  1072. inline bool operator>(const wxTimeSpan &ts) const
  1073. {
  1074. return GetValue() > ts.GetValue();
  1075. }
  1076. inline bool operator>=(const wxTimeSpan &ts) const
  1077. {
  1078. return GetValue() >= ts.GetValue();
  1079. }
  1080. inline bool operator==(const wxTimeSpan &ts) const
  1081. {
  1082. return GetValue() == ts.GetValue();
  1083. }
  1084. inline bool operator!=(const wxTimeSpan &ts) const
  1085. {
  1086. return GetValue() != ts.GetValue();
  1087. }
  1088. // breaking into days, hours, minutes and seconds
  1089. // ------------------------------------------------------------------------
  1090. // get the max number of weeks in this timespan
  1091. inline int GetWeeks() const;
  1092. // get the max number of days in this timespan
  1093. inline int GetDays() const;
  1094. // get the max number of hours in this timespan
  1095. inline int GetHours() const;
  1096. // get the max number of minutes in this timespan
  1097. inline int GetMinutes() const;
  1098. // get the max number of seconds in this timespan
  1099. inline wxLongLong GetSeconds() const;
  1100. // get the number of milliseconds in this timespan
  1101. wxLongLong GetMilliseconds() const { return m_diff; }
  1102. // conversion to text
  1103. // ------------------------------------------------------------------------
  1104. // this function accepts strftime()-like format string (default
  1105. // argument corresponds to the preferred date and time representation
  1106. // for the current locale) and returns the string containing the
  1107. // resulting text representation. Notice that only some of format
  1108. // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
  1109. // minutes and seconds make sense, but not "PM/AM" string for example.
  1110. wxString Format(const wxString& format = wxDefaultTimeSpanFormat) const;
  1111. // implementation
  1112. // ------------------------------------------------------------------------
  1113. // construct from internal representation
  1114. wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
  1115. // get the internal representation
  1116. wxLongLong GetValue() const { return m_diff; }
  1117. private:
  1118. // the (signed) time span in milliseconds
  1119. wxLongLong m_diff;
  1120. };
  1121. // ----------------------------------------------------------------------------
  1122. // This class is a "logical time span" and is useful for implementing program
  1123. // logic for such things as "add one month to the date" which, in general,
  1124. // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
  1125. // the next month (to understand that this is indeed different consider adding
  1126. // one month to Feb, 15 - we want to get Mar, 15, of course).
  1127. //
  1128. // When adding a month to the date, all lesser components (days, hours, ...)
  1129. // won't be changed unless the resulting date would be invalid: for example,
  1130. // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
  1131. //
  1132. // Because of this feature, adding and subtracting back again the same
  1133. // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
  1134. // month will be Jan 28, not Jan 31!
  1135. //
  1136. // wxDateSpan can be either positive or negative. They may be
  1137. // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
  1138. // month and 1 day) is 2 months and 2 days. They can be added together and
  1139. // with wxDateTime or wxTimeSpan, but the type of result is different for each
  1140. // case.
  1141. //
  1142. // Beware about weeks: if you specify both weeks and days, the total number of
  1143. // days added will be 7*weeks + days! See also GetTotalDays() function.
  1144. //
  1145. // Equality operators are defined for wxDateSpans. Two datespans are equal if
  1146. // they both give the same target date when added to *every* source date.
  1147. // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
  1148. // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
  1149. // equal to wxDateSpan::Weeks(2)
  1150. //
  1151. // Finally, notice that for adding hours, minutes &c you don't need this
  1152. // class: wxTimeSpan will do the job because there are no subtleties
  1153. // associated with those.
  1154. // ----------------------------------------------------------------------------
  1155. class WXDLLIMPEXP_BASE wxDateSpan
  1156. {
  1157. public:
  1158. // constructors
  1159. // ------------------------------------------------------------------------
  1160. // this many years/months/weeks/days
  1161. wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
  1162. {
  1163. m_years = years;
  1164. m_months = months;
  1165. m_weeks = weeks;
  1166. m_days = days;
  1167. }
  1168. // get an object for the given number of days
  1169. static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
  1170. static wxDateSpan Day() { return Days(1); }
  1171. // get an object for the given number of weeks
  1172. static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
  1173. static wxDateSpan Week() { return Weeks(1); }
  1174. // get an object for the given number of months
  1175. static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
  1176. static wxDateSpan Month() { return Months(1); }
  1177. // get an object for the given number of years
  1178. static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
  1179. static wxDateSpan Year() { return Years(1); }
  1180. // default copy ctor is ok
  1181. // no dtor
  1182. // accessors (all SetXXX() return the (modified) wxDateSpan object)
  1183. // ------------------------------------------------------------------------
  1184. // set number of years
  1185. wxDateSpan& SetYears(int n) { m_years = n; return *this; }
  1186. // set number of months
  1187. wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
  1188. // set number of weeks
  1189. wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
  1190. // set number of days
  1191. wxDateSpan& SetDays(int n) { m_days = n; return *this; }
  1192. // get number of years
  1193. int GetYears() const { return m_years; }
  1194. // get number of months
  1195. int GetMonths() const { return m_months; }
  1196. // returns 12*GetYears() + GetMonths()
  1197. int GetTotalMonths() const { return 12*m_years + m_months; }
  1198. // get number of weeks
  1199. int GetWeeks() const { return m_weeks; }
  1200. // get number of days
  1201. int GetDays() const { return m_days; }
  1202. // returns 7*GetWeeks() + GetDays()
  1203. int GetTotalDays() const { return 7*m_weeks + m_days; }
  1204. // arithmetics with date spans (see also below for more operators)
  1205. // ------------------------------------------------------------------------
  1206. // return sum of two date spans
  1207. inline wxDateSpan Add(const wxDateSpan& other) const;
  1208. // add another wxDateSpan to us
  1209. inline wxDateSpan& Add(const wxDateSpan& other);
  1210. // add another wxDateSpan to us
  1211. inline wxDateSpan& operator+=(const wxDateSpan& other);
  1212. inline wxDateSpan operator+(const wxDateSpan& ds) const
  1213. {
  1214. return wxDateSpan(GetYears() + ds.GetYears(),
  1215. GetMonths() + ds.GetMonths(),
  1216. GetWeeks() + ds.GetWeeks(),
  1217. GetDays() + ds.GetDays());
  1218. }
  1219. // return difference of two date spans
  1220. inline wxDateSpan Subtract(const wxDateSpan& other) const;
  1221. // subtract another wxDateSpan from us
  1222. inline wxDateSpan& Subtract(const wxDateSpan& other);
  1223. // subtract another wxDateSpan from us
  1224. inline wxDateSpan& operator-=(const wxDateSpan& other);
  1225. inline wxDateSpan operator-(const wxDateSpan& ds) const
  1226. {
  1227. return wxDateSpan(GetYears() - ds.GetYears(),
  1228. GetMonths() - ds.GetMonths(),
  1229. GetWeeks() - ds.GetWeeks(),
  1230. GetDays() - ds.GetDays());
  1231. }
  1232. // return a copy of this time span with changed sign
  1233. inline wxDateSpan Negate() const;
  1234. // inverse the sign of this timespan
  1235. inline wxDateSpan& Neg();
  1236. // inverse the sign of this timespan
  1237. wxDateSpan& operator-() { return Neg(); }
  1238. // return the date span proportional to this one with given factor
  1239. inline wxDateSpan Multiply(int factor) const;
  1240. // multiply all components by a (signed) number
  1241. inline wxDateSpan& Multiply(int factor);
  1242. // multiply all components by a (signed) number
  1243. inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
  1244. inline wxDateSpan operator*(int n) const
  1245. {
  1246. return wxDateSpan(*this).Multiply(n);
  1247. }
  1248. // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
  1249. inline bool operator==(const wxDateSpan& ds) const
  1250. {
  1251. return GetYears() == ds.GetYears() &&
  1252. GetMonths() == ds.GetMonths() &&
  1253. GetTotalDays() == ds.GetTotalDays();
  1254. }
  1255. inline bool operator!=(const wxDateSpan& ds) const
  1256. {
  1257. return !(*this == ds);
  1258. }
  1259. private:
  1260. int m_years,
  1261. m_months,
  1262. m_weeks,
  1263. m_days;
  1264. };
  1265. // ----------------------------------------------------------------------------
  1266. // wxDateTimeArray: array of dates.
  1267. // ----------------------------------------------------------------------------
  1268. WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE);
  1269. // ----------------------------------------------------------------------------
  1270. // wxDateTimeHolidayAuthority: an object of this class will decide whether a
  1271. // given date is a holiday and is used by all functions working with "work
  1272. // days".
  1273. //
  1274. // NB: the base class is an ABC, derived classes must implement the pure
  1275. // virtual methods to work with the holidays they correspond to.
  1276. // ----------------------------------------------------------------------------
  1277. class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority;
  1278. WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *,
  1279. wxHolidayAuthoritiesArray,
  1280. class WXDLLIMPEXP_BASE);
  1281. class wxDateTimeHolidaysModule;
  1282. class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
  1283. {
  1284. friend class wxDateTimeHolidaysModule;
  1285. public:
  1286. // returns true if the given date is a holiday
  1287. static bool IsHoliday(const wxDateTime& dt);
  1288. // fills the provided array with all holidays in the given range, returns
  1289. // the number of them
  1290. static size_t GetHolidaysInRange(const wxDateTime& dtStart,
  1291. const wxDateTime& dtEnd,
  1292. wxDateTimeArray& holidays);
  1293. // clear the list of holiday authorities
  1294. static void ClearAllAuthorities();
  1295. // add a new holiday authority (the pointer will be deleted by
  1296. // wxDateTimeHolidayAuthority)
  1297. static void AddAuthority(wxDateTimeHolidayAuthority *auth);
  1298. // the base class must have a virtual dtor
  1299. virtual ~wxDateTimeHolidayAuthority();
  1300. protected:
  1301. // this function is called to determine whether a given day is a holiday
  1302. virtual bool DoIsHoliday(const wxDateTime& dt) const = 0;
  1303. // this function should fill the array with all holidays between the two
  1304. // given dates - it is implemented in the base class, but in a very
  1305. // inefficient way (it just iterates over all days and uses IsHoliday() for
  1306. // each of them), so it must be overridden in the derived class where the
  1307. // base class version may be explicitly used if needed
  1308. //
  1309. // returns the number of holidays in the given range and fills holidays
  1310. // array
  1311. virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
  1312. const wxDateTime& dtEnd,
  1313. wxDateTimeArray& holidays) const = 0;
  1314. private:
  1315. // all holiday authorities
  1316. static wxHolidayAuthoritiesArray ms_authorities;
  1317. };
  1318. // the holidays for this class are all Saturdays and Sundays
  1319. class WXDLLIMPEXP_BASE wxDateTimeWorkDays : public wxDateTimeHolidayAuthority
  1320. {
  1321. protected:
  1322. virtual bool DoIsHoliday(const wxDateTime& dt) const;
  1323. virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
  1324. const wxDateTime& dtEnd,
  1325. wxDateTimeArray& holidays) const;
  1326. };
  1327. // ============================================================================
  1328. // inline functions implementation
  1329. // ============================================================================
  1330. // ----------------------------------------------------------------------------
  1331. // private macros
  1332. // ----------------------------------------------------------------------------
  1333. #define MILLISECONDS_PER_DAY 86400000l
  1334. // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
  1335. // using a temp variable always might prevent other compilers from optimising
  1336. // it away - hence use of this ugly macro
  1337. #ifndef __HPUX__
  1338. #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
  1339. #else
  1340. #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
  1341. #endif
  1342. // ----------------------------------------------------------------------------
  1343. // wxDateTime construction
  1344. // ----------------------------------------------------------------------------
  1345. inline bool wxDateTime::IsInStdRange() const
  1346. {
  1347. // currently we don't know what is the real type of time_t so prefer to err
  1348. // on the safe side and limit it to 32 bit values which is safe everywhere
  1349. return m_time >= 0l && (m_time / TIME_T_FACTOR) < wxINT32_MAX;
  1350. }
  1351. /* static */
  1352. inline wxDateTime wxDateTime::Now()
  1353. {
  1354. struct tm tmstruct;
  1355. return wxDateTime(*GetTmNow(&tmstruct));
  1356. }
  1357. /* static */
  1358. inline wxDateTime wxDateTime::Today()
  1359. {
  1360. wxDateTime dt(Now());
  1361. dt.ResetTime();
  1362. return dt;
  1363. }
  1364. #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
  1365. inline wxDateTime& wxDateTime::Set(time_t timet)
  1366. {
  1367. if ( timet == (time_t)-1 )
  1368. {
  1369. m_time = wxInvalidDateTime.m_time;
  1370. }
  1371. else
  1372. {
  1373. // assign first to avoid long multiplication overflow!
  1374. m_time = timet - WX_TIME_BASE_OFFSET;
  1375. m_time *= TIME_T_FACTOR;
  1376. }
  1377. return *this;
  1378. }
  1379. #endif
  1380. inline wxDateTime& wxDateTime::SetToCurrent()
  1381. {
  1382. *this = Now();
  1383. return *this;
  1384. }
  1385. #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
  1386. inline wxDateTime::wxDateTime(time_t timet)
  1387. {
  1388. Set(timet);
  1389. }
  1390. #endif
  1391. inline wxDateTime::wxDateTime(const struct tm& tm)
  1392. {
  1393. Set(tm);
  1394. }
  1395. inline wxDateTime::wxDateTime(const Tm& tm)
  1396. {
  1397. Set(tm);
  1398. }
  1399. inline wxDateTime::wxDateTime(double jdn)
  1400. {
  1401. Set(jdn);
  1402. }
  1403. inline wxDateTime& wxDateTime::Set(const Tm& tm)
  1404. {
  1405. wxASSERT_MSG( tm.IsValid(), wxT("invalid broken down date/time") );
  1406. return Set(tm.mday, (Month)tm.mon, tm.year,
  1407. tm.hour, tm.min, tm.sec, tm.msec);
  1408. }
  1409. inline wxDateTime::wxDateTime(wxDateTime_t hour,
  1410. wxDateTime_t minute,
  1411. wxDateTime_t second,
  1412. wxDateTime_t millisec)
  1413. {
  1414. Set(hour, minute, second, millisec);
  1415. }
  1416. inline wxDateTime::wxDateTime(wxDateTime_t day,
  1417. Month month,
  1418. int year,
  1419. wxDateTime_t hour,
  1420. wxDateTime_t minute,
  1421. wxDateTime_t second,
  1422. wxDateTime_t millisec)
  1423. {
  1424. Set(day, month, year, hour, minute, second, millisec);
  1425. }
  1426. // ----------------------------------------------------------------------------
  1427. // wxDateTime accessors
  1428. // ----------------------------------------------------------------------------
  1429. inline wxLongLong wxDateTime::GetValue() const
  1430. {
  1431. wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
  1432. return m_time;
  1433. }
  1434. inline time_t wxDateTime::GetTicks() const
  1435. {
  1436. wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
  1437. if ( !IsInStdRange() )
  1438. {
  1439. return (time_t)-1;
  1440. }
  1441. return (time_t)((m_time / (long)TIME_T_FACTOR).ToLong()) + WX_TIME_BASE_OFFSET;
  1442. }
  1443. inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
  1444. Month month,
  1445. int year)
  1446. {
  1447. return SetToWeekDay(weekday, -1, month, year);
  1448. }
  1449. inline wxDateTime
  1450. wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
  1451. WeekFlags WXUNUSED(flags)) const
  1452. {
  1453. MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
  1454. }
  1455. inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
  1456. {
  1457. MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
  1458. }
  1459. inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
  1460. {
  1461. MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
  1462. }
  1463. inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
  1464. int n,
  1465. Month month,
  1466. int year) const
  1467. {
  1468. wxDateTime dt(*this);
  1469. return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
  1470. }
  1471. inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
  1472. Month month,
  1473. int year)
  1474. {
  1475. wxDateTime dt(*this);
  1476. return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
  1477. }
  1478. inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
  1479. {
  1480. MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
  1481. }
  1482. inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
  1483. {
  1484. MODIFY_AND_RETURN( SetToYearDay(yday) );
  1485. }
  1486. // ----------------------------------------------------------------------------
  1487. // wxDateTime comparison
  1488. // ----------------------------------------------------------------------------
  1489. inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
  1490. {
  1491. wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime"));
  1492. return m_time == datetime.m_time;
  1493. }
  1494. inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
  1495. {
  1496. wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime"));
  1497. return m_time < datetime.m_time;
  1498. }
  1499. inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
  1500. {
  1501. wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime"));
  1502. return m_time > datetime.m_time;
  1503. }
  1504. inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
  1505. const wxDateTime& t2) const
  1506. {
  1507. // no need for assert, will be checked by the functions we call
  1508. return IsLaterThan(t1) && IsEarlierThan(t2);
  1509. }
  1510. inline bool wxDateTime::IsBetween(const wxDateTime& t1,
  1511. const wxDateTime& t2) const
  1512. {
  1513. // no need for assert, will be checked by the functions we call
  1514. return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
  1515. }
  1516. inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
  1517. {
  1518. Tm tm1 = GetTm(),
  1519. tm2 = dt.GetTm();
  1520. return tm1.year == tm2.year &&
  1521. tm1.mon == tm2.mon &&
  1522. tm1.mday == tm2.mday;
  1523. }
  1524. inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
  1525. {
  1526. // notice that we can't do something like this:
  1527. //
  1528. // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
  1529. //
  1530. // because we have also to deal with (possibly) different DST settings!
  1531. Tm tm1 = GetTm(),
  1532. tm2 = dt.GetTm();
  1533. return tm1.hour == tm2.hour &&
  1534. tm1.min == tm2.min &&
  1535. tm1.sec == tm2.sec &&
  1536. tm1.msec == tm2.msec;
  1537. }
  1538. inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
  1539. const wxTimeSpan& ts) const
  1540. {
  1541. return IsBetween(dt.Subtract(ts), dt.Add(ts));
  1542. }
  1543. // ----------------------------------------------------------------------------
  1544. // wxDateTime arithmetics
  1545. // ----------------------------------------------------------------------------
  1546. inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
  1547. {
  1548. wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
  1549. return wxDateTime(m_time + diff.GetValue());
  1550. }
  1551. inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
  1552. {
  1553. wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
  1554. m_time += diff.GetValue();
  1555. return *this;
  1556. }
  1557. inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
  1558. {
  1559. return Add(diff);
  1560. }
  1561. inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
  1562. {
  1563. wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
  1564. return wxDateTime(m_time - diff.GetValue());
  1565. }
  1566. inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
  1567. {
  1568. wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
  1569. m_time -= diff.GetValue();
  1570. return *this;
  1571. }
  1572. inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
  1573. {
  1574. return Subtract(diff);
  1575. }
  1576. inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
  1577. {
  1578. wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime"));
  1579. return wxTimeSpan(GetValue() - datetime.GetValue());
  1580. }
  1581. inline wxTimeSpan wxDateTime::operator-(const wxDateTime& dt2) const
  1582. {
  1583. return this->Subtract(dt2);
  1584. }
  1585. inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
  1586. {
  1587. return wxDateTime(*this).Add(diff);
  1588. }
  1589. inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
  1590. {
  1591. return Add(diff.Negate());
  1592. }
  1593. inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
  1594. {
  1595. return wxDateTime(*this).Subtract(diff);
  1596. }
  1597. inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
  1598. {
  1599. return Subtract(diff);
  1600. }
  1601. inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
  1602. {
  1603. return Add(diff);
  1604. }
  1605. // ----------------------------------------------------------------------------
  1606. // wxDateTime and timezones
  1607. // ----------------------------------------------------------------------------
  1608. inline wxDateTime
  1609. wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
  1610. {
  1611. MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
  1612. }
  1613. inline wxDateTime
  1614. wxDateTime::FromTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
  1615. {
  1616. MODIFY_AND_RETURN( MakeFromTimezone(tz, noDST) );
  1617. }
  1618. // ----------------------------------------------------------------------------
  1619. // wxTimeSpan construction
  1620. // ----------------------------------------------------------------------------
  1621. inline wxTimeSpan::wxTimeSpan(long hours,
  1622. long minutes,
  1623. wxLongLong seconds,
  1624. wxLongLong milliseconds)
  1625. {
  1626. // assign first to avoid precision loss
  1627. m_diff = hours;
  1628. m_diff *= 60l;
  1629. m_diff += minutes;
  1630. m_diff *= 60l;
  1631. m_diff += seconds;
  1632. m_diff *= 1000l;
  1633. m_diff += milliseconds;
  1634. }
  1635. // ----------------------------------------------------------------------------
  1636. // wxTimeSpan accessors
  1637. // ----------------------------------------------------------------------------
  1638. inline wxLongLong wxTimeSpan::GetSeconds() const
  1639. {
  1640. return m_diff / 1000l;
  1641. }
  1642. inline int wxTimeSpan::GetMinutes() const
  1643. {
  1644. // For compatibility, this method (and the other accessors) return int,
  1645. // even though GetLo() actually returns unsigned long with greater range.
  1646. return static_cast<int>((GetSeconds() / 60l).GetLo());
  1647. }
  1648. inline int wxTimeSpan::GetHours() const
  1649. {
  1650. return GetMinutes() / 60;
  1651. }
  1652. inline int wxTimeSpan::GetDays() const
  1653. {
  1654. return GetHours() / 24;
  1655. }
  1656. inline int wxTimeSpan::GetWeeks() const
  1657. {
  1658. return GetDays() / 7;
  1659. }
  1660. // ----------------------------------------------------------------------------
  1661. // wxTimeSpan arithmetics
  1662. // ----------------------------------------------------------------------------
  1663. inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
  1664. {
  1665. return wxTimeSpan(m_diff + diff.GetValue());
  1666. }
  1667. inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
  1668. {
  1669. m_diff += diff.GetValue();
  1670. return *this;
  1671. }
  1672. inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
  1673. {
  1674. return wxTimeSpan(m_diff - diff.GetValue());
  1675. }
  1676. inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
  1677. {
  1678. m_diff -= diff.GetValue();
  1679. return *this;
  1680. }
  1681. inline wxTimeSpan& wxTimeSpan::Multiply(int n)
  1682. {
  1683. m_diff *= (long)n;
  1684. return *this;
  1685. }
  1686. inline wxTimeSpan wxTimeSpan::Multiply(int n) const
  1687. {
  1688. return wxTimeSpan(m_diff * (long)n);
  1689. }
  1690. inline wxTimeSpan wxTimeSpan::Abs() const
  1691. {
  1692. return wxTimeSpan(GetValue().Abs());
  1693. }
  1694. inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
  1695. {
  1696. return GetValue() == ts.GetValue();
  1697. }
  1698. inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
  1699. {
  1700. return GetValue().Abs() > ts.GetValue().Abs();
  1701. }
  1702. inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan& ts) const
  1703. {
  1704. return GetValue().Abs() < ts.GetValue().Abs();
  1705. }
  1706. // ----------------------------------------------------------------------------
  1707. // wxDateSpan
  1708. // ----------------------------------------------------------------------------
  1709. inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
  1710. {
  1711. m_years += other.m_years;
  1712. m_months += other.m_months;
  1713. m_weeks += other.m_weeks;
  1714. m_days += other.m_days;
  1715. return *this;
  1716. }
  1717. inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
  1718. {
  1719. return *this += other;
  1720. }
  1721. inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
  1722. {
  1723. wxDateSpan ds(*this);
  1724. ds.Add(other);
  1725. return ds;
  1726. }
  1727. inline wxDateSpan& wxDateSpan::Multiply(int factor)
  1728. {
  1729. m_years *= factor;
  1730. m_months *= factor;
  1731. m_weeks *= factor;
  1732. m_days *= factor;
  1733. return *this;
  1734. }
  1735. inline wxDateSpan wxDateSpan::Multiply(int factor) const
  1736. {
  1737. wxDateSpan ds(*this);
  1738. ds.Multiply(factor);
  1739. return ds;
  1740. }
  1741. inline wxDateSpan wxDateSpan::Negate() const
  1742. {
  1743. return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
  1744. }
  1745. inline wxDateSpan& wxDateSpan::Neg()
  1746. {
  1747. m_years = -m_years;
  1748. m_months = -m_months;
  1749. m_weeks = -m_weeks;
  1750. m_days = -m_days;
  1751. return *this;
  1752. }
  1753. inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
  1754. {
  1755. return *this += other.Negate();
  1756. }
  1757. inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
  1758. {
  1759. return *this -= other;
  1760. }
  1761. inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
  1762. {
  1763. wxDateSpan ds(*this);
  1764. ds.Subtract(other);
  1765. return ds;
  1766. }
  1767. #undef MILLISECONDS_PER_DAY
  1768. #undef MODIFY_AND_RETURN
  1769. // ============================================================================
  1770. // binary operators
  1771. // ============================================================================
  1772. // ----------------------------------------------------------------------------
  1773. // wxTimeSpan operators
  1774. // ----------------------------------------------------------------------------
  1775. wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts);
  1776. // ----------------------------------------------------------------------------
  1777. // wxDateSpan
  1778. // ----------------------------------------------------------------------------
  1779. wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds);
  1780. // ============================================================================
  1781. // other helper functions
  1782. // ============================================================================
  1783. // ----------------------------------------------------------------------------
  1784. // iteration helpers: can be used to write a for loop over enum variable like
  1785. // this:
  1786. // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
  1787. // ----------------------------------------------------------------------------
  1788. WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m);
  1789. WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m);
  1790. WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd);
  1791. WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd);
  1792. #endif // wxUSE_DATETIME
  1793. #endif // _WX_DATETIME_H