dateutils.pp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. $Id$
  5. This file is part of the Free Pascal run time library.
  6. Copyright (c) 1999-2000 by the Free Pascal development team
  7. Delphi/Kylix compatibility unit, provides Date/Time handling routines.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit dateutils;
  15. interface
  16. uses
  17. SysUtils, Math, Types;
  18. { ---------------------------------------------------------------------
  19. Various constants
  20. ---------------------------------------------------------------------}
  21. const
  22. DaysPerWeek = 7;
  23. WeeksPerFortnight = 2;
  24. MonthsPerYear = 12;
  25. YearsPerDecade = 10;
  26. YearsPerCentury = 100;
  27. YearsPerMillennium = 1000;
  28. // ISO day numbers.
  29. DayMonday = 1;
  30. DayTuesday = 2;
  31. DayWednesday = 3;
  32. DayThursday = 4;
  33. DayFriday = 5;
  34. DaySaturday = 6;
  35. DaySunday = 7;
  36. // Fraction of a day
  37. OneHour = 1/HoursPerDay;
  38. OneMinute = 1/MinsPerDay;
  39. OneSecond = 1/SecsPerDay;
  40. OneMillisecond = 1/MSecsPerDay;
  41. { This is actual days per year but you need to know if it's a leap year}
  42. DaysPerYear: array [Boolean] of Word = (365, 366);
  43. { Used in RecodeDate, RecodeTime and RecodeDateTime for those datetime }
  44. { fields you want to leave alone }
  45. RecodeLeaveFieldAsIs = High(Word);
  46. { ---------------------------------------------------------------------
  47. Global variables used in this unit
  48. ---------------------------------------------------------------------}
  49. Const
  50. { Average over a 4 year span. Valid for next 100 years }
  51. ApproxDaysPerMonth: Double = 30.4375;
  52. ApproxDaysPerYear: Double = 365.25;
  53. { ---------------------------------------------------------------------
  54. Simple trimming functions.
  55. ---------------------------------------------------------------------}
  56. Function DateOf(const AValue: TDateTime): TDateTime;
  57. Function TimeOf(const AValue: TDateTime): TDateTime;
  58. { ---------------------------------------------------------------------
  59. Identification functions.
  60. ---------------------------------------------------------------------}
  61. Function IsInLeapYear(const AValue: TDateTime): Boolean;
  62. Function IsPM(const AValue: TDateTime): Boolean;
  63. Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
  64. Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  65. Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  66. Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
  67. Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
  68. Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
  69. { ---------------------------------------------------------------------
  70. Enumeration functions.
  71. ---------------------------------------------------------------------}
  72. Function WeeksInYear(const AValue: TDateTime): Word;
  73. Function WeeksInAYear(const AYear: Word): Word;
  74. Function DaysInYear(const AValue: TDateTime): Word;
  75. Function DaysInAYear(const AYear: Word): Word;
  76. Function DaysInMonth(const AValue: TDateTime): Word;
  77. Function DaysInAMonth(const AYear, AMonth: Word): Word;
  78. { ---------------------------------------------------------------------
  79. Variations on current date/time.
  80. ---------------------------------------------------------------------}
  81. Function Today: TDateTime;
  82. Function Yesterday: TDateTime;
  83. Function Tomorrow: TDateTime;
  84. Function IsToday(const AValue: TDateTime): Boolean;
  85. Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
  86. { ---------------------------------------------------------------------
  87. Extraction functions.
  88. ---------------------------------------------------------------------}
  89. Function YearOf(const AValue: TDateTime): Word;
  90. Function MonthOf(const AValue: TDateTime): Word;
  91. Function WeekOf(const AValue: TDateTime): Word;
  92. Function DayOf(const AValue: TDateTime): Word;
  93. Function HourOf(const AValue: TDateTime): Word;
  94. Function MinuteOf(const AValue: TDateTime): Word;
  95. Function SecondOf(const AValue: TDateTime): Word;
  96. Function MilliSecondOf(const AValue: TDateTime): Word;
  97. { ---------------------------------------------------------------------
  98. Start/End of year functions.
  99. ---------------------------------------------------------------------}
  100. Function StartOfTheYear(const AValue: TDateTime): TDateTime;
  101. Function EndOfTheYear(const AValue: TDateTime): TDateTime;
  102. Function StartOfAYear(const AYear: Word): TDateTime;
  103. Function EndOfAYear(const AYear: Word): TDateTime;
  104. { ---------------------------------------------------------------------
  105. Start/End of month functions.
  106. ---------------------------------------------------------------------}
  107. Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
  108. Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
  109. Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
  110. Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
  111. { ---------------------------------------------------------------------
  112. Start/End of week functions.
  113. ---------------------------------------------------------------------}
  114. Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
  115. Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
  116. Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  117. Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
  118. Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  119. Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
  120. { ---------------------------------------------------------------------
  121. Start/End of day functions.
  122. ---------------------------------------------------------------------}
  123. Function StartOfTheDay(const AValue: TDateTime): TDateTime;
  124. Function EndOfTheDay(const AValue: TDateTime): TDateTime;
  125. Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
  126. Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
  127. Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
  128. Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
  129. { ---------------------------------------------------------------------
  130. Part of year functions.
  131. ---------------------------------------------------------------------}
  132. Function MonthOfTheYear(const AValue: TDateTime): Word;
  133. Function WeekOfTheYear(const AValue: TDateTime): Word; overload;
  134. Function WeekOfTheYear(const AValue: TDateTime; var AYear: Word): Word; overload;
  135. Function DayOfTheYear(const AValue: TDateTime): Word;
  136. Function HourOfTheYear(const AValue: TDateTime): Word;
  137. Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
  138. Function SecondOfTheYear(const AValue: TDateTime): LongWord;
  139. Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
  140. { ---------------------------------------------------------------------
  141. Part of month functions.
  142. ---------------------------------------------------------------------}
  143. Function WeekOfTheMonth(const AValue: TDateTime): Word; overload;
  144. Function WeekOfTheMonth(const AValue: TDateTime; var AYear, AMonth: Word): Word; overload;
  145. Function DayOfTheMonth(const AValue: TDateTime): Word;
  146. Function HourOfTheMonth(const AValue: TDateTime): Word;
  147. Function MinuteOfTheMonth(const AValue: TDateTime): Word;
  148. Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
  149. Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
  150. { ---------------------------------------------------------------------
  151. Part of week functions.
  152. ---------------------------------------------------------------------}
  153. Function DayOfTheWeek(const AValue: TDateTime): Word;
  154. Function HourOfTheWeek(const AValue: TDateTime): Word;
  155. Function MinuteOfTheWeek(const AValue: TDateTime): Word;
  156. Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
  157. Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
  158. { ---------------------------------------------------------------------
  159. Part of day functions.
  160. ---------------------------------------------------------------------}
  161. Function HourOfTheDay(const AValue: TDateTime): Word;
  162. Function MinuteOfTheDay(const AValue: TDateTime): Word;
  163. Function SecondOfTheDay(const AValue: TDateTime): LongWord;
  164. Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
  165. { ---------------------------------------------------------------------
  166. Part of hour functions.
  167. ---------------------------------------------------------------------}
  168. Function MinuteOfTheHour(const AValue: TDateTime): Word;
  169. Function SecondOfTheHour(const AValue: TDateTime): Word;
  170. Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
  171. { ---------------------------------------------------------------------
  172. Part of minute functions.
  173. ---------------------------------------------------------------------}
  174. Function SecondOfTheMinute(const AValue: TDateTime): Word;
  175. Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
  176. { ---------------------------------------------------------------------
  177. Part of second functions.
  178. ---------------------------------------------------------------------}
  179. Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
  180. { ---------------------------------------------------------------------
  181. Range checking functions.
  182. ---------------------------------------------------------------------}
  183. Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
  184. Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
  185. Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
  186. Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
  187. Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
  188. Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
  189. Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
  190. Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
  191. { ---------------------------------------------------------------------
  192. Period functions.
  193. ---------------------------------------------------------------------}
  194. Function YearsBetween(const ANow, AThen: TDateTime): Integer;
  195. Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
  196. Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
  197. Function DaysBetween(const ANow, AThen: TDateTime): Integer;
  198. Function HoursBetween(const ANow, AThen: TDateTime): Int64;
  199. Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
  200. Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
  201. Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
  202. { ---------------------------------------------------------------------
  203. Timespan in xxx functions.
  204. ---------------------------------------------------------------------}
  205. { YearSpan and MonthSpan are approximate values }
  206. Function YearSpan(const ANow, AThen: TDateTime): Double;
  207. Function MonthSpan(const ANow, AThen: TDateTime): Double;
  208. Function WeekSpan(const ANow, AThen: TDateTime): Double;
  209. Function DaySpan(const ANow, AThen: TDateTime): Double;
  210. Function HourSpan(const ANow, AThen: TDateTime): Double;
  211. Function MinuteSpan(const ANow, AThen: TDateTime): Double;
  212. Function SecondSpan(const ANow, AThen: TDateTime): Double;
  213. Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
  214. { ---------------------------------------------------------------------
  215. Increment/decrement functions.
  216. ---------------------------------------------------------------------}
  217. Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
  218. Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
  219. // Function IncMonth is in SysUtils
  220. Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
  221. Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
  222. Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
  223. Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
  224. Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
  225. Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
  226. Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
  227. Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
  228. Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
  229. Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
  230. Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
  231. Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
  232. { ---------------------------------------------------------------------
  233. Encode/Decode of complete timestamp
  234. ---------------------------------------------------------------------}
  235. Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  236. Procedure DecodeDateTime(const AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
  237. Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AValue: TDateTime): Boolean;
  238. { ---------------------------------------------------------------------
  239. Encode/decode date, specifying week of year and day of week
  240. ---------------------------------------------------------------------}
  241. Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  242. Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
  243. Procedure DecodeDateWeek(const AValue: TDateTime; var AYear, AWeekOfYear, ADayOfWeek: Word);
  244. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime; const ADayOfWeek: Word): Boolean;
  245. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
  246. { ---------------------------------------------------------------------
  247. Encode/decode date, specifying day of year
  248. ---------------------------------------------------------------------}
  249. Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
  250. Procedure DecodeDateDay(const AValue: TDateTime; var AYear, ADayOfYear: Word);
  251. Function TryEncodeDateDay(const AYear, ADayOfYear: Word; var AValue: TDateTime): Boolean;
  252. { ---------------------------------------------------------------------
  253. Encode/decode date, specifying week of month
  254. ---------------------------------------------------------------------}
  255. Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
  256. Procedure DecodeDateMonthWeek(const AValue: TDateTime; var AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  257. Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
  258. { ---------------------------------------------------------------------
  259. Replace given element with supplied value.
  260. ---------------------------------------------------------------------}
  261. Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
  262. Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
  263. Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
  264. Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
  265. Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
  266. Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
  267. Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
  268. Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
  269. Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  270. Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  271. Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AResult: TDateTime): Boolean;
  272. { ---------------------------------------------------------------------
  273. Comparision of date/time
  274. ---------------------------------------------------------------------}
  275. Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
  276. Function CompareDate(const A, B: TDateTime): TValueRelationship;
  277. Function CompareTime(const A, B: TDateTime): TValueRelationship;
  278. Function SameDateTime(const A, B: TDateTime): Boolean;
  279. Function SameDate(const A, B: TDateTime): Boolean;
  280. Function SameTime(const A, B: TDateTime): Boolean;
  281. { For a given date these Functions tell you the which day of the week of the
  282. month (or year). If its a Thursday, they will tell you if its the first,
  283. second, etc Thursday of the month (or year). Remember, even though its
  284. the first Thursday of the year it doesn't mean its the first week of the
  285. year. See ISO 8601 above for more information. }
  286. Function NthDayOfWeek(const AValue: TDateTime): Word;
  287. Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; var AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  288. Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
  289. Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
  290. { ---------------------------------------------------------------------
  291. Exception throwing routines
  292. ---------------------------------------------------------------------}
  293. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
  294. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
  295. Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
  296. Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
  297. Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  298. Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  299. { ---------------------------------------------------------------------
  300. Julian and Modified Julian Date conversion support
  301. ---------------------------------------------------------------------}
  302. Function DateTimeToJulianDate(const AValue: TDateTime): Double;
  303. Function JulianDateToDateTime(const AValue: Double): TDateTime;
  304. Function TryJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
  305. Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
  306. Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
  307. Function TryModifiedJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
  308. { ---------------------------------------------------------------------
  309. Unix timestamp support.
  310. ---------------------------------------------------------------------}
  311. Function DateTimeToUnix(const AValue: TDateTime): Int64;
  312. Function UnixToDateTime(const AValue: Int64): TDateTime;
  313. implementation
  314. { ---------------------------------------------------------------------
  315. Auxiliary routines
  316. ---------------------------------------------------------------------}
  317. Procedure NotYetImplemented (FN : String);
  318. begin
  319. Raise Exception.CreateFmt('Function "%s" (dateutils) is not yet implemented',[FN]);
  320. end;
  321. { ---------------------------------------------------------------------
  322. Simple trimming functions.
  323. ---------------------------------------------------------------------}
  324. Function DateOf(const AValue: TDateTime): TDateTime;
  325. begin
  326. Result:=Trunc(DateOf);
  327. end;
  328. Function TimeOf(const AValue: TDateTime): TDateTime;
  329. begin
  330. Result:=Frac(Avalue);
  331. end;
  332. { ---------------------------------------------------------------------
  333. Identification functions.
  334. ---------------------------------------------------------------------}
  335. Function IsInLeapYear(const AValue: TDateTime): Boolean;
  336. Var
  337. D,Y,M : Word;
  338. begin
  339. DecodeDate(AValue,Y,M,D);
  340. Result:=IsLeapYear(Y);
  341. end;
  342. Function IsPM(const AValue: TDateTime): Boolean;
  343. Var
  344. H,M,S,MS : Word;
  345. begin
  346. DecodeTime(AValue,H,M,S,MS);
  347. Result:=(H>=12);
  348. end;
  349. Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
  350. begin
  351. Result:=(AYear<>0) and (AYear<10000)
  352. and (AMonth in [1..12])
  353. and (ADay<>0) and (ADay<=MonthDays[IsleapYear(AYear),AMonth]);
  354. end;
  355. Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  356. begin
  357. Result:=(AHour=HoursPerDay) and (AMinute=0) and (ASecond=0) and (AMillisecond=0);
  358. Result:=Result or
  359. ((AHour<HoursPerDay) and (AMinute<MinsPerHour) and (ASecond<SecsPerMin) and
  360. (AMillisecond<MSecsPerSec));
  361. end;
  362. Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  363. begin
  364. Result:=IsValidDate(AYear,AMonth,ADay) and
  365. IsValidTime(AHour,AMinute,ASecond,AMillisecond)
  366. end;
  367. Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
  368. begin
  369. Result:=(AYear<>0) and (ADayOfYear<>0) and (AYear<10000) and
  370. (ADayOfYear<=DaysPerYear[IsLeapYear(AYear)]);
  371. end;
  372. Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
  373. begin
  374. Result:=(AYear<>0) and (AYear<10000)
  375. and (ADayOfWeek in [1..7])
  376. and (AWeekOfYear<>0)
  377. and (AWeekOfYear<=WeeksInaYear(AYear));
  378. { should we not also check whether the day of the week is not
  379. larger than the last day of the last week in the year 9999 ?? }
  380. end;
  381. Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
  382. begin
  383. Result:=(AYear<>0) and (AYear<10000)
  384. and (AMonth in [1..12])
  385. and (AWeekOfMonth in [1..5])
  386. and (ADayOfWeek in [1..7]);
  387. end;
  388. { ---------------------------------------------------------------------
  389. Enumeration functions.
  390. ---------------------------------------------------------------------}
  391. Function WeeksInYear(const AValue: TDateTime): Word;
  392. Var
  393. Y,M,D : Word;
  394. begin
  395. DecodeDate(AValue,Y,M,D);
  396. Result:=WeeksInAYear(Y);
  397. end;
  398. Function WeeksInAYear(const AYear: Word): Word;
  399. Var
  400. DOW : Word;
  401. begin
  402. Result:=52;
  403. DOW:=DayOfTheWeek(StartOfAYear(AYear));
  404. If (DOW=4) or ((DOW=3) and IsLeapYear(AYear)) then
  405. Inc(Result);
  406. end;
  407. Function DaysInYear(const AValue: TDateTime): Word;
  408. Var
  409. Y,M,D : Word;
  410. begin
  411. DecodeDate(AValue,Y,M,D);
  412. Result:=DaysPerYear[IsLeapYear(Y)];
  413. end;
  414. Function DaysInAYear(const AYear: Word): Word;
  415. begin
  416. Result:=DaysPerYear[Isleapyear(AYear)];
  417. end;
  418. Function DaysInMonth(const AValue: TDateTime): Word;
  419. Var
  420. Y,M,D : Word;
  421. begin
  422. Decodedate(AValue,Y,M,D);
  423. Result:=MonthDays[IsLeapYear(Y),M];
  424. end;
  425. Function DaysInAMonth(const AYear, AMonth: Word): Word;
  426. begin
  427. Result:=MonthDays[IsLeapYear(AYear),AMonth];
  428. end;
  429. { ---------------------------------------------------------------------
  430. Variations on current date/time.
  431. ---------------------------------------------------------------------}
  432. Function Today: TDateTime;
  433. begin
  434. Result:=Date;
  435. end;
  436. Function Yesterday: TDateTime;
  437. begin
  438. Result:=Date-1;
  439. end;
  440. Function Tomorrow: TDateTime;
  441. begin
  442. Result:=Date+1;
  443. end;
  444. Function IsToday(const AValue: TDateTime): Boolean;
  445. begin
  446. Result:=IsSameDay(AValue,Date);
  447. end;
  448. Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
  449. Var
  450. D : TDateTime;
  451. begin
  452. D:=AValue-Trunc(ABasis);
  453. Result:=(D>=0) and (D<1);
  454. end;
  455. { ---------------------------------------------------------------------
  456. Extraction functions.
  457. ---------------------------------------------------------------------}
  458. Function YearOf(const AValue: TDateTime): Word;
  459. Var
  460. D,M : Word;
  461. begin
  462. DecodeDate(AValue,Result,D,M);
  463. end;
  464. Function MonthOf(const AValue: TDateTime): Word;
  465. Var
  466. Y,D : Word;
  467. begin
  468. DecodeDate(AValue,Y,Result,D);
  469. end;
  470. Function WeekOf(const AValue: TDateTime): Word;
  471. begin
  472. Result:=WeekOfTheYear(AValue);
  473. end;
  474. Function DayOf(const AValue: TDateTime): Word;
  475. Var
  476. Y,M : Word;
  477. begin
  478. DecodeDate(AValue,Y,M,Result);
  479. end;
  480. Function HourOf(const AValue: TDateTime): Word;
  481. Var
  482. N,S,MS : Word;
  483. begin
  484. DecodeTime(AValue,Result,N,S,MS);
  485. end;
  486. Function MinuteOf(const AValue: TDateTime): Word;
  487. Var
  488. H,S,MS : Word;
  489. begin
  490. DecodeTime(AValue,H,Result,S,MS);
  491. end;
  492. Function SecondOf(const AValue: TDateTime): Word;
  493. Var
  494. H,N,MS : Word;
  495. begin
  496. DecodeTime(AVAlue,H,N,Result,MS);
  497. end;
  498. Function MilliSecondOf(const AValue: TDateTime): Word;
  499. Var
  500. H,N,S : Word;
  501. begin
  502. DecodeTime(AValue,H,N,S,Result);
  503. end;
  504. { ---------------------------------------------------------------------
  505. Start/End of year functions.
  506. ---------------------------------------------------------------------}
  507. Function StartOfTheYear(const AValue: TDateTime): TDateTime;
  508. Var
  509. Y,M,D : Word;
  510. begin
  511. DecodeDate(AValue,Y,M,D);
  512. Result:=EncodeDate(Y,1,1);
  513. end;
  514. Function EndOfTheYear(const AValue: TDateTime): TDateTime;
  515. Var
  516. Y,M,D : Word;
  517. begin
  518. DecodeDate(AValue,Y,M,D);
  519. Result:=EncodeDateTime(Y,12,31,23,59,59,999);
  520. end;
  521. Function StartOfAYear(const AYear: Word): TDateTime;
  522. begin
  523. Result:=EncodeDate(AYear,1,1);
  524. end;
  525. Function EndOfAYear(const AYear: Word): TDateTime;
  526. begin
  527. Result:=(EncodeDateTime(AYear,12,31,23,59,59,999));
  528. end;
  529. { ---------------------------------------------------------------------
  530. Start/End of month functions.
  531. ---------------------------------------------------------------------}
  532. Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
  533. Var
  534. Y,M,D : Word;
  535. begin
  536. DecodeDate(AValue,Y,M,D);
  537. Result:=EncodeDate(Y,M,1);
  538. // MonthDays[IsLeapYear(Y),M])
  539. end;
  540. Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
  541. Var
  542. Y,M,D : Word;
  543. begin
  544. DecodeDate(AValue,Y,M,D);
  545. Result:=EncodeDateTime(Y,M,MonthDays[IsLeapYear(Y),M],23,59,59,999);
  546. end;
  547. Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
  548. begin
  549. Result:=EncodeDate(AYear,AMonth,1);
  550. end;
  551. Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
  552. begin
  553. Result:=EncodeDateTime(AYear,AMonth,MonthDays[IsLeapYear(AYear),AMonth],23,59,59,999);
  554. end;
  555. { ---------------------------------------------------------------------
  556. Start/End of week functions.
  557. ---------------------------------------------------------------------}
  558. Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
  559. begin
  560. Result:=Trunc(AValue)-DayOfTheWeek(AValue)+1;
  561. end;
  562. Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
  563. begin
  564. Result:=EndOfTheDay(AValue-DayOfTheWeek(AValue)+7);
  565. end;
  566. Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  567. begin
  568. Result:=EncodeDateWeek(AYear,AWeekOfYear,ADayOfWeek);
  569. end;
  570. Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
  571. begin
  572. Result:=StartOfAWeek(AYear,AWeekOfYear,1)
  573. end;
  574. Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  575. begin
  576. NotYetImplemented('EndOfAWeek');
  577. end;
  578. Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
  579. begin
  580. Result:=EndOfAWeek(AYear,AWeekOfYear,7);
  581. end;
  582. { ---------------------------------------------------------------------
  583. Start/End of day functions.
  584. ---------------------------------------------------------------------}
  585. Function StartOfTheDay(const AValue: TDateTime): TDateTime;
  586. begin
  587. StartOfTheDay:=Trunc(Avalue);
  588. end;
  589. Function EndOfTheDay(const AValue: TDateTime): TDateTime;
  590. Var
  591. Y,M,D : Word;
  592. begin
  593. DecodeDate(AValue,Y,M,D);
  594. Result:=EncodeDateTime(Y,M,D,23,59,59,999);
  595. end;
  596. Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime;
  597. begin
  598. Result:=EncodeDate(AYear,AMonth,ADay);
  599. end;
  600. Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime;
  601. begin
  602. Result:=StartOfAYear(AYear)+ADayOfYear;
  603. end;
  604. Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime;
  605. begin
  606. Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
  607. end;
  608. Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime;
  609. begin
  610. Result:=StartOfAYear(AYear)+ADayOfYear+EncodeTime(23,59,59,999);
  611. end;
  612. { ---------------------------------------------------------------------
  613. Part of year functions.
  614. ---------------------------------------------------------------------}
  615. Function MonthOfTheYear(const AValue: TDateTime): Word;
  616. Var
  617. Y,D : Word;
  618. begin
  619. DecodeDate(AValue,Y,Result,D);
  620. end;
  621. Function WeekOfTheYear(const AValue: TDateTime): Word;
  622. Var
  623. Y,DOW : Word;
  624. begin
  625. DecodeDateWeek(AValue,Y,Result,DOW)
  626. end;
  627. Function WeekOfTheYear(const AValue: TDateTime; var AYear: Word): Word;
  628. Var
  629. DOW : Word;
  630. begin
  631. DecodeDateWeek(AValue,AYear,Result,DOW);
  632. end;
  633. Function DayOfTheYear(const AValue: TDateTime): Word;
  634. begin
  635. Result:=Trunc(AValue-StartOfTheYear(AValue)+1);
  636. end;
  637. Function HourOfTheYear(const AValue: TDateTime): Word;
  638. Var
  639. H,M,S,MS : Word;
  640. begin
  641. DecodeTime(AValue,H,M,S,MS);
  642. Result:=H+((DayOfTheYear(AValue)-1)*24);
  643. end;
  644. Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
  645. Var
  646. H,M,S,MS : Word;
  647. begin
  648. DecodeTime(AValue,H,M,S,MS);
  649. Result:=M+(H+((DayOfTheYear(AValue)-1)*24))*60;
  650. end;
  651. Function SecondOfTheYear(const AValue: TDateTime): LongWord;
  652. Var
  653. H,M,S,MS : Word;
  654. begin
  655. DecodeTime(AValue,H,M,S,MS);
  656. Result:=(M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S;
  657. end;
  658. Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
  659. Var
  660. H,M,S,MS : Word;
  661. begin
  662. DecodeTime(AValue,H,M,S,MS);
  663. Result:=((M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S)*1000+MS;
  664. end;
  665. { ---------------------------------------------------------------------
  666. Part of month functions.
  667. ---------------------------------------------------------------------}
  668. Function WeekOfTheMonth(const AValue: TDateTime): Word;
  669. var
  670. Y,M,DOW : word;
  671. begin
  672. DecodeDateMonthWeek(AValue,Y,M,Result,DOW);
  673. end;
  674. Function WeekOfTheMonth(const AValue: TDateTime; var AYear, AMonth: Word): Word;
  675. Var
  676. DOW : Word;
  677. begin
  678. DecodeDateMonthWeek(AValue,AYear,AMonth,Result,DOW);
  679. end;
  680. Function DayOfTheMonth(const AValue: TDateTime): Word;
  681. Var
  682. Y,M : Word;
  683. begin
  684. DecodeDate(AValue,Y,M,Result);
  685. end;
  686. Function HourOfTheMonth(const AValue: TDateTime): Word;
  687. Var
  688. Y,M,D,H,N,S,MS : Word;
  689. begin
  690. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  691. Result:=(D-1)*24+H;
  692. end;
  693. Function MinuteOfTheMonth(const AValue: TDateTime): Word;
  694. Var
  695. Y,M,D,H,N,S,MS : Word;
  696. begin
  697. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  698. Result:=((D-1)*24+H)*60+N;
  699. end;
  700. Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
  701. Var
  702. Y,M,D,H,N,S,MS : Word;
  703. begin
  704. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  705. Result:=(((D-1)*24+H)*60+N)*60+S;
  706. end;
  707. Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
  708. Var
  709. Y,M,D,H,N,S,MS : Word;
  710. begin
  711. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  712. Result:=((((D-1)*24+H)*60+N)*60+S)*1000+MS;
  713. end;
  714. { ---------------------------------------------------------------------
  715. Part of week functions.
  716. ---------------------------------------------------------------------}
  717. Function DayOfTheWeek(const AValue: TDateTime): Word;
  718. begin
  719. Result:=(DayOfWeek(AValue)-1) mod 7;
  720. end;
  721. Function HourOfTheWeek(const AValue: TDateTime): Word;
  722. Var
  723. H,M,S,MS : Word;
  724. begin
  725. DecodeTime(AValue,H,M,S,MS);
  726. Result:=(DayOfTheWeek(AValue)-1)*24+H;
  727. end;
  728. Function MinuteOfTheWeek(const AValue: TDateTime): Word;
  729. Var
  730. H,M,S,MS : Word;
  731. begin
  732. DecodeTime(AValue,H,M,S,MS);
  733. Result:=((DayOfTheWeek(AValue)-1)*24+H)*60+M;
  734. end;
  735. Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
  736. Var
  737. H,M,S,MS : Word;
  738. begin
  739. DecodeTime(AValue,H,M,S,MS);
  740. Result:=(((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S;
  741. end;
  742. Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
  743. Var
  744. H,M,S,MS : Word;
  745. begin
  746. DecodeTime(AValue,H,M,S,MS);
  747. Result:=((((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S)*1000+MS;
  748. end;
  749. { ---------------------------------------------------------------------
  750. Part of day functions.
  751. ---------------------------------------------------------------------}
  752. Function HourOfTheDay(const AValue: TDateTime): Word;
  753. Var
  754. M,S,MS : Word;
  755. begin
  756. DecodeTime(AValue,Result,M,S,MS);
  757. end;
  758. Function MinuteOfTheDay(const AValue: TDateTime): Word;
  759. Var
  760. H,M,S,MS : Word;
  761. begin
  762. DecodeTime(AValue,H,M,S,MS);
  763. Result:=(H*60)+M;
  764. end;
  765. Function SecondOfTheDay(const AValue: TDateTime): LongWord;
  766. Var
  767. H,M,S,MS : Word;
  768. begin
  769. DecodeTime(AValue,H,M,S,MS);
  770. Result:=((H*60)+M)*60+S;
  771. end;
  772. Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
  773. Var
  774. H,M,S,MS : Word;
  775. begin
  776. DecodeTime(AValue,H,M,S,MS);
  777. Result:=(((H*60)+M)*60+S)*1000+MS;
  778. end;
  779. { ---------------------------------------------------------------------
  780. Part of hour functions.
  781. ---------------------------------------------------------------------}
  782. Function MinuteOfTheHour(const AValue: TDateTime): Word;
  783. Var
  784. H,S,MS : Word;
  785. begin
  786. DecodeTime(AValue,H,Result,S,MS);
  787. end;
  788. Function SecondOfTheHour(const AValue: TDateTime): Word;
  789. Var
  790. H,S,M,MS : Word;
  791. begin
  792. DecodeTime(AValue,H,M,S,MS);
  793. Result:=M*60+S;
  794. end;
  795. Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
  796. Var
  797. H,S,M,MS : Word;
  798. begin
  799. DecodeTime(AValue,H,M,S,MS);
  800. Result:=(M*60+S)*1000+MS;
  801. end;
  802. { ---------------------------------------------------------------------
  803. Part of minute functions.
  804. ---------------------------------------------------------------------}
  805. Function SecondOfTheMinute(const AValue: TDateTime): Word;
  806. Var
  807. H,M,MS : Word;
  808. begin
  809. DecodeTime(AValue,H,M,Result,MS);
  810. end;
  811. Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
  812. Var
  813. H,S,M,MS : Word;
  814. begin
  815. DecodeTime(AValue,H,M,S,MS);
  816. Result:=S*1000+MS;
  817. end;
  818. { ---------------------------------------------------------------------
  819. Part of second functions.
  820. ---------------------------------------------------------------------}
  821. Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
  822. Var
  823. H,M,S : Word;
  824. begin
  825. DecodeTime(AValue,H,M,S,Result);
  826. end;
  827. { ---------------------------------------------------------------------
  828. Range checking functions.
  829. ---------------------------------------------------------------------}
  830. Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
  831. begin
  832. Result:=YearsBetween(ANow,AThen)<=AYears;
  833. end;
  834. Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
  835. begin
  836. Result:=MonthsBetween(ANow,AThen)<=AMonths;
  837. end;
  838. Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
  839. begin
  840. Result:=WeeksBetween(ANow,AThen)<=AWeeks;
  841. end;
  842. Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
  843. begin
  844. Result:=DaysBetween(ANow,AThen)<=ADays;
  845. end;
  846. Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
  847. begin
  848. Result:=HoursBetween(ANow,AThen)<=AHours;
  849. end;
  850. Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
  851. begin
  852. Result:=MinutesBetween(ANow,AThen)<=AMinutes;
  853. end;
  854. Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
  855. begin
  856. Result:=SecondsBetween(ANow,Athen)<=ASeconds;
  857. end;
  858. Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
  859. begin
  860. Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
  861. end;
  862. { ---------------------------------------------------------------------
  863. Period functions.
  864. ---------------------------------------------------------------------}
  865. {
  866. These functions are declared as approximate by Borland.
  867. A bit strange, since it can be calculated exactly ?
  868. }
  869. Function YearsBetween(const ANow, AThen: TDateTime): Integer;
  870. begin
  871. Result:=Trunc(Abs(ANow-AThen)/ApproxDaysPerYear);
  872. end;
  873. Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
  874. begin
  875. Result:=Trunc(Abs(ANow-Athen)/ApproxDaysPerMonth);
  876. end;
  877. Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
  878. begin
  879. Result:=Trunc(Abs(ANow-AThen)) div 7;
  880. end;
  881. Function DaysBetween(const ANow, AThen: TDateTime): Integer;
  882. begin
  883. Result:=Trunc(Abs(ANow-AThen));
  884. end;
  885. Function HoursBetween(const ANow, AThen: TDateTime): Int64;
  886. begin
  887. Result:=Trunc(Abs(ANow-AThen)*HoursPerDay);
  888. end;
  889. Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
  890. begin
  891. Result:=Trunc(Abs(ANow-AThen)*MinsPerDay);
  892. end;
  893. Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
  894. begin
  895. Result:=Trunc(Abs(ANow-AThen)*SecsPerDay);
  896. end;
  897. Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
  898. begin
  899. Result:=Trunc(Abs(ANow-AThen)*MSecsPerDay);
  900. end;
  901. { ---------------------------------------------------------------------
  902. Timespan in xxx functions.
  903. ---------------------------------------------------------------------}
  904. Function YearSpan(const ANow, AThen: TDateTime): Double;
  905. begin
  906. Result:=Abs(Anow-Athen)/ApproxDaysPerYear;
  907. end;
  908. Function MonthSpan(const ANow, AThen: TDateTime): Double;
  909. begin
  910. Result:=Abs(ANow-AThen)/ApproxDaysPerMonth;
  911. end;
  912. Function WeekSpan(const ANow, AThen: TDateTime): Double;
  913. begin
  914. Result:=Abs(ANow-AThen) / 7
  915. end;
  916. Function DaySpan(const ANow, AThen: TDateTime): Double;
  917. begin
  918. Result:=Abs(ANow-AThen);
  919. end;
  920. Function HourSpan(const ANow, AThen: TDateTime): Double;
  921. begin
  922. Result:=Abs(ANow-AThen)*HoursPerDay;
  923. end;
  924. Function MinuteSpan(const ANow, AThen: TDateTime): Double;
  925. begin
  926. Result:=Abs(ANow-AThen)*MinsPerDay;
  927. end;
  928. Function SecondSpan(const ANow, AThen: TDateTime): Double;
  929. begin
  930. Result:=Abs(ANow-AThen)*SecsPerDay;
  931. end;
  932. Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
  933. begin
  934. Result:=Abs(ANow-AThen)*MSecsPerDay;
  935. end;
  936. { ---------------------------------------------------------------------
  937. Increment/decrement functions.
  938. ---------------------------------------------------------------------}
  939. Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
  940. Var
  941. Y,M,D,H,N,S,MS : Word;
  942. begin
  943. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  944. Y:=Y+ANumberOfYears;
  945. If (M=2) and (D=29) And (Not IsLeapYear(Y)) then
  946. D:=28;
  947. Result:=EncodeDateTime(Y,M,D,H,N,S,MS);
  948. end;
  949. Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
  950. begin
  951. Result:=IncYear(Avalue,1);
  952. end;
  953. Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
  954. begin
  955. Result:=AValue+ANumberOfWeeks*7;
  956. end;
  957. Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
  958. begin
  959. Result:=IncWeek(Avalue,1);
  960. end;
  961. Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
  962. begin
  963. Result:=AValue+ANumberOfDays;
  964. end;
  965. Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
  966. begin
  967. Result:=IncDay(Avalue,1);
  968. end;
  969. Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
  970. begin
  971. Result:=AValue+ANumberOfHours/HoursPerDay;
  972. end;
  973. Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
  974. begin
  975. Result:=IncHour(AValue,1);
  976. end;
  977. Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
  978. begin
  979. Result:=Result+ANumberOfMinutes / MinsPerDay;
  980. end;
  981. Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
  982. begin
  983. Result:=IncMinute(AValue,1);
  984. end;
  985. Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
  986. begin
  987. Result:=Result+ANumberOfSeconds / SecsPerDay;
  988. end;
  989. Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
  990. begin
  991. Result:=IncSecond(Avalue,1);
  992. end;
  993. Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
  994. begin
  995. Result:=Result+ANumberOfMilliSeconds/MSecsPerDay;
  996. end;
  997. Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
  998. begin
  999. Result:=IncMilliSecond(AValue,1);
  1000. end;
  1001. { ---------------------------------------------------------------------
  1002. Encode/Decode of complete timestamp
  1003. ---------------------------------------------------------------------}
  1004. Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1005. begin
  1006. If Not TryEncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond,Result) then
  1007. InvalidDateTimeError(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond)
  1008. end;
  1009. Procedure DecodeDateTime(const AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
  1010. begin
  1011. DecodeDate(AValue,AYear,AMonth,ADay);
  1012. DecodeTime(AValue,AHour,AMinute,ASecond,AMilliSecond);
  1013. end;
  1014. Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AValue: TDateTime): Boolean;
  1015. Var
  1016. tmp : TDateTime;
  1017. begin
  1018. Result:=TryEncodeDate(AYear,AMonth,ADay,AValue);
  1019. Result:=Result and TryEncodeTime(AHour,AMinute,ASecond,Amillisecond,Tmp);
  1020. If Result then
  1021. Avalue:=AValue+Tmp;
  1022. end;
  1023. { ---------------------------------------------------------------------
  1024. Encode/decode date, specifying week of year and day of week
  1025. ---------------------------------------------------------------------}
  1026. Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  1027. begin
  1028. If Not TryEncodeDateWeek(AYear,AWeekOfYear,Result,ADayOfWeek) then
  1029. Raise Exception.CreateFmt('%d %d %d is not a valid dateweek',[AYear,AWeekOfYear,ADayOfWeek]);
  1030. end;
  1031. Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
  1032. begin
  1033. EncodeDateWeek(AYear,AWeekOfYear,1);
  1034. end;
  1035. Procedure DecodeDateWeek(const AValue: TDateTime; var AYear, AWeekOfYear, ADayOfWeek: Word);
  1036. begin
  1037. NotYetImplemented('DecodeDateWeek');
  1038. end;
  1039. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime; const ADayOfWeek: Word): Boolean;
  1040. Var
  1041. DOW : Word;
  1042. Rest : Integer;
  1043. begin
  1044. Result:=IsValidDateWeek(Ayear,AWeekOfYear,ADayOfWeek);
  1045. If Result then
  1046. begin
  1047. AValue:=EncodeDate(AYear,1,1)+(7*(AWeekOfYear-1));
  1048. DOW:=DayOfTheWeek(AValue);
  1049. Rest:=ADayOfWeek-DOW;
  1050. If (DOW>4) then
  1051. Inc(Rest,7);
  1052. AValue:=AValue+Rest;
  1053. end;
  1054. end;
  1055. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
  1056. begin
  1057. Result:=TryEncodeDateWeek(AYear,AWeekOfYear,AValue,1);
  1058. end;
  1059. { ---------------------------------------------------------------------
  1060. Encode/decode date, specifying day of year
  1061. ---------------------------------------------------------------------}
  1062. Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
  1063. begin
  1064. NotYetImplemented('EncodeDateDay');
  1065. end;
  1066. Procedure DecodeDateDay(const AValue: TDateTime; var AYear, ADayOfYear: Word);
  1067. begin
  1068. NotYetImplemented('DecodeDateDay');
  1069. end;
  1070. Function TryEncodeDateDay(const AYear, ADayOfYear: Word; var AValue: TDateTime): Boolean;
  1071. begin
  1072. NotYetImplemented('TryEncodeDateDay');
  1073. end;
  1074. { ---------------------------------------------------------------------
  1075. Encode/decode date, specifying week of month
  1076. ---------------------------------------------------------------------}
  1077. Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
  1078. begin
  1079. NotYetImplemented('EncodeDateMonthWeek');
  1080. end;
  1081. Procedure DecodeDateMonthWeek(const AValue: TDateTime; var AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  1082. begin
  1083. NotYetImplemented('DecodeDateMonthWeek');
  1084. end;
  1085. Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
  1086. begin
  1087. NotYetImplemented('TryEncodeDateMonthWeek');
  1088. end;
  1089. { ---------------------------------------------------------------------
  1090. Replace given element with supplied value.
  1091. ---------------------------------------------------------------------}
  1092. Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
  1093. begin
  1094. NotYetImplemented('RecodeYear');
  1095. end;
  1096. Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
  1097. begin
  1098. NotYetImplemented('RecodeMonth');
  1099. end;
  1100. Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
  1101. begin
  1102. NotYetImplemented('RecodeDay');
  1103. end;
  1104. Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
  1105. begin
  1106. NotYetImplemented('RecodeHour');
  1107. end;
  1108. Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
  1109. begin
  1110. NotYetImplemented('RecodeMinute');
  1111. end;
  1112. Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
  1113. begin
  1114. NotYetImplemented('RecodeSecond');
  1115. end;
  1116. Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
  1117. begin
  1118. NotYetImplemented('RecodeMilliSecond');
  1119. end;
  1120. Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
  1121. begin
  1122. NotYetImplemented('RecodeDate');
  1123. end;
  1124. Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1125. begin
  1126. NotYetImplemented('RecodeTime');
  1127. end;
  1128. Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1129. begin
  1130. NotYetImplemented('RecodeDateTime');
  1131. end;
  1132. Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AResult: TDateTime): Boolean;
  1133. begin
  1134. NotYetImplemented('TryRecodeDateTime');
  1135. end;
  1136. { ---------------------------------------------------------------------
  1137. Comparision of date/time
  1138. ---------------------------------------------------------------------}
  1139. Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
  1140. begin
  1141. NotYetImplemented('CompareDateTime');
  1142. end;
  1143. Function CompareDate(const A, B: TDateTime): TValueRelationship;
  1144. begin
  1145. NotYetImplemented('CompareDate');
  1146. end;
  1147. Function CompareTime(const A, B: TDateTime): TValueRelationship;
  1148. begin
  1149. NotYetImplemented('NotYetImplemented');
  1150. end;
  1151. Function SameDateTime(const A, B: TDateTime): Boolean;
  1152. begin
  1153. NotYetImplemented('SameDateTime');
  1154. end;
  1155. Function SameDate(const A, B: TDateTime): Boolean;
  1156. begin
  1157. NotYetImplemented('SameDate');
  1158. end;
  1159. Function SameTime(const A, B: TDateTime): Boolean;
  1160. begin
  1161. NotYetImplemented('SameTime');
  1162. end;
  1163. Function NthDayOfWeek(const AValue: TDateTime): Word;
  1164. begin
  1165. NotYetImplemented('NthDayOfWeek');
  1166. end;
  1167. Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; var AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  1168. begin
  1169. NotYetImplemented('DecodeDayOfWeekInMonth');
  1170. end;
  1171. Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
  1172. begin
  1173. NotYetImplemented('EncodeDayOfWeekInMonth');
  1174. end;
  1175. Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
  1176. begin
  1177. NotYetImplemented('TryEncodeDayOfWeekInMonth');
  1178. end;
  1179. { ---------------------------------------------------------------------
  1180. Exception throwing routines
  1181. ---------------------------------------------------------------------}
  1182. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
  1183. begin
  1184. NotYetImplemented('InvalidDateTimeError');
  1185. end;
  1186. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
  1187. begin
  1188. NotYetImplemented('InvalidDateTimeError');
  1189. end;
  1190. Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
  1191. begin
  1192. NotYetImplemented('InvalidDateWeekError');
  1193. end;
  1194. Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
  1195. begin
  1196. NotYetImplemented('InvalidDateDayError');
  1197. end;
  1198. Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  1199. begin
  1200. NotYetImplemented('InvalidDateMonthWeekError');
  1201. end;
  1202. Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  1203. begin
  1204. NotYetImplemented('InvalidDayOfWeekInMonthError');
  1205. end;
  1206. { ---------------------------------------------------------------------
  1207. Julian and Modified Julian Date conversion support
  1208. ---------------------------------------------------------------------}
  1209. Function DateTimeToJulianDate(const AValue: TDateTime): Double;
  1210. begin
  1211. NotYetImplemented('DateTimeToJulianDate');
  1212. end;
  1213. Function JulianDateToDateTime(const AValue: Double): TDateTime;
  1214. begin
  1215. NotYetImplemented('JulianDateToDateTime');
  1216. end;
  1217. Function TryJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
  1218. begin
  1219. NotYetImplemented('TryJulianDateToDateTime');
  1220. end;
  1221. Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
  1222. begin
  1223. NotYetImplemented('DateTimeToModifiedJulianDate');
  1224. end;
  1225. Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
  1226. begin
  1227. NotYetImplemented('ModifiedJulianDateToDateTime');
  1228. end;
  1229. Function TryModifiedJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
  1230. begin
  1231. NotYetImplemented('TryModifiedJulianDateToDateTime');
  1232. end;
  1233. { ---------------------------------------------------------------------
  1234. Unix timestamp support.
  1235. ---------------------------------------------------------------------}
  1236. Function DateTimeToUnix(const AValue: TDateTime): Int64;
  1237. begin
  1238. NotYetImplemented('DateTimeToUnix');
  1239. end;
  1240. Function UnixToDateTime(const AValue: Int64): TDateTime;
  1241. begin
  1242. NotYetImplemented('UnixToDateTime');
  1243. end;
  1244. end.
  1245. {
  1246. $Log$
  1247. Revision 1.1 2003-01-19 00:01:55 michael
  1248. + initial checkin.
  1249. }