dateutil.inc 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. This file is part of the Free Pascal run time library.
  5. Copyright (c) 1999-2000 by the Free Pascal development team
  6. Delphi/Kylix compatibility unit, provides Date/Time handling routines.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit dateutils;
  14. interface
  15. uses
  16. SysUtils, Math, Types;
  17. { ---------------------------------------------------------------------
  18. Various constants
  19. ---------------------------------------------------------------------}
  20. const
  21. DaysPerWeek = 7;
  22. WeeksPerFortnight = 2;
  23. MonthsPerYear = 12;
  24. YearsPerDecade = 10;
  25. YearsPerCentury = 100;
  26. YearsPerMillennium = 1000;
  27. // ISO day numbers.
  28. DayMonday = 1;
  29. DayTuesday = 2;
  30. DayWednesday = 3;
  31. DayThursday = 4;
  32. DayFriday = 5;
  33. DaySaturday = 6;
  34. DaySunday = 7;
  35. // Fraction of a day
  36. OneHour = 1/HoursPerDay;
  37. OneMinute = 1/MinsPerDay;
  38. OneSecond = 1/SecsPerDay;
  39. OneMillisecond = 1/MSecsPerDay;
  40. { This is actual days per year but you need to know if it's a leap year}
  41. DaysPerYear: array [Boolean] of Word = (365, 366);
  42. { Used in RecodeDate, RecodeTime and RecodeDateTime for those datetime }
  43. { fields you want to leave alone }
  44. RecodeLeaveFieldAsIs = High(Word);
  45. { ---------------------------------------------------------------------
  46. Global variables used in this unit
  47. ---------------------------------------------------------------------}
  48. Const
  49. { Average over a 4 year span. Valid for next 100 years }
  50. ApproxDaysPerMonth: Double = 30.4375;
  51. ApproxDaysPerYear: Double = 365.25;
  52. { ---------------------------------------------------------------------
  53. Simple trimming functions.
  54. ---------------------------------------------------------------------}
  55. Function DateOf(const AValue: TDateTime): TDateTime;
  56. Function TimeOf(const AValue: TDateTime): TDateTime;
  57. { ---------------------------------------------------------------------
  58. Identification functions.
  59. ---------------------------------------------------------------------}
  60. Function IsInLeapYear(const AValue: TDateTime): Boolean;
  61. Function IsPM(const AValue: TDateTime): Boolean;
  62. Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
  63. Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  64. Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  65. Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
  66. Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
  67. Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
  68. { ---------------------------------------------------------------------
  69. Enumeration functions.
  70. ---------------------------------------------------------------------}
  71. Function WeeksInYear(const AValue: TDateTime): Word;
  72. Function WeeksInAYear(const AYear: Word): Word;
  73. Function DaysInYear(const AValue: TDateTime): Word;
  74. Function DaysInAYear(const AYear: Word): Word;
  75. Function DaysInMonth(const AValue: TDateTime): Word;
  76. Function DaysInAMonth(const AYear, AMonth: Word): Word;
  77. { ---------------------------------------------------------------------
  78. Variations on current date/time.
  79. ---------------------------------------------------------------------}
  80. Function Today: TDateTime;
  81. Function Yesterday: TDateTime;
  82. Function Tomorrow: TDateTime;
  83. Function IsToday(const AValue: TDateTime): Boolean;
  84. Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
  85. Function PreviousDayOfWeek (DayOfWeek : Word) : Word;
  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. uses sysconst;
  315. { ---------------------------------------------------------------------
  316. Auxiliary routines
  317. ---------------------------------------------------------------------}
  318. Procedure NotYetImplemented (FN : String);
  319. begin
  320. Raise Exception.CreateFmt('Function "%s" (dateutils) is not yet implemented',[FN]);
  321. end;
  322. { ---------------------------------------------------------------------
  323. Simple trimming functions.
  324. ---------------------------------------------------------------------}
  325. Function DateOf(const AValue: TDateTime): TDateTime;
  326. begin
  327. Result:=Trunc(AValue);
  328. end;
  329. Function TimeOf(const AValue: TDateTime): TDateTime;
  330. begin
  331. Result:=Frac(Avalue);
  332. end;
  333. { ---------------------------------------------------------------------
  334. Identification functions.
  335. ---------------------------------------------------------------------}
  336. Function IsInLeapYear(const AValue: TDateTime): Boolean;
  337. Var
  338. D,Y,M : Word;
  339. begin
  340. DecodeDate(AValue,Y,M,D);
  341. Result:=IsLeapYear(Y);
  342. end;
  343. Function IsPM(const AValue: TDateTime): Boolean;
  344. Var
  345. H,M,S,MS : Word;
  346. begin
  347. DecodeTime(AValue,H,M,S,MS);
  348. Result:=(H>=12);
  349. end;
  350. Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
  351. begin
  352. Result:=(AYear<>0) and (AYear<10000)
  353. and (AMonth in [1..12])
  354. and (ADay<>0) and (ADay<=MonthDays[IsleapYear(AYear),AMonth]);
  355. end;
  356. Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  357. begin
  358. Result:=(AHour=HoursPerDay) and (AMinute=0) and (ASecond=0) and (AMillisecond=0);
  359. Result:=Result or
  360. ((AHour<HoursPerDay) and (AMinute<MinsPerHour) and (ASecond<SecsPerMin) and
  361. (AMillisecond<MSecsPerSec));
  362. end;
  363. Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  364. begin
  365. Result:=IsValidDate(AYear,AMonth,ADay) and
  366. IsValidTime(AHour,AMinute,ASecond,AMillisecond)
  367. end;
  368. Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
  369. begin
  370. Result:=(AYear<>0) and (ADayOfYear<>0) and (AYear<10000) and
  371. (ADayOfYear<=DaysPerYear[IsLeapYear(AYear)]);
  372. end;
  373. Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
  374. begin
  375. Result:=(AYear<>0) and (AYear<10000)
  376. and (ADayOfWeek in [1..7])
  377. and (AWeekOfYear<>0)
  378. and (AWeekOfYear<=WeeksInaYear(AYear));
  379. { should we not also check whether the day of the week is not
  380. larger than the last day of the last week in the year 9999 ?? }
  381. end;
  382. Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
  383. begin
  384. Result:=(AYear<>0) and (AYear<10000)
  385. and (AMonth in [1..12])
  386. and (AWeekOfMonth in [1..5])
  387. and (ADayOfWeek in [1..7]);
  388. end;
  389. { ---------------------------------------------------------------------
  390. Enumeration functions.
  391. ---------------------------------------------------------------------}
  392. Function WeeksInYear(const AValue: TDateTime): Word;
  393. Var
  394. Y,M,D : Word;
  395. begin
  396. DecodeDate(AValue,Y,M,D);
  397. Result:=WeeksInAYear(Y);
  398. end;
  399. Function WeeksInAYear(const AYear: Word): Word;
  400. Var
  401. DOW : Word;
  402. begin
  403. Result:=52;
  404. DOW:=DayOfTheWeek(StartOfAYear(AYear));
  405. If (DOW=4) or ((DOW=3) and IsLeapYear(AYear)) then
  406. Inc(Result);
  407. end;
  408. Function DaysInYear(const AValue: TDateTime): Word;
  409. Var
  410. Y,M,D : Word;
  411. begin
  412. DecodeDate(AValue,Y,M,D);
  413. Result:=DaysPerYear[IsLeapYear(Y)];
  414. end;
  415. Function DaysInAYear(const AYear: Word): Word;
  416. begin
  417. Result:=DaysPerYear[Isleapyear(AYear)];
  418. end;
  419. Function DaysInMonth(const AValue: TDateTime): Word;
  420. Var
  421. Y,M,D : Word;
  422. begin
  423. Decodedate(AValue,Y,M,D);
  424. Result:=MonthDays[IsLeapYear(Y),M];
  425. end;
  426. Function DaysInAMonth(const AYear, AMonth: Word): Word;
  427. begin
  428. Result:=MonthDays[IsLeapYear(AYear),AMonth];
  429. end;
  430. { ---------------------------------------------------------------------
  431. Variations on current date/time.
  432. ---------------------------------------------------------------------}
  433. Function Today: TDateTime;
  434. begin
  435. Result:=Date;
  436. end;
  437. Function Yesterday: TDateTime;
  438. begin
  439. Result:=Date-1;
  440. end;
  441. Function Tomorrow: TDateTime;
  442. begin
  443. Result:=Date+1;
  444. end;
  445. Function IsToday(const AValue: TDateTime): Boolean;
  446. begin
  447. Result:=IsSameDay(AValue,Date);
  448. end;
  449. Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
  450. Var
  451. D : TDateTime;
  452. begin
  453. D:=AValue-Trunc(ABasis);
  454. Result:=(D>=0) and (D<1);
  455. end;
  456. const
  457. DOWMap: array [1..7] of Word = (7, 1, 2, 3, 4, 5, 6);
  458. Function PreviousDayOfWeek (DayOfWeek : Word) : Word;
  459. begin
  460. If Not (DayOfWeek in [1..7]) then
  461. Raise EConvertError.CreateFmt(SErrInvalidDayOfWeek,[DayOfWeek]);
  462. Result:=DOWMap[DayOfWeek];
  463. end;
  464. { ---------------------------------------------------------------------
  465. Extraction functions.
  466. ---------------------------------------------------------------------}
  467. Function YearOf(const AValue: TDateTime): Word;
  468. Var
  469. D,M : Word;
  470. begin
  471. DecodeDate(AValue,Result,D,M);
  472. end;
  473. Function MonthOf(const AValue: TDateTime): Word;
  474. Var
  475. Y,D : Word;
  476. begin
  477. DecodeDate(AValue,Y,Result,D);
  478. end;
  479. Function WeekOf(const AValue: TDateTime): Word;
  480. begin
  481. Result:=WeekOfTheYear(AValue);
  482. end;
  483. Function DayOf(const AValue: TDateTime): Word;
  484. Var
  485. Y,M : Word;
  486. begin
  487. DecodeDate(AValue,Y,M,Result);
  488. end;
  489. Function HourOf(const AValue: TDateTime): Word;
  490. Var
  491. N,S,MS : Word;
  492. begin
  493. DecodeTime(AValue,Result,N,S,MS);
  494. end;
  495. Function MinuteOf(const AValue: TDateTime): Word;
  496. Var
  497. H,S,MS : Word;
  498. begin
  499. DecodeTime(AValue,H,Result,S,MS);
  500. end;
  501. Function SecondOf(const AValue: TDateTime): Word;
  502. Var
  503. H,N,MS : Word;
  504. begin
  505. DecodeTime(AVAlue,H,N,Result,MS);
  506. end;
  507. Function MilliSecondOf(const AValue: TDateTime): Word;
  508. Var
  509. H,N,S : Word;
  510. begin
  511. DecodeTime(AValue,H,N,S,Result);
  512. end;
  513. { ---------------------------------------------------------------------
  514. Start/End of year functions.
  515. ---------------------------------------------------------------------}
  516. Function StartOfTheYear(const AValue: TDateTime): TDateTime;
  517. Var
  518. Y,M,D : Word;
  519. begin
  520. DecodeDate(AValue,Y,M,D);
  521. Result:=EncodeDate(Y,1,1);
  522. end;
  523. Function EndOfTheYear(const AValue: TDateTime): TDateTime;
  524. Var
  525. Y,M,D : Word;
  526. begin
  527. DecodeDate(AValue,Y,M,D);
  528. Result:=EncodeDateTime(Y,12,31,23,59,59,999);
  529. end;
  530. Function StartOfAYear(const AYear: Word): TDateTime;
  531. begin
  532. Result:=EncodeDate(AYear,1,1);
  533. end;
  534. Function EndOfAYear(const AYear: Word): TDateTime;
  535. begin
  536. Result:=(EncodeDateTime(AYear,12,31,23,59,59,999));
  537. end;
  538. { ---------------------------------------------------------------------
  539. Start/End of month functions.
  540. ---------------------------------------------------------------------}
  541. Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
  542. Var
  543. Y,M,D : Word;
  544. begin
  545. DecodeDate(AValue,Y,M,D);
  546. Result:=EncodeDate(Y,M,1);
  547. // MonthDays[IsLeapYear(Y),M])
  548. end;
  549. Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
  550. Var
  551. Y,M,D : Word;
  552. begin
  553. DecodeDate(AValue,Y,M,D);
  554. Result:=EncodeDateTime(Y,M,MonthDays[IsLeapYear(Y),M],23,59,59,999);
  555. end;
  556. Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
  557. begin
  558. Result:=EncodeDate(AYear,AMonth,1);
  559. end;
  560. Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
  561. begin
  562. Result:=EncodeDateTime(AYear,AMonth,MonthDays[IsLeapYear(AYear),AMonth],23,59,59,999);
  563. end;
  564. { ---------------------------------------------------------------------
  565. Start/End of week functions.
  566. ---------------------------------------------------------------------}
  567. Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
  568. begin
  569. Result:=Trunc(AValue)-DayOfTheWeek(AValue)+1;
  570. end;
  571. Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
  572. begin
  573. Result:=EndOfTheDay(AValue-DayOfTheWeek(AValue)+7);
  574. end;
  575. Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  576. begin
  577. Result:=EncodeDateWeek(AYear,AWeekOfYear,ADayOfWeek);
  578. end;
  579. Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
  580. begin
  581. Result:=StartOfAWeek(AYear,AWeekOfYear,1)
  582. end;
  583. Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  584. begin
  585. Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
  586. end;
  587. Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
  588. begin
  589. Result:=EndOfAWeek(AYear,AWeekOfYear,7);
  590. end;
  591. { ---------------------------------------------------------------------
  592. Start/End of day functions.
  593. ---------------------------------------------------------------------}
  594. Function StartOfTheDay(const AValue: TDateTime): TDateTime;
  595. begin
  596. StartOfTheDay:=Trunc(Avalue);
  597. end;
  598. Function EndOfTheDay(const AValue: TDateTime): TDateTime;
  599. Var
  600. Y,M,D : Word;
  601. begin
  602. DecodeDate(AValue,Y,M,D);
  603. Result:=EncodeDateTime(Y,M,D,23,59,59,999);
  604. end;
  605. Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime;
  606. begin
  607. Result:=EncodeDate(AYear,AMonth,ADay);
  608. end;
  609. Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime;
  610. begin
  611. Result:=StartOfAYear(AYear)+ADayOfYear-1;
  612. end;
  613. Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime;
  614. begin
  615. Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
  616. end;
  617. Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime;
  618. begin
  619. Result:=StartOfAYear(AYear)+ADayOfYear-1+EncodeTime(23,59,59,999);
  620. end;
  621. { ---------------------------------------------------------------------
  622. Part of year functions.
  623. ---------------------------------------------------------------------}
  624. Function MonthOfTheYear(const AValue: TDateTime): Word;
  625. Var
  626. Y,D : Word;
  627. begin
  628. DecodeDate(AValue,Y,Result,D);
  629. end;
  630. Function WeekOfTheYear(const AValue: TDateTime): Word;
  631. Var
  632. Y,DOW : Word;
  633. begin
  634. DecodeDateWeek(AValue,Y,Result,DOW)
  635. end;
  636. Function WeekOfTheYear(const AValue: TDateTime; var AYear: Word): Word;
  637. Var
  638. DOW : Word;
  639. begin
  640. DecodeDateWeek(AValue,AYear,Result,DOW);
  641. end;
  642. Function DayOfTheYear(const AValue: TDateTime): Word;
  643. begin
  644. Result:=Trunc(AValue-StartOfTheYear(AValue)+1);
  645. end;
  646. Function HourOfTheYear(const AValue: TDateTime): Word;
  647. Var
  648. H,M,S,MS : Word;
  649. begin
  650. DecodeTime(AValue,H,M,S,MS);
  651. Result:=H+((DayOfTheYear(AValue)-1)*24);
  652. end;
  653. Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
  654. Var
  655. H,M,S,MS : Word;
  656. begin
  657. DecodeTime(AValue,H,M,S,MS);
  658. Result:=M+(H+((DayOfTheYear(AValue)-1)*24))*60;
  659. end;
  660. Function SecondOfTheYear(const AValue: TDateTime): LongWord;
  661. Var
  662. H,M,S,MS : Word;
  663. begin
  664. DecodeTime(AValue,H,M,S,MS);
  665. Result:=(M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S;
  666. end;
  667. Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
  668. Var
  669. H,M,S,MS : Word;
  670. begin
  671. DecodeTime(AValue,H,M,S,MS);
  672. Result:=((M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S)*1000+MS;
  673. end;
  674. { ---------------------------------------------------------------------
  675. Part of month functions.
  676. ---------------------------------------------------------------------}
  677. Function WeekOfTheMonth(const AValue: TDateTime): Word;
  678. var
  679. Y,M,DOW : word;
  680. begin
  681. DecodeDateMonthWeek(AValue,Y,M,Result,DOW);
  682. end;
  683. Function WeekOfTheMonth(const AValue: TDateTime; var AYear, AMonth: Word): Word;
  684. Var
  685. DOW : Word;
  686. begin
  687. DecodeDateMonthWeek(AValue,AYear,AMonth,Result,DOW);
  688. end;
  689. Function DayOfTheMonth(const AValue: TDateTime): Word;
  690. Var
  691. Y,M : Word;
  692. begin
  693. DecodeDate(AValue,Y,M,Result);
  694. end;
  695. Function HourOfTheMonth(const AValue: TDateTime): Word;
  696. Var
  697. Y,M,D,H,N,S,MS : Word;
  698. begin
  699. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  700. Result:=(D-1)*24+H;
  701. end;
  702. Function MinuteOfTheMonth(const AValue: TDateTime): Word;
  703. Var
  704. Y,M,D,H,N,S,MS : Word;
  705. begin
  706. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  707. Result:=((D-1)*24+H)*60+N;
  708. end;
  709. Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
  710. Var
  711. Y,M,D,H,N,S,MS : Word;
  712. begin
  713. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  714. Result:=(((D-1)*24+H)*60+N)*60+S;
  715. end;
  716. Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
  717. Var
  718. Y,M,D,H,N,S,MS : Word;
  719. begin
  720. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  721. Result:=((((D-1)*24+H)*60+N)*60+S)*1000+MS;
  722. end;
  723. { ---------------------------------------------------------------------
  724. Part of week functions.
  725. ---------------------------------------------------------------------}
  726. Function DayOfTheWeek(const AValue: TDateTime): Word;
  727. begin
  728. Result:=DowMAP[DayOfWeek(AValue)];
  729. end;
  730. Function HourOfTheWeek(const AValue: TDateTime): Word;
  731. Var
  732. H,M,S,MS : Word;
  733. begin
  734. DecodeTime(AValue,H,M,S,MS);
  735. Result:=(DayOfTheWeek(AValue)-1)*24+H;
  736. end;
  737. Function MinuteOfTheWeek(const AValue: TDateTime): Word;
  738. Var
  739. H,M,S,MS : Word;
  740. begin
  741. DecodeTime(AValue,H,M,S,MS);
  742. Result:=((DayOfTheWeek(AValue)-1)*24+H)*60+M;
  743. end;
  744. Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
  745. Var
  746. H,M,S,MS : Word;
  747. begin
  748. DecodeTime(AValue,H,M,S,MS);
  749. Result:=(((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S;
  750. end;
  751. Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
  752. Var
  753. H,M,S,MS : Word;
  754. begin
  755. DecodeTime(AValue,H,M,S,MS);
  756. Result:=((((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S)*1000+MS;
  757. end;
  758. { ---------------------------------------------------------------------
  759. Part of day functions.
  760. ---------------------------------------------------------------------}
  761. Function HourOfTheDay(const AValue: TDateTime): Word;
  762. Var
  763. M,S,MS : Word;
  764. begin
  765. DecodeTime(AValue,Result,M,S,MS);
  766. end;
  767. Function MinuteOfTheDay(const AValue: TDateTime): Word;
  768. Var
  769. H,M,S,MS : Word;
  770. begin
  771. DecodeTime(AValue,H,M,S,MS);
  772. Result:=(H*60)+M;
  773. end;
  774. Function SecondOfTheDay(const AValue: TDateTime): LongWord;
  775. Var
  776. H,M,S,MS : Word;
  777. begin
  778. DecodeTime(AValue,H,M,S,MS);
  779. Result:=((H*60)+M)*60+S;
  780. end;
  781. Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
  782. Var
  783. H,M,S,MS : Word;
  784. begin
  785. DecodeTime(AValue,H,M,S,MS);
  786. Result:=(((H*60)+M)*60+S)*1000+MS;
  787. end;
  788. { ---------------------------------------------------------------------
  789. Part of hour functions.
  790. ---------------------------------------------------------------------}
  791. Function MinuteOfTheHour(const AValue: TDateTime): Word;
  792. Var
  793. H,S,MS : Word;
  794. begin
  795. DecodeTime(AValue,H,Result,S,MS);
  796. end;
  797. Function SecondOfTheHour(const AValue: TDateTime): Word;
  798. Var
  799. H,S,M,MS : Word;
  800. begin
  801. DecodeTime(AValue,H,M,S,MS);
  802. Result:=M*60+S;
  803. end;
  804. Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
  805. Var
  806. H,S,M,MS : Word;
  807. begin
  808. DecodeTime(AValue,H,M,S,MS);
  809. Result:=(M*60+S)*1000+MS;
  810. end;
  811. { ---------------------------------------------------------------------
  812. Part of minute functions.
  813. ---------------------------------------------------------------------}
  814. Function SecondOfTheMinute(const AValue: TDateTime): Word;
  815. Var
  816. H,M,MS : Word;
  817. begin
  818. DecodeTime(AValue,H,M,Result,MS);
  819. end;
  820. Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
  821. Var
  822. H,S,M,MS : Word;
  823. begin
  824. DecodeTime(AValue,H,M,S,MS);
  825. Result:=S*1000+MS;
  826. end;
  827. { ---------------------------------------------------------------------
  828. Part of second functions.
  829. ---------------------------------------------------------------------}
  830. Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
  831. Var
  832. H,M,S : Word;
  833. begin
  834. DecodeTime(AValue,H,M,S,Result);
  835. end;
  836. { ---------------------------------------------------------------------
  837. Range checking functions.
  838. ---------------------------------------------------------------------}
  839. Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
  840. begin
  841. Result:=YearsBetween(ANow,AThen)<=AYears;
  842. end;
  843. Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
  844. begin
  845. Result:=MonthsBetween(ANow,AThen)<=AMonths;
  846. end;
  847. Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
  848. begin
  849. Result:=WeeksBetween(ANow,AThen)<=AWeeks;
  850. end;
  851. Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
  852. begin
  853. Result:=DaysBetween(ANow,AThen)<=ADays;
  854. end;
  855. Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
  856. begin
  857. Result:=HoursBetween(ANow,AThen)<=AHours;
  858. end;
  859. Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
  860. begin
  861. Result:=MinutesBetween(ANow,AThen)<=AMinutes;
  862. end;
  863. Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
  864. begin
  865. Result:=SecondsBetween(ANow,Athen)<=ASeconds;
  866. end;
  867. Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
  868. begin
  869. Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
  870. end;
  871. { ---------------------------------------------------------------------
  872. Period functions.
  873. ---------------------------------------------------------------------}
  874. {
  875. These functions are declared as approximate by Borland.
  876. A bit strange, since it can be calculated exactly ?
  877. }
  878. Function YearsBetween(const ANow, AThen: TDateTime): Integer;
  879. begin
  880. Result:=Trunc(Abs(ANow-AThen)/ApproxDaysPerYear);
  881. end;
  882. Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
  883. begin
  884. Result:=Trunc(Abs(ANow-Athen)/ApproxDaysPerMonth);
  885. end;
  886. Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
  887. begin
  888. Result:=Trunc(Abs(ANow-AThen)) div 7;
  889. end;
  890. Function DaysBetween(const ANow, AThen: TDateTime): Integer;
  891. begin
  892. Result:=Trunc(Abs(ANow-AThen));
  893. end;
  894. Function HoursBetween(const ANow, AThen: TDateTime): Int64;
  895. begin
  896. Result:=Trunc(Abs(ANow-AThen)*HoursPerDay);
  897. end;
  898. Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
  899. begin
  900. Result:=Trunc(Abs(ANow-AThen)*MinsPerDay);
  901. end;
  902. Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
  903. begin
  904. Result:=Trunc(Abs(ANow-AThen)*SecsPerDay);
  905. end;
  906. Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
  907. begin
  908. Result:=Trunc(Abs(ANow-AThen)*MSecsPerDay);
  909. end;
  910. { ---------------------------------------------------------------------
  911. Timespan in xxx functions.
  912. ---------------------------------------------------------------------}
  913. Function YearSpan(const ANow, AThen: TDateTime): Double;
  914. begin
  915. Result:=Abs(Anow-Athen)/ApproxDaysPerYear;
  916. end;
  917. Function MonthSpan(const ANow, AThen: TDateTime): Double;
  918. begin
  919. Result:=Abs(ANow-AThen)/ApproxDaysPerMonth;
  920. end;
  921. Function WeekSpan(const ANow, AThen: TDateTime): Double;
  922. begin
  923. Result:=Abs(ANow-AThen) / 7
  924. end;
  925. Function DaySpan(const ANow, AThen: TDateTime): Double;
  926. begin
  927. Result:=Abs(ANow-AThen);
  928. end;
  929. Function HourSpan(const ANow, AThen: TDateTime): Double;
  930. begin
  931. Result:=Abs(ANow-AThen)*HoursPerDay;
  932. end;
  933. Function MinuteSpan(const ANow, AThen: TDateTime): Double;
  934. begin
  935. Result:=Abs(ANow-AThen)*MinsPerDay;
  936. end;
  937. Function SecondSpan(const ANow, AThen: TDateTime): Double;
  938. begin
  939. Result:=Abs(ANow-AThen)*SecsPerDay;
  940. end;
  941. Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
  942. begin
  943. Result:=Abs(ANow-AThen)*MSecsPerDay;
  944. end;
  945. { ---------------------------------------------------------------------
  946. Increment/decrement functions.
  947. ---------------------------------------------------------------------}
  948. Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
  949. Var
  950. Y,M,D,H,N,S,MS : Word;
  951. begin
  952. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  953. Y:=Y+ANumberOfYears;
  954. If (M=2) and (D=29) And (Not IsLeapYear(Y)) then
  955. D:=28;
  956. Result:=EncodeDateTime(Y,M,D,H,N,S,MS);
  957. end;
  958. Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
  959. begin
  960. Result:=IncYear(Avalue,1);
  961. end;
  962. Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
  963. begin
  964. Result:=AValue+ANumberOfWeeks*7;
  965. end;
  966. Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
  967. begin
  968. Result:=IncWeek(Avalue,1);
  969. end;
  970. Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
  971. begin
  972. Result:=AValue+ANumberOfDays;
  973. end;
  974. Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
  975. begin
  976. Result:=IncDay(Avalue,1);
  977. end;
  978. Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
  979. begin
  980. Result:=AValue+ANumberOfHours/HoursPerDay;
  981. end;
  982. Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
  983. begin
  984. Result:=IncHour(AValue,1);
  985. end;
  986. Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
  987. begin
  988. Result:=AValue+ANumberOfMinutes / MinsPerDay;
  989. end;
  990. Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
  991. begin
  992. Result:=IncMinute(AValue,1);
  993. end;
  994. Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
  995. begin
  996. Result:=AValue+ANumberOfSeconds / SecsPerDay;
  997. end;
  998. Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
  999. begin
  1000. Result:=IncSecond(Avalue,1);
  1001. end;
  1002. Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
  1003. begin
  1004. Result:=AValue+ANumberOfMilliSeconds/MSecsPerDay;
  1005. end;
  1006. Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
  1007. begin
  1008. Result:=IncMilliSecond(AValue,1);
  1009. end;
  1010. { ---------------------------------------------------------------------
  1011. Encode/Decode of complete timestamp
  1012. ---------------------------------------------------------------------}
  1013. Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1014. begin
  1015. If Not TryEncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond,Result) then
  1016. InvalidDateTimeError(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond)
  1017. end;
  1018. Procedure DecodeDateTime(const AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
  1019. begin
  1020. DecodeDate(AValue,AYear,AMonth,ADay);
  1021. DecodeTime(AValue,AHour,AMinute,ASecond,AMilliSecond);
  1022. end;
  1023. Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AValue: TDateTime): Boolean;
  1024. Var
  1025. tmp : TDateTime;
  1026. begin
  1027. Result:=TryEncodeDate(AYear,AMonth,ADay,AValue);
  1028. Result:=Result and TryEncodeTime(AHour,AMinute,ASecond,Amillisecond,Tmp);
  1029. If Result then
  1030. Avalue:=AValue+Tmp;
  1031. end;
  1032. { ---------------------------------------------------------------------
  1033. Encode/decode date, specifying week of year and day of week
  1034. ---------------------------------------------------------------------}
  1035. Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  1036. begin
  1037. If Not TryEncodeDateWeek(AYear,AWeekOfYear,Result,ADayOfWeek) then
  1038. InvalidDateWeekError(AYear,AWeekOfYear,ADayOfWeek);
  1039. end;
  1040. Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
  1041. begin
  1042. Result := EncodeDateWeek(AYear,AWeekOfYear,1);
  1043. end;
  1044. Procedure DecodeDateWeek(const AValue: TDateTime; var AYear, AWeekOfYear, ADayOfWeek: Word);
  1045. var
  1046. DOY : Integer;
  1047. D: Word;
  1048. YS : TDateTime;
  1049. YSDOW, YEDOW: Word;
  1050. begin
  1051. AYear:=YearOf(AValue);
  1052. // Correct to ISO DOW
  1053. ADayOfWeek:=DayOfWeek(AValue)-1;
  1054. If ADAyOfWeek=0 then
  1055. ADayofweek:=7;
  1056. YS:=StartOfAYear(AYear);
  1057. DOY:=Trunc(AValue-YS)+1;
  1058. YSDOW:=DayOfTheWeek(YS);
  1059. // Correct week if later than wednesday. First week never starts later than wednesday
  1060. if (YSDOW<5) then
  1061. Inc(DOY,YSDOW-1)
  1062. else
  1063. Dec(DOY,8-YSDOW);
  1064. if (DOY<=0) then // Day is in last week of previous year.
  1065. DecodeDateWeek(YS-1,AYear,AWeekOfYear,D)
  1066. else
  1067. begin
  1068. AWeekOfYear:=DOY div 7;
  1069. if ((DOY mod 7)<>0) then
  1070. Inc(AWeekOfYear);
  1071. if (AWeekOfYear>52) then // Maybe in first week of next year ?
  1072. begin
  1073. YEDOW:=YSDOW;
  1074. if IsLeapYear(AYear) then
  1075. begin
  1076. Inc(YEDOW);
  1077. if (YEDOW>7) then
  1078. YEDOW:=1
  1079. else
  1080. end;
  1081. if (YEDOW<4) then // Really next year.
  1082. begin
  1083. Inc(AYear);
  1084. AWeekOfYear:=1;
  1085. end;
  1086. end;
  1087. end;
  1088. end;
  1089. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime; const ADayOfWeek: Word): Boolean;
  1090. Var
  1091. DOW : Word;
  1092. Rest : Integer;
  1093. begin
  1094. Result:=IsValidDateWeek(Ayear,AWeekOfYear,ADayOfWeek);
  1095. If Result then
  1096. begin
  1097. AValue:=EncodeDate(AYear,1,1)+(7*(AWeekOfYear-1));
  1098. DOW:=DayOfTheWeek(AValue);
  1099. Rest:=ADayOfWeek-DOW;
  1100. If (DOW>4) then
  1101. Inc(Rest,7);
  1102. AValue:=AValue+Rest;
  1103. end;
  1104. end;
  1105. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
  1106. begin
  1107. Result:=TryEncodeDateWeek(AYear,AWeekOfYear,AValue,1);
  1108. end;
  1109. { ---------------------------------------------------------------------
  1110. Encode/decode date, specifying day of year
  1111. ---------------------------------------------------------------------}
  1112. Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
  1113. begin
  1114. If Not TryEncodeDateDay(AYear,ADayOfYear,Result) then
  1115. InvalidDateDayError(AYear,ADayOfYear);
  1116. end;
  1117. Procedure DecodeDateDay(const AValue: TDateTime; var AYear, ADayOfYear: Word);
  1118. Var
  1119. M,D : Word;
  1120. begin
  1121. DecodeDate(AValue,AYear,M,D);
  1122. ADayOfyear:=Trunc(AValue-EncodeDate(AYear,1,1))+1;
  1123. end;
  1124. Function TryEncodeDateDay(const AYear, ADayOfYear: Word; var AValue: TDateTime): Boolean;
  1125. begin
  1126. Result:=(ADayOfYear<>0) and (ADayOfYear<=DaysPerYear [IsleapYear(AYear)]);
  1127. If Result then
  1128. AValue:=EncodeDate(AYear,1,1)+ADayOfYear-1;
  1129. end;
  1130. { ---------------------------------------------------------------------
  1131. Encode/decode date, specifying week of month
  1132. ---------------------------------------------------------------------}
  1133. Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
  1134. begin
  1135. If Not TryEncodeDateMonthWeek(Ayear,AMonth,AWeekOfMonth,ADayOfWeek,Result) then
  1136. InvalidDateMonthWeekError(AYear,AMonth,AWeekOfMonth,ADayOfWeek);
  1137. end;
  1138. Procedure DecodeDateMonthWeek(const AValue: TDateTime; var AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  1139. Var
  1140. D,SDOM,EDOM : Word;
  1141. SOM : TdateTime;
  1142. DOM : Integer;
  1143. begin
  1144. DecodeDate(AValue,AYear,AMonth,D);
  1145. ADayOfWeek:=DayOfTheWeek(AValue);
  1146. SOM:=EncodeDate(Ayear,Amonth,1);
  1147. SDOM:=DayOfTheWeek(SOM);
  1148. DOM:=D-1+SDOM;
  1149. If SDOM>4 then
  1150. Dec(DOM,7);
  1151. // Too early in the month. First full week is next week, day is after thursday.
  1152. If DOM<=0 Then
  1153. DecodeDateMonthWeek(SOM-1,AYear,AMonth,AWeekOfMonth,D)
  1154. else
  1155. begin
  1156. AWeekOfMonth:=(DOM div 7)+Ord((DOM mod 7)<>0);
  1157. EDOM:=DayOfTheWeek(EndOfAMonth(Ayear,AMonth));
  1158. // In last days of last long week, so in next month...
  1159. If (EDOM<4) and ((DaysInAMonth(AYear,Amonth)-D)<EDOM) then
  1160. begin
  1161. AWeekOfMonth:=1;
  1162. Inc(AMonth);
  1163. If (AMonth=13) then
  1164. begin
  1165. AMonth:=1;
  1166. Inc(AYear);
  1167. end;
  1168. end;
  1169. end;
  1170. end;
  1171. Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
  1172. var
  1173. S : Word;
  1174. DOM : Integer;
  1175. begin
  1176. Result:=IsValidDateMonthWeek(AYear,AMonth,AWeekOfMonth,ADayOfWeek);
  1177. if Result then
  1178. begin
  1179. AValue:=EncodeDate(AYear,AMonth,1);
  1180. DOM:=(AWeekOfMonth-1)*7+ADayOfWeek-1;
  1181. { Correct for first week in last month.}
  1182. S:=DayOfTheWeek(AValue);
  1183. Dec(DOM,S-1);
  1184. if S in [DayFriday..DaySunday] then
  1185. Inc(DOM,7);
  1186. AValue:=AValue+DOM;
  1187. end;
  1188. end;
  1189. { ---------------------------------------------------------------------
  1190. Replace given element with supplied value.
  1191. ---------------------------------------------------------------------}
  1192. Const
  1193. LFAI = RecodeLeaveFieldAsIS; // Less typing, readable code
  1194. {
  1195. Note: We have little choice but to implement it like Borland did:
  1196. If AValue contains some 'wrong' value, it will throw an error.
  1197. To simulate this we'd have to check in each function whether
  1198. both arguments are correct. To avoid it, all is routed through
  1199. the 'central' RecodeDateTime function as in Borland's implementation.
  1200. }
  1201. Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
  1202. begin
  1203. Result := RecodeDateTime(AValue,AYear,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI);
  1204. end;
  1205. Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
  1206. begin
  1207. Result := RecodeDateTime(AValue,LFAI,AMonth,LFAI,LFAI,LFAI,LFAI,LFAI);
  1208. end;
  1209. Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
  1210. begin
  1211. Result := RecodeDateTime(AValue,LFAI,LFAI,ADay,LFAI,LFAI,LFAI,LFAI);
  1212. end;
  1213. Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
  1214. begin
  1215. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,LFAI,LFAI,LFAI);
  1216. end;
  1217. Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
  1218. begin
  1219. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,AMinute,LFAI,LFAI);
  1220. end;
  1221. Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
  1222. begin
  1223. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,ASecond,LFAI);
  1224. end;
  1225. Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
  1226. begin
  1227. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI,AMilliSecond);
  1228. end;
  1229. Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
  1230. begin
  1231. Result := RecodeDateTime(AValue,AYear,AMonth,ADay,LFAI,LFAI,LFAI,LFAI);
  1232. end;
  1233. Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1234. begin
  1235. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,AMinute,ASecond,AMilliSecond);
  1236. end;
  1237. Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1238. begin
  1239. If Not TryRecodeDateTime(AValue,AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,Result) then
  1240. InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,AValue);
  1241. end;
  1242. Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AResult: TDateTime): Boolean;
  1243. Procedure FV (Var AV : Word; Arg : Word);
  1244. begin
  1245. If (Arg<>LFAI) then
  1246. AV:=Arg;
  1247. end;
  1248. Var
  1249. Y,M,D,H,N,S,MS : Word;
  1250. begin
  1251. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  1252. FV(Y,AYear);
  1253. FV(M,AMonth);
  1254. FV(D,ADay);
  1255. FV(H,AHour);
  1256. FV(N,AMinute);
  1257. FV(S,ASecond);
  1258. FV(MS,AMillisecond);
  1259. Result:=TryEncodeDateTime(Y,M,D,H,N,S,MS,AResult);
  1260. end;
  1261. { ---------------------------------------------------------------------
  1262. Comparision of date/time
  1263. ---------------------------------------------------------------------}
  1264. Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
  1265. begin
  1266. If SameDateTime(A,B) then
  1267. Result:=EqualsValue
  1268. else If A>B then
  1269. Result:=GreaterThanValue
  1270. else
  1271. Result:=LessThanValue
  1272. end;
  1273. Function CompareDate(const A, B: TDateTime): TValueRelationship;
  1274. begin
  1275. If SameDate(A,B) then
  1276. Result:=EQualsValue
  1277. else if A<B then
  1278. Result:=LessThanValue
  1279. else
  1280. Result:=GreaterThanValue;
  1281. end;
  1282. Function CompareTime(const A, B: TDateTime): TValueRelationship;
  1283. begin
  1284. If SameTime(A,B) then
  1285. Result:=EQualsValue
  1286. else If Frac(A)<Frac(B) then
  1287. Result:=LessThanValue
  1288. else
  1289. Result:=GreaterThanValue;
  1290. end;
  1291. Function SameDateTime(const A, B: TDateTime): Boolean;
  1292. begin
  1293. Result:=Abs(A-B)<OneMilliSecond;
  1294. end;
  1295. Function SameDate(const A, B: TDateTime): Boolean;
  1296. begin
  1297. Result:=Trunc(A)=Trunc(B);
  1298. end;
  1299. Function SameTime(const A, B: TDateTime): Boolean;
  1300. begin
  1301. Result:=Frac(Abs(A-B))<OneMilliSecond;
  1302. end;
  1303. Function InternalNthDayOfWeek(DoM : Word) : Word;
  1304. begin
  1305. Result:=(Dom-1) div 7 +1;
  1306. end;
  1307. Function NthDayOfWeek(const AValue: TDateTime): Word;
  1308. begin
  1309. Result:=InternalNthDayOfWeek(DayOfTheMonth(AValue));
  1310. end;
  1311. Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; var AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  1312. var
  1313. D: Word;
  1314. begin
  1315. DecodeDate(AValue,AYear,AMonth,D);
  1316. ADayOfWeek:=DayOfTheWeek(AValue);
  1317. ANthDayOfWeek:=InternalNthDayOfWeek(D);
  1318. end;
  1319. Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
  1320. begin
  1321. If Not TryEncodeDayOfWeekInMonth(AYear,AMonth,ANthDayOfWeek,ADayOfWeek,Result) then
  1322. InvalidDayOfWeekInMonthError(AYear,AMonth,ANthDayOfWeek,ADayOfWeek);
  1323. end;
  1324. Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
  1325. Var
  1326. SOM,D : Word;
  1327. begin
  1328. SOM:=DayOfTheWeek(EncodeDate(Ayear,AMonth,1));
  1329. D:=1+ADayOfWeek-SOM+7*(ANthDayOfWeek-1);
  1330. If SOM>ADayOfWeek then
  1331. D:=D+7; // Clearer would have been Inc(ANthDayOfweek) but it's a const
  1332. Result:=TryEncodeDate(Ayear,AMonth,D,AValue);
  1333. end;
  1334. { ---------------------------------------------------------------------
  1335. Exception throwing routines
  1336. ---------------------------------------------------------------------}
  1337. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
  1338. Function DoField(Arg,Def : Word; Unknown: String) : String;
  1339. begin
  1340. If (Arg<>LFAI) then
  1341. Result:=Format('%.*d',[Length(Unknown),Arg])
  1342. else if (ABaseDate=0) then
  1343. Result:=Unknown
  1344. else
  1345. Result:=Format('%.*d',[Length(Unknown),Arg]);
  1346. end;
  1347. Var
  1348. Y,M,D,H,N,S,MS : Word;
  1349. Msg : String;
  1350. begin
  1351. DecodeDateTime(ABasedate,Y,M,D,H,N,S,MS);
  1352. Msg:=DoField(AYear,Y,'????');
  1353. Msg:=Msg+DateSeparator+DoField(AMonth,M,'??');
  1354. Msg:=Msg+DateSeparator+DoField(ADay,D,'??');
  1355. Msg:=Msg+' '+DoField(AHour,H,'??');
  1356. Msg:=Msg+TimeSeparator+DoField(AMinute,N,'??');
  1357. Msg:=Msg+TimeSeparator+Dofield(ASecond,S,'??');
  1358. Msg:=Msg+DecimalSeparator+DoField(AMilliSecond,MS,'???');
  1359. Raise EConvertError.CreateFmt(SErrInvalidTimeStamp,[Msg]);
  1360. end;
  1361. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
  1362. begin
  1363. InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,0);
  1364. end;
  1365. Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
  1366. begin
  1367. Raise EConvertError.CreateFmt(SErrInvalidDateWeek,[AYear,AWeekOfYear,ADayOfWeek]);
  1368. end;
  1369. Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
  1370. begin
  1371. Raise EConvertError.CreateFmt(SErrInvalidDayOfYear,[AYear,ADayOfYear]);
  1372. end;
  1373. Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  1374. begin
  1375. Raise EConvertError.CreateFmt(SErrInvalidDateMonthWeek,[Ayear,AMonth,AWeekOfMonth,ADayOfWeek]);
  1376. end;
  1377. Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  1378. begin
  1379. Raise EConvertError.CreateFmt(SErrInvalidDayOfWeekInMonth,[AYear,AMonth,ANthDayOfWeek,ADayOfWeek]);
  1380. end;
  1381. { ---------------------------------------------------------------------
  1382. Julian and Modified Julian Date conversion support
  1383. ---------------------------------------------------------------------}
  1384. Function DateTimeToJulianDate(const AValue: TDateTime): Double;
  1385. begin
  1386. NotYetImplemented('DateTimeToJulianDate');
  1387. end;
  1388. Function JulianDateToDateTime(const AValue: Double): TDateTime;
  1389. begin
  1390. NotYetImplemented('JulianDateToDateTime');
  1391. end;
  1392. Function TryJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
  1393. begin
  1394. NotYetImplemented('TryJulianDateToDateTime');
  1395. end;
  1396. Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
  1397. begin
  1398. NotYetImplemented('DateTimeToModifiedJulianDate');
  1399. end;
  1400. Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
  1401. begin
  1402. NotYetImplemented('ModifiedJulianDateToDateTime');
  1403. end;
  1404. Function TryModifiedJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
  1405. begin
  1406. NotYetImplemented('TryModifiedJulianDateToDateTime');
  1407. end;
  1408. { ---------------------------------------------------------------------
  1409. Unix timestamp support.
  1410. ---------------------------------------------------------------------}
  1411. Function DateTimeToUnix(const AValue: TDateTime): Int64;
  1412. begin
  1413. NotYetImplemented('DateTimeToUnix');
  1414. end;
  1415. Function UnixToDateTime(const AValue: Int64): TDateTime;
  1416. begin
  1417. NotYetImplemented('UnixToDateTime');
  1418. end;
  1419. end.