dateutil.inc 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  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. {$ifndef FPUNONE}
  16. uses
  17. SysUtils, Math;
  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. MonthJanuary = 1;
  37. MonthFebruary = 2;
  38. MonthMarch = 3;
  39. MonthApril = 4;
  40. MonthMay = 5;
  41. MonthJune = 6;
  42. MonthJuly = 7;
  43. MonthAugust = 8;
  44. MonthSeptember = 9;
  45. MonthOctober = 10;
  46. MonthNovember = 11;
  47. MonthDecember = 12;
  48. // Fraction of a day
  49. OneHour = TDateTime(1)/HoursPerDay;
  50. OneMinute = TDateTime(1)/MinsPerDay;
  51. OneSecond = TDateTime(1)/SecsPerDay;
  52. OneMillisecond = TDateTime(1)/MSecsPerDay;
  53. { This is actual days per year but you need to know if it's a leap year}
  54. DaysPerYear: array [Boolean] of Word = (365, 366);
  55. { Used in RecodeDate, RecodeTime and RecodeDateTime for those datetime }
  56. { fields you want to leave alone }
  57. RecodeLeaveFieldAsIs = High(Word);
  58. { ---------------------------------------------------------------------
  59. Global variables used in this unit
  60. ---------------------------------------------------------------------}
  61. Const
  62. { Average over a 4 year span. Valid for next 100 years }
  63. ApproxDaysPerMonth: Double = 30.4375;
  64. ApproxDaysPerYear: Double = 365.25;
  65. { ---------------------------------------------------------------------
  66. Simple trimming functions.
  67. ---------------------------------------------------------------------}
  68. Function DateOf(const AValue: TDateTime): TDateTime;
  69. Function TimeOf(const AValue: TDateTime): TDateTime;
  70. { ---------------------------------------------------------------------
  71. Identification functions.
  72. ---------------------------------------------------------------------}
  73. Function IsInLeapYear(const AValue: TDateTime): Boolean;
  74. Function IsPM(const AValue: TDateTime): Boolean;
  75. Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
  76. Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  77. Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  78. Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
  79. Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
  80. Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
  81. { ---------------------------------------------------------------------
  82. Enumeration functions.
  83. ---------------------------------------------------------------------}
  84. Function WeeksInYear(const AValue: TDateTime): Word;
  85. Function WeeksInAYear(const AYear: Word): Word;
  86. Function DaysInYear(const AValue: TDateTime): Word;
  87. Function DaysInAYear(const AYear: Word): Word;
  88. Function DaysInMonth(const AValue: TDateTime): Word;
  89. Function DaysInAMonth(const AYear, AMonth: Word): Word;
  90. { ---------------------------------------------------------------------
  91. Variations on current date/time.
  92. ---------------------------------------------------------------------}
  93. Function Today: TDateTime;
  94. Function Yesterday: TDateTime;
  95. Function Tomorrow: TDateTime;
  96. Function IsToday(const AValue: TDateTime): Boolean;
  97. Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
  98. function IsSameMonth(const Avalue, ABasis: TDateTime): Boolean;
  99. Function PreviousDayOfWeek (DayOfWeek : Word) : Word;
  100. { ---------------------------------------------------------------------
  101. Extraction functions.
  102. ---------------------------------------------------------------------}
  103. Function YearOf(const AValue: TDateTime): Word;
  104. Function MonthOf(const AValue: TDateTime): Word;
  105. Function WeekOf(const AValue: TDateTime): Word;
  106. Function DayOf(const AValue: TDateTime): Word;
  107. Function HourOf(const AValue: TDateTime): Word;
  108. Function MinuteOf(const AValue: TDateTime): Word;
  109. Function SecondOf(const AValue: TDateTime): Word;
  110. Function MilliSecondOf(const AValue: TDateTime): Word;
  111. { ---------------------------------------------------------------------
  112. Start/End of year functions.
  113. ---------------------------------------------------------------------}
  114. Function StartOfTheYear(const AValue: TDateTime): TDateTime;
  115. Function EndOfTheYear(const AValue: TDateTime): TDateTime;
  116. Function StartOfAYear(const AYear: Word): TDateTime;
  117. Function EndOfAYear(const AYear: Word): TDateTime;
  118. { ---------------------------------------------------------------------
  119. Start/End of month functions.
  120. ---------------------------------------------------------------------}
  121. Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
  122. Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
  123. Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
  124. Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
  125. { ---------------------------------------------------------------------
  126. Start/End of week functions.
  127. ---------------------------------------------------------------------}
  128. Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
  129. Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
  130. Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  131. Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
  132. Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  133. Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
  134. { ---------------------------------------------------------------------
  135. Start/End of day functions.
  136. ---------------------------------------------------------------------}
  137. Function StartOfTheDay(const AValue: TDateTime): TDateTime;
  138. Function EndOfTheDay(const AValue: TDateTime): TDateTime;
  139. Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
  140. Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
  141. Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
  142. Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
  143. { ---------------------------------------------------------------------
  144. Part of year functions.
  145. ---------------------------------------------------------------------}
  146. Function MonthOfTheYear(const AValue: TDateTime): Word;
  147. Function WeekOfTheYear(const AValue: TDateTime): Word; overload;
  148. Function WeekOfTheYear(const AValue: TDateTime; out AYear: Word): Word; overload;
  149. Function DayOfTheYear(const AValue: TDateTime): Word;
  150. Function HourOfTheYear(const AValue: TDateTime): Word;
  151. Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
  152. Function SecondOfTheYear(const AValue: TDateTime): LongWord;
  153. Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
  154. { ---------------------------------------------------------------------
  155. Part of month functions.
  156. ---------------------------------------------------------------------}
  157. Function WeekOfTheMonth(const AValue: TDateTime): Word; overload;
  158. Function WeekOfTheMonth(const AValue: TDateTime; out AYear, AMonth: Word): Word; overload;
  159. Function DayOfTheMonth(const AValue: TDateTime): Word;
  160. Function HourOfTheMonth(const AValue: TDateTime): Word;
  161. Function MinuteOfTheMonth(const AValue: TDateTime): Word;
  162. Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
  163. Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
  164. { ---------------------------------------------------------------------
  165. Part of week functions.
  166. ---------------------------------------------------------------------}
  167. Function DayOfTheWeek(const AValue: TDateTime): Word;
  168. Function HourOfTheWeek(const AValue: TDateTime): Word;
  169. Function MinuteOfTheWeek(const AValue: TDateTime): Word;
  170. Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
  171. Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
  172. { ---------------------------------------------------------------------
  173. Part of day functions.
  174. ---------------------------------------------------------------------}
  175. Function HourOfTheDay(const AValue: TDateTime): Word;
  176. Function MinuteOfTheDay(const AValue: TDateTime): Word;
  177. Function SecondOfTheDay(const AValue: TDateTime): LongWord;
  178. Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
  179. { ---------------------------------------------------------------------
  180. Part of hour functions.
  181. ---------------------------------------------------------------------}
  182. Function MinuteOfTheHour(const AValue: TDateTime): Word;
  183. Function SecondOfTheHour(const AValue: TDateTime): Word;
  184. Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
  185. { ---------------------------------------------------------------------
  186. Part of minute functions.
  187. ---------------------------------------------------------------------}
  188. Function SecondOfTheMinute(const AValue: TDateTime): Word;
  189. Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
  190. { ---------------------------------------------------------------------
  191. Part of second functions.
  192. ---------------------------------------------------------------------}
  193. Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
  194. { ---------------------------------------------------------------------
  195. Range checking functions.
  196. ---------------------------------------------------------------------}
  197. Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
  198. Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
  199. Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
  200. Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
  201. Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
  202. Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
  203. Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
  204. Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
  205. { ---------------------------------------------------------------------
  206. Period functions.
  207. ---------------------------------------------------------------------}
  208. Function YearsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
  209. Function MonthsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
  210. Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
  211. Function DaysBetween(const ANow, AThen: TDateTime): Integer;
  212. Function HoursBetween(const ANow, AThen: TDateTime): Int64;
  213. Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
  214. Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
  215. Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
  216. Procedure PeriodBetween(const ANow, AThen: TDateTime; Out Years, months, days : Word);
  217. { ---------------------------------------------------------------------
  218. Timespan in xxx functions.
  219. ---------------------------------------------------------------------}
  220. { YearSpan and MonthSpan are approximate values }
  221. Function YearSpan(const ANow, AThen: TDateTime): Double;
  222. Function MonthSpan(const ANow, AThen: TDateTime): Double;
  223. Function WeekSpan(const ANow, AThen: TDateTime): Double;
  224. Function DaySpan(const ANow, AThen: TDateTime): Double;
  225. Function HourSpan(const ANow, AThen: TDateTime): Double;
  226. Function MinuteSpan(const ANow, AThen: TDateTime): Double;
  227. Function SecondSpan(const ANow, AThen: TDateTime): Double;
  228. Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
  229. { ---------------------------------------------------------------------
  230. Increment/decrement functions.
  231. ---------------------------------------------------------------------}
  232. Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
  233. Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
  234. // Function IncMonth is in SysUtils
  235. Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
  236. Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
  237. Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
  238. Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
  239. Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
  240. Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
  241. Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
  242. Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
  243. Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
  244. Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
  245. Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
  246. Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
  247. { ---------------------------------------------------------------------
  248. Encode/Decode of complete timestamp
  249. ---------------------------------------------------------------------}
  250. Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  251. Procedure DecodeDateTime(const AValue: TDateTime; out AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
  252. Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; out AValue: TDateTime): Boolean;
  253. { ---------------------------------------------------------------------
  254. Encode/decode date, specifying week of year and day of week
  255. ---------------------------------------------------------------------}
  256. Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  257. Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
  258. Procedure DecodeDateWeek(const AValue: TDateTime; out AYear, AWeekOfYear, ADayOfWeek: Word);
  259. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; out AValue: TDateTime; const ADayOfWeek: Word): Boolean;
  260. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; out AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
  261. { ---------------------------------------------------------------------
  262. Encode/decode date, specifying day of year
  263. ---------------------------------------------------------------------}
  264. Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
  265. Procedure DecodeDateDay(const AValue: TDateTime; out AYear, ADayOfYear: Word);
  266. Function TryEncodeDateDay(const AYear, ADayOfYear: Word; out AValue: TDateTime): Boolean;
  267. { ---------------------------------------------------------------------
  268. Encode/decode date, specifying week of month
  269. ---------------------------------------------------------------------}
  270. Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
  271. Procedure DecodeDateMonthWeek(const AValue: TDateTime; out AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  272. Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; out AValue: TDateTime): Boolean;
  273. { ---------------------------------------------------------------------
  274. Encode time interval, allowing hours>24
  275. ---------------------------------------------------------------------}
  276. function TryEncodeTimeInterval(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;
  277. function EncodeTimeInterval(Hour, Minute, Second, MilliSecond:word): TDateTime;
  278. { ---------------------------------------------------------------------
  279. Replace given element with supplied value.
  280. ---------------------------------------------------------------------}
  281. Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
  282. Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
  283. Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
  284. Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
  285. Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
  286. Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
  287. Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
  288. Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
  289. Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  290. Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  291. Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; out AResult: TDateTime): Boolean;
  292. { ---------------------------------------------------------------------
  293. Comparision of date/time
  294. ---------------------------------------------------------------------}
  295. Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
  296. Function CompareDate(const A, B: TDateTime): TValueRelationship;
  297. Function CompareTime(const A, B: TDateTime): TValueRelationship;
  298. Function SameDateTime(const A, B: TDateTime): Boolean;
  299. Function SameDate(const A, B: TDateTime): Boolean;
  300. Function SameTime(const A, B: TDateTime): Boolean;
  301. function DateTimeInRange(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
  302. function TimeInRange(ATime: TTime; AStartTime, AEndTime: TTime; AInclusive: Boolean = True): Boolean;
  303. function DateInRange(ADate: TDate; AStartDate, AEndDate: TDate; AInclusive: Boolean = True): Boolean;
  304. { For a given date these Functions tell you the which day of the week of the
  305. month (or year). If its a Thursday, they will tell you if its the first,
  306. second, etc Thursday of the month (or year). Remember, even though its
  307. the first Thursday of the year it doesn't mean its the first week of the
  308. year. See ISO 8601 above for more information. }
  309. Function NthDayOfWeek(const AValue: TDateTime): Word;
  310. Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; out AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  311. Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
  312. Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; out AValue: TDateTime): Boolean;
  313. { ---------------------------------------------------------------------
  314. Exception throwing routines
  315. ---------------------------------------------------------------------}
  316. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
  317. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
  318. Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
  319. Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
  320. Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  321. Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  322. { ---------------------------------------------------------------------
  323. Julian and Modified Julian Date conversion support
  324. ---------------------------------------------------------------------}
  325. Function DateTimeToJulianDate(const AValue: TDateTime): Double;
  326. Function JulianDateToDateTime(const AValue: Double): TDateTime;
  327. Function TryJulianDateToDateTime(const AValue: Double; out ADateTime: TDateTime): Boolean;
  328. Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
  329. Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
  330. Function TryModifiedJulianDateToDateTime(const AValue: Double; out ADateTime: TDateTime): Boolean;
  331. { ---------------------------------------------------------------------
  332. Unix timestamp support.
  333. ---------------------------------------------------------------------}
  334. Function DateTimeToUnix(const AValue: TDateTime; AInputIsUTC: Boolean = True): Int64;
  335. Function UnixToDateTime(const AValue: Int64; aReturnUTC : Boolean = true): TDateTime;
  336. Function UnixTimeStampToMac(const AValue: Int64): Int64;
  337. { ---------------------------------------------------------------------
  338. Mac timestamp support.
  339. ---------------------------------------------------------------------}
  340. Function DateTimeToMac(const AValue: TDateTime): Int64;
  341. Function MacToDateTime(const AValue: Int64): TDateTime;
  342. Function MacTimeStampToUnix(const AValue: Int64): Int64;
  343. { .....................................................................
  344. Dos <-> Delphi datetime support
  345. .....................................................................}
  346. Function DateTimeToDosDateTime(const AValue: TDateTime): longint;
  347. Function DosDateTimeToDateTime( AValue: longint): TDateTime;
  348. { UTC <-> Local time }
  349. Function UniversalTimeToLocal(UT: TDateTime): TDateTime;
  350. Function UniversalTimeToLocal(UT: TDateTime; TZOffset : Integer): TDateTime;
  351. Function LocalTimeToUniversal(LT: TDateTime): TDateTime;
  352. Function LocalTimeToUniversal(LT: TDateTime; TZOffset: Integer): TDateTime;
  353. { ScanDateTime is a limited inverse of formatdatetime }
  354. function ScanDateTime(const Pattern:string;const s:string;const fmt:TFormatSettings;startpos:integer=1) : tdatetime; overload;
  355. function ScanDateTime(const Pattern:string;const s:string;startpos:integer=1) : tdatetime; overload;
  356. // ISO date/time
  357. // YYYYMMDD or YYYY-MM-DD
  358. function TryISOStrToDate(const aString: string; out outDate: TDateTime): Boolean;
  359. // HH HH:NN HH:NN:SS HH:NN:SS.ZZZ or HHNN HHNNSS HHNNSS.ZZZ
  360. function TryISOStrToTime(const aString: string; Out outTime: TDateTime): Boolean;
  361. // Combination of previous
  362. function TryISOStrToDateTime(const aString: string; out outDateTime: TDateTime): Boolean;
  363. // Z +hh:nn -hh:nn
  364. Function TryISOTZStrToTZOffset(const TZ : String; Out TZOffset : Integer) : boolean;
  365. // ISO 8601 Date/Time formatting
  366. function DateToISO8601(const ADate: TDateTime; AInputIsUTC: Boolean = True): string;
  367. Function ISO8601ToDate(const DateString: string; ReturnUTC : Boolean = True): TDateTime;
  368. Function ISO8601ToDateDef(const DateString: string; ReturnUTC : Boolean; aDefault : TDateTime ): TDateTime; deprecated;
  369. Function ISO8601ToDateDef(const DateString: string; aDefault : TDateTime; ReturnUTC : Boolean = True ): TDateTime;
  370. Function TryISO8601ToDate(const DateString: string; out ADateTime: TDateTime; ReturnUTC : Boolean = True) : Boolean;
  371. implementation
  372. uses sysconst;
  373. const
  374. TDateTimeEpsilon = 2.2204460493e-16;
  375. HalfMilliSecond = OneMillisecond /2 ;
  376. { ---------------------------------------------------------------------
  377. Auxiliary routines
  378. ---------------------------------------------------------------------}
  379. Procedure NotYetImplemented (FN : String);
  380. begin
  381. Raise Exception.CreateFmt('Function "%s" (dateutils) is not yet implemented',[FN]);
  382. end;
  383. { ---------------------------------------------------------------------
  384. Simple trimming functions.
  385. ---------------------------------------------------------------------}
  386. Function DateOf(const AValue: TDateTime): TDateTime; inline;
  387. begin
  388. Result:=Trunc(AValue);
  389. end;
  390. Function TimeOf(const AValue: TDateTime): TDateTime; inline;
  391. begin
  392. Result:=Frac(Avalue);
  393. end;
  394. { ---------------------------------------------------------------------
  395. Identification functions.
  396. ---------------------------------------------------------------------}
  397. Function IsInLeapYear(const AValue: TDateTime): Boolean;
  398. begin
  399. Result:=IsLeapYear(YearOf(AValue));
  400. end;
  401. Function IsPM(const AValue: TDateTime): Boolean; inline;
  402. begin
  403. Result:=(HourOf(AValue)>=12);
  404. end;
  405. Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
  406. begin
  407. Result:=(AYear<>0) and (AYear<10000)
  408. and (AMonth in [1..12])
  409. and (ADay<>0) and (ADay<=MonthDays[IsleapYear(AYear),AMonth]);
  410. end;
  411. Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  412. begin
  413. Result:=(AHour=HoursPerDay) and (AMinute=0) and (ASecond=0) and (AMillisecond=0);
  414. Result:=Result or
  415. ((AHour<HoursPerDay) and (AMinute<MinsPerHour) and (ASecond<SecsPerMin) and
  416. (AMillisecond<MSecsPerSec));
  417. end;
  418. Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
  419. begin
  420. Result:=IsValidDate(AYear,AMonth,ADay) and
  421. IsValidTime(AHour,AMinute,ASecond,AMillisecond)
  422. end;
  423. Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
  424. begin
  425. Result:=(AYear<>0) and (ADayOfYear<>0) and (AYear<10000) and
  426. (ADayOfYear<=DaysPerYear[IsLeapYear(AYear)]);
  427. end;
  428. Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
  429. begin
  430. Result:=(AYear<>0) and (AYear<10000)
  431. and (ADayOfWeek in [1..7])
  432. and (AWeekOfYear<>0)
  433. and (AWeekOfYear<=WeeksInaYear(AYear));
  434. { should we not also check whether the day of the week is not
  435. larger than the last day of the last week in the year 9999 ?? }
  436. end;
  437. Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
  438. begin
  439. Result:=(AYear<>0) and (AYear<10000)
  440. and (AMonth in [1..12])
  441. and (AWeekOfMonth in [1..5])
  442. and (ADayOfWeek in [1..7]);
  443. end;
  444. { ---------------------------------------------------------------------
  445. Enumeration functions.
  446. ---------------------------------------------------------------------}
  447. Function WeeksInYear(const AValue: TDateTime): Word;
  448. begin
  449. Result:=WeeksInAYear(YearOf(AValue));
  450. end;
  451. Function WeeksInAYear(const AYear: Word): Word;
  452. Var
  453. DOW : Word;
  454. begin
  455. Result:=52;
  456. DOW:=DayOfTheWeek(StartOfAYear(AYear));
  457. If (DOW=4) or ((DOW=3) and IsLeapYear(AYear)) then
  458. Inc(Result);
  459. end;
  460. Function DaysInYear(const AValue: TDateTime): Word;
  461. begin
  462. Result:=DaysPerYear[IsLeapYear(YearOf(AValue))];
  463. end;
  464. Function DaysInAYear(const AYear: Word): Word;
  465. begin
  466. Result:=DaysPerYear[Isleapyear(AYear)];
  467. end;
  468. Function DaysInMonth(const AValue: TDateTime): Word;
  469. Var
  470. Y,M,D : Word;
  471. begin
  472. Decodedate(AValue,Y,M,D);
  473. Result:=MonthDays[IsLeapYear(Y),M];
  474. end;
  475. Function DaysInAMonth(const AYear, AMonth: Word): Word;
  476. begin
  477. Result:=MonthDays[IsLeapYear(AYear),AMonth];
  478. end;
  479. { ---------------------------------------------------------------------
  480. Variations on current date/time.
  481. ---------------------------------------------------------------------}
  482. Function Today: TDateTime; inline;
  483. begin
  484. Result:=Date();
  485. end;
  486. Function Yesterday: TDateTime;
  487. begin
  488. Result:=Date()-1;
  489. end;
  490. Function Tomorrow: TDateTime;
  491. begin
  492. Result:=Date()+1;
  493. end;
  494. Function IsToday(const AValue: TDateTime): Boolean;
  495. begin
  496. Result:=IsSameDay(AValue,Date());
  497. end;
  498. Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
  499. Var
  500. D : TDateTime;
  501. begin
  502. D:=AValue-Trunc(ABasis);
  503. Result:=(D>=0) and (D<1);
  504. end;
  505. function IsSameMonth(const Avalue, ABasis: TDateTime): Boolean;
  506. begin
  507. result:=( YearOf(Avalue) = YearOf(Abasis) );
  508. result:=result and ( MonthOf(AValue) = MonthOf(ABasis) );
  509. end;
  510. const
  511. DOWMap: array [1..7] of Word = (7, 1, 2, 3, 4, 5, 6);
  512. Function PreviousDayOfWeek (DayOfWeek : Word) : Word;
  513. begin
  514. If Not (DayOfWeek in [1..7]) then
  515. Raise EConvertError.CreateFmt(SErrInvalidDayOfWeek,[DayOfWeek]);
  516. Result:=DOWMap[DayOfWeek];
  517. end;
  518. { ---------------------------------------------------------------------
  519. Extraction functions.
  520. ---------------------------------------------------------------------}
  521. Function YearOf(const AValue: TDateTime): Word;
  522. Var
  523. D,M : Word;
  524. begin
  525. DecodeDate(AValue,Result,D,M);
  526. end;
  527. Function MonthOf(const AValue: TDateTime): Word;
  528. Var
  529. Y,D : Word;
  530. begin
  531. DecodeDate(AValue,Y,Result,D);
  532. end;
  533. Function WeekOf(const AValue: TDateTime): Word; inline;
  534. begin
  535. Result:=WeekOfTheYear(AValue);
  536. end;
  537. Function DayOf(const AValue: TDateTime): Word;
  538. Var
  539. Y,M : Word;
  540. begin
  541. DecodeDate(AValue,Y,M,Result);
  542. end;
  543. Function HourOf(const AValue: TDateTime): Word;
  544. Var
  545. N,S,MS : Word;
  546. begin
  547. DecodeTime(AValue,Result,N,S,MS);
  548. end;
  549. Function MinuteOf(const AValue: TDateTime): Word;
  550. Var
  551. H,S,MS : Word;
  552. begin
  553. DecodeTime(AValue,H,Result,S,MS);
  554. end;
  555. Function SecondOf(const AValue: TDateTime): Word;
  556. Var
  557. H,N,MS : Word;
  558. begin
  559. DecodeTime(AValue,H,N,Result,MS);
  560. end;
  561. Function MilliSecondOf(const AValue: TDateTime): Word;
  562. Var
  563. H,N,S : Word;
  564. begin
  565. DecodeTime(AValue,H,N,S,Result);
  566. end;
  567. { ---------------------------------------------------------------------
  568. Start/End of year functions.
  569. ---------------------------------------------------------------------}
  570. Function StartOfTheYear(const AValue: TDateTime): TDateTime;
  571. begin
  572. Result:=EncodeDate(YearOf(AValue),1,1);
  573. end;
  574. Function EndOfTheYear(const AValue: TDateTime): TDateTime;
  575. begin
  576. Result:=EncodeDateTime(YearOf(AValue),12,31,23,59,59,999);
  577. end;
  578. Function StartOfAYear(const AYear: Word): TDateTime;
  579. begin
  580. Result:=EncodeDate(AYear,1,1);
  581. end;
  582. Function EndOfAYear(const AYear: Word): TDateTime;
  583. begin
  584. Result:=(EncodeDateTime(AYear,12,31,23,59,59,999));
  585. end;
  586. { ---------------------------------------------------------------------
  587. Start/End of month functions.
  588. ---------------------------------------------------------------------}
  589. Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
  590. Var
  591. Y,M,D : Word;
  592. begin
  593. DecodeDate(AValue,Y,M,D);
  594. Result:=EncodeDate(Y,M,1);
  595. // MonthDays[IsLeapYear(Y),M])
  596. end;
  597. Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
  598. Var
  599. Y,M,D : Word;
  600. begin
  601. DecodeDate(AValue,Y,M,D);
  602. Result:=EncodeDateTime(Y,M,MonthDays[IsLeapYear(Y),M],23,59,59,999);
  603. end;
  604. Function StartOfAMonth(const AYear, AMonth: Word): TDateTime; inline;
  605. begin
  606. Result:=EncodeDate(AYear,AMonth,1);
  607. end;
  608. Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
  609. begin
  610. Result:=EncodeDateTime(AYear,AMonth,MonthDays[IsLeapYear(AYear),AMonth],23,59,59,999);
  611. end;
  612. { ---------------------------------------------------------------------
  613. Start/End of week functions.
  614. ---------------------------------------------------------------------}
  615. Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
  616. begin
  617. Result:=Trunc(AValue)-DayOfTheWeek(AValue)+1;
  618. end;
  619. Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
  620. begin
  621. Result:=EndOfTheDay(AValue-DayOfTheWeek(AValue)+7);
  622. end;
  623. Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  624. begin
  625. Result:=EncodeDateWeek(AYear,AWeekOfYear,ADayOfWeek);
  626. end;
  627. Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; inline; // ADayOFWeek 1
  628. begin
  629. Result:=StartOfAWeek(AYear,AWeekOfYear,1)
  630. end;
  631. Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime; inline;
  632. begin
  633. Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
  634. end;
  635. Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
  636. begin
  637. Result:=EndOfAWeek(AYear,AWeekOfYear,7);
  638. end;
  639. { ---------------------------------------------------------------------
  640. Start/End of day functions.
  641. ---------------------------------------------------------------------}
  642. Function StartOfTheDay(const AValue: TDateTime): TDateTime; inline;
  643. begin
  644. StartOfTheDay:=Trunc(Avalue);
  645. end;
  646. Function EndOfTheDay(const AValue: TDateTime): TDateTime;
  647. Var
  648. Y,M,D : Word;
  649. begin
  650. DecodeDate(AValue,Y,M,D);
  651. Result:=EncodeDateTime(Y,M,D,23,59,59,999);
  652. end;
  653. Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; inline;
  654. begin
  655. Result:=EncodeDate(AYear,AMonth,ADay);
  656. end;
  657. Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime;
  658. begin
  659. Result:=StartOfAYear(AYear)+ADayOfYear-1;
  660. end;
  661. Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; inline;
  662. begin
  663. Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
  664. end;
  665. Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime;
  666. begin
  667. Result:=StartOfAYear(AYear)+ADayOfYear-1+EncodeTime(23,59,59,999);
  668. end;
  669. { ---------------------------------------------------------------------
  670. Part of year functions.
  671. ---------------------------------------------------------------------}
  672. Function MonthOfTheYear(const AValue: TDateTime): Word; inline;
  673. begin
  674. Result:=MonthOf(AValue);
  675. end;
  676. Function WeekOfTheYear(const AValue: TDateTime): Word;
  677. Var
  678. Y,DOW : Word;
  679. begin
  680. DecodeDateWeek(AValue,Y,Result,DOW)
  681. end;
  682. Function WeekOfTheYear(const AValue: TDateTime; out AYear: Word): Word;
  683. Var
  684. DOW : Word;
  685. begin
  686. DecodeDateWeek(AValue,AYear,Result,DOW);
  687. end;
  688. Function DayOfTheYear(const AValue: TDateTime): Word;
  689. begin
  690. Result:=Trunc(AValue-StartOfTheYear(AValue)+1);
  691. end;
  692. Function HourOfTheYear(const AValue: TDateTime): Word;
  693. Var
  694. H,M,S,MS : Word;
  695. begin
  696. DecodeTime(AValue,H,M,S,MS);
  697. Result:=H+((DayOfTheYear(AValue)-1)*24);
  698. end;
  699. Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
  700. Var
  701. H,M,S,MS : Word;
  702. begin
  703. DecodeTime(AValue,H,M,S,MS);
  704. Result:=M+(H+((DayOfTheYear(AValue)-1)*24))*60;
  705. end;
  706. Function SecondOfTheYear(const AValue: TDateTime): LongWord;
  707. Var
  708. H,M,S,MS : Word;
  709. begin
  710. DecodeTime(AValue,H,M,S,MS);
  711. Result:=(M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S;
  712. end;
  713. Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
  714. Var
  715. H,M,S,MS : Word;
  716. begin
  717. DecodeTime(AValue,H,M,S,MS);
  718. Result:=((M+(H+((int64(DayOfTheYear(AValue))-1)*24))*60)*60+S)*1000+MS;
  719. end;
  720. { ---------------------------------------------------------------------
  721. Part of month functions.
  722. ---------------------------------------------------------------------}
  723. Function WeekOfTheMonth(const AValue: TDateTime): Word;
  724. var
  725. Y,M,DOW : word;
  726. begin
  727. DecodeDateMonthWeek(AValue,Y,M,Result,DOW);
  728. end;
  729. Function WeekOfTheMonth(const AValue: TDateTime; out AYear, AMonth: Word): Word;
  730. Var
  731. DOW : Word;
  732. begin
  733. DecodeDateMonthWeek(AValue,AYear,AMonth,Result,DOW);
  734. end;
  735. Function DayOfTheMonth(const AValue: TDateTime): Word;
  736. Var
  737. Y,M : Word;
  738. begin
  739. DecodeDate(AValue,Y,M,Result);
  740. end;
  741. Function HourOfTheMonth(const AValue: TDateTime): Word;
  742. Var
  743. Y,M,D,H,N,S,MS : Word;
  744. begin
  745. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  746. Result:=(D-1)*24+H;
  747. end;
  748. Function MinuteOfTheMonth(const AValue: TDateTime): Word;
  749. Var
  750. Y,M,D,H,N,S,MS : Word;
  751. begin
  752. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  753. Result:=((D-1)*24+H)*60+N;
  754. end;
  755. Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
  756. Var
  757. Y,M,D,H,N,S,MS : Word;
  758. begin
  759. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  760. Result:=(((D-1)*24+H)*60+N)*60+S;
  761. end;
  762. Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
  763. Var
  764. Y,M,D,H,N,S,MS : Word;
  765. begin
  766. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  767. Result:=((((D-1)*24+H)*60+N)*60+S)*1000+MS;
  768. end;
  769. { ---------------------------------------------------------------------
  770. Part of week functions.
  771. ---------------------------------------------------------------------}
  772. Function DayOfTheWeek(const AValue: TDateTime): Word;
  773. begin
  774. Result:=DowMAP[DayOfWeek(AValue)];
  775. end;
  776. Function HourOfTheWeek(const AValue: TDateTime): Word;
  777. Var
  778. H,M,S,MS : Word;
  779. begin
  780. DecodeTime(AValue,H,M,S,MS);
  781. Result:=(DayOfTheWeek(AValue)-1)*24+H;
  782. end;
  783. Function MinuteOfTheWeek(const AValue: TDateTime): Word;
  784. Var
  785. H,M,S,MS : Word;
  786. begin
  787. DecodeTime(AValue,H,M,S,MS);
  788. Result:=((DayOfTheWeek(AValue)-1)*24+H)*60+M;
  789. end;
  790. Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
  791. Var
  792. H,M,S,MS : Word;
  793. begin
  794. DecodeTime(AValue,H,M,S,MS);
  795. Result:=(((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S;
  796. end;
  797. Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
  798. Var
  799. H,M,S,MS : Word;
  800. begin
  801. DecodeTime(AValue,H,M,S,MS);
  802. Result:=((((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S)*1000+MS;
  803. end;
  804. { ---------------------------------------------------------------------
  805. Part of day functions.
  806. ---------------------------------------------------------------------}
  807. Function HourOfTheDay(const AValue: TDateTime): Word; inline;
  808. begin
  809. Result:=HourOf(AValue);
  810. end;
  811. Function MinuteOfTheDay(const AValue: TDateTime): Word;
  812. Var
  813. H,M,S,MS : Word;
  814. begin
  815. DecodeTime(AValue,H,M,S,MS);
  816. Result:=(H*60)+M;
  817. end;
  818. Function SecondOfTheDay(const AValue: TDateTime): LongWord;
  819. Var
  820. H,M,S,MS : Word;
  821. begin
  822. DecodeTime(AValue,H,M,S,MS);
  823. Result:=((H*60)+M)*60+S;
  824. end;
  825. Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
  826. Var
  827. H,M,S,MS : Word;
  828. begin
  829. DecodeTime(AValue,H,M,S,MS);
  830. Result:=(((H*60)+M)*60+S)*1000+MS;
  831. end;
  832. { ---------------------------------------------------------------------
  833. Part of hour functions.
  834. ---------------------------------------------------------------------}
  835. Function MinuteOfTheHour(const AValue: TDateTime): Word; inline;
  836. begin
  837. Result:=MinuteOf(AValue);
  838. end;
  839. Function SecondOfTheHour(const AValue: TDateTime): Word;
  840. Var
  841. H,S,M,MS : Word;
  842. begin
  843. DecodeTime(AValue,H,M,S,MS);
  844. Result:=M*60+S;
  845. end;
  846. Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
  847. Var
  848. H,S,M,MS : Word;
  849. begin
  850. DecodeTime(AValue,H,M,S,MS);
  851. Result:=(M*60+S)*1000+MS;
  852. end;
  853. { ---------------------------------------------------------------------
  854. Part of minute functions.
  855. ---------------------------------------------------------------------}
  856. Function SecondOfTheMinute(const AValue: TDateTime): Word; inline;
  857. begin
  858. Result:=SecondOf(AValue);
  859. end;
  860. Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
  861. Var
  862. H,S,M,MS : Word;
  863. begin
  864. DecodeTime(AValue,H,M,S,MS);
  865. Result:=S*1000+MS;
  866. end;
  867. { ---------------------------------------------------------------------
  868. Part of second functions.
  869. ---------------------------------------------------------------------}
  870. Function MilliSecondOfTheSecond(const AValue: TDateTime): Word; inline;
  871. begin
  872. Result:=MilliSecondOf(AValue);
  873. end;
  874. { ---------------------------------------------------------------------
  875. Range checking functions.
  876. ---------------------------------------------------------------------}
  877. Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean; inline;
  878. begin
  879. Result:=YearsBetween(ANow,AThen)<=AYears;
  880. end;
  881. Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean; inline;
  882. begin
  883. Result:=MonthsBetween(ANow,AThen)<=AMonths;
  884. end;
  885. Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean; inline;
  886. begin
  887. Result:=WeeksBetween(ANow,AThen)<=AWeeks;
  888. end;
  889. Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean; inline;
  890. begin
  891. Result:=DaysBetween(ANow,AThen)<=ADays;
  892. end;
  893. Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean; inline;
  894. begin
  895. Result:=HoursBetween(ANow,AThen)<=AHours;
  896. end;
  897. Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean; inline;
  898. begin
  899. Result:=MinutesBetween(ANow,AThen)<=AMinutes;
  900. end;
  901. Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean; inline;
  902. begin
  903. Result:=SecondsBetween(ANow,Athen)<=ASeconds;
  904. end;
  905. Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean; inline;
  906. begin
  907. Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
  908. end;
  909. function DateTimeInRange(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
  910. begin
  911. if aInclusive then
  912. Result:=(AStartDateTime<=ADateTime) and (ADateTime<=AEndDateTime)
  913. else
  914. Result:=(AStartDateTime<ADateTime) and (ADateTime<AEndDateTime);
  915. end;
  916. function TimeInRange(ATime: TTime; AStartTime, AEndTime: TTime; AInclusive: Boolean = True): Boolean;
  917. var
  918. LTime, LStartTime, LEndTime: TTime;
  919. begin
  920. LTime:=TimeOf(ATime);
  921. LStartTime:=TimeOf(AStartTime);
  922. LEndTime:=TimeOf(AEndTime);
  923. if LEndTime<LStartTime then
  924. if AInclusive then
  925. Result:=(LStartTime<=LTime) or (LTime<=LEndTime)
  926. else
  927. Result:=(LStartTime<LTime) or (LTime<LEndTime)
  928. else
  929. if AInclusive then
  930. Result:=(LStartTime<=LTime) and (LTime<=LEndTime)
  931. else
  932. Result:=(LStartTime<LTime) and (LTime<LEndTime);
  933. end;
  934. function DateInRange(ADate: TDate; AStartDate, AEndDate: TDate; AInclusive: Boolean = True): Boolean;
  935. begin
  936. if AInclusive then
  937. Result:=(DateOf(AStartDate)<=DateOf(ADate)) and (DateOf(ADate)<=DateOf(AEndDate))
  938. else
  939. Result:=(DateOf(AStartDate)<DateOf(ADate)) and (DateOf(ADate)<DateOf(AEndDate));
  940. end;
  941. { ---------------------------------------------------------------------
  942. Period functions.
  943. ---------------------------------------------------------------------}
  944. {
  945. These functions are declared as approximate by Borland.
  946. A bit strange, since it can be calculated exactly ?
  947. -- No, because you need rounding or truncating (JM)
  948. }
  949. function DateTimeToNumber(ADateTime: TDateTime): Double;
  950. begin
  951. if ADateTime >= 0 then
  952. Result := ADateTime
  953. else
  954. Result := int(ADateTime) - frac(ADateTime);
  955. end;
  956. function NumberToDateTime(AValue: Double): TDateTime;
  957. begin
  958. if AValue >= 0 then
  959. Result := AValue
  960. else
  961. Result := int(AValue) + frac(AValue);
  962. end;
  963. Function DateTimeDiff(const ANow, AThen: TDateTime): TDateTime;
  964. begin
  965. Result := NumberToDateTime(DateTimeToNumber(ANow) - DateTimeToNumber(AThen));
  966. end;
  967. Function YearsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
  968. var
  969. yy, mm, dd: Word;
  970. begin
  971. if AExact and (ANow >= -DateDelta) and (AThen >= -DateDelta) and
  972. (ANow <= MaxDateTime) and (AThen <= MaxDateTime) then
  973. begin
  974. PeriodBetween(ANow, AThen, yy , mm, dd);
  975. Result := yy;
  976. end
  977. else
  978. Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)/ApproxDaysPerYear);
  979. end;
  980. Function MonthsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
  981. var
  982. y, m, d: Word;
  983. begin
  984. if AExact and (ANow >= -DateDelta) and (AThen >= -DateDelta) and
  985. (ANow <= MaxDateTime) and (AThen <= MaxDateTime) then
  986. begin
  987. PeriodBetween(ANow, AThen, y, m, d);
  988. Result := y*12 + m;
  989. end
  990. else
  991. Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)/ApproxDaysPerMonth);
  992. end;
  993. Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
  994. begin
  995. Result:=Trunc(Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond) div 7;
  996. end;
  997. Function DaysBetween(const ANow, AThen: TDateTime): Integer;
  998. begin
  999. if anow>athen then
  1000. Result:=Trunc(Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)
  1001. else
  1002. Result:=Trunc(Abs(DateTimeDiff(AThen,ANow))+HalfMilliSecond); // bug 37361
  1003. end;
  1004. Function HoursBetween(const ANow, AThen: TDateTime): Int64;
  1005. begin
  1006. Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)*HoursPerDay);
  1007. end;
  1008. Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
  1009. begin
  1010. Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)*MinsPerDay);
  1011. end;
  1012. Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
  1013. begin
  1014. Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)*SecsPerDay);
  1015. end;
  1016. Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
  1017. begin
  1018. Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)*MSecsPerDay);
  1019. end;
  1020. Procedure PeriodBetween(Const ANow, AThen: TDateTime; Out Years, months, days : Word);
  1021. var
  1022. Y1, Y2, M1, M2, D1, D2: word;
  1023. begin
  1024. if (AThen>ANow) then
  1025. begin
  1026. DecodeDate(ANow,Y1,M1,D1);
  1027. DecodeDate(AThen,Y2,M2,D2);
  1028. end
  1029. else
  1030. begin
  1031. DecodeDate(AThen,Y1,M1,D1);
  1032. DecodeDate(ANow,Y2,M2,D2);
  1033. end;
  1034. Years:=Y2-Y1;
  1035. if (M1>M2) or ((M1=M2) and (D1>D2)) then Dec(Years);
  1036. if (M1>M2) then Inc(M2,12); //already adjusted Years in that case
  1037. Months:=M2-M1;
  1038. if (D2>=D1) then
  1039. Days:=D2-D1
  1040. else
  1041. begin
  1042. if (Months=0) then
  1043. Months:=11
  1044. else
  1045. Dec(Months);
  1046. Days:=(DaysInAMonth(Y1,M1)-D1)+D2;
  1047. end;
  1048. end;
  1049. { ---------------------------------------------------------------------
  1050. Timespan in xxx functions.
  1051. ---------------------------------------------------------------------}
  1052. Function YearSpan(const ANow, AThen: TDateTime): Double;
  1053. begin
  1054. Result:=Abs(DateTimeDiff(ANow,AThen))/ApproxDaysPerYear;
  1055. end;
  1056. Function MonthSpan(const ANow, AThen: TDateTime): Double;
  1057. begin
  1058. Result:=Abs(DateTimeDiff(ANow,AThen))/ApproxDaysPerMonth;
  1059. end;
  1060. Function WeekSpan(const ANow, AThen: TDateTime): Double;
  1061. begin
  1062. Result:=Abs(DateTimeDiff(ANow,AThen)) / 7
  1063. end;
  1064. Function DaySpan(const ANow, AThen: TDateTime): Double;
  1065. begin
  1066. Result:=Abs(DateTimeDiff(ANow,AThen));
  1067. end;
  1068. Function HourSpan(const ANow, AThen: TDateTime): Double;
  1069. begin
  1070. Result:=Abs(DateTimeDiff(ANow,AThen))*HoursPerDay;
  1071. end;
  1072. Function MinuteSpan(const ANow, AThen: TDateTime): Double;
  1073. begin
  1074. Result:=Abs(DateTimeDiff(ANow,AThen))*MinsPerDay;
  1075. end;
  1076. Function SecondSpan(const ANow, AThen: TDateTime): Double;
  1077. begin
  1078. Result:=Abs(DateTimeDiff(ANow,AThen))*SecsPerDay;
  1079. end;
  1080. Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
  1081. begin
  1082. Result:=Abs(DateTimeDiff(ANow,AThen))*MSecsPerDay;
  1083. end;
  1084. { ---------------------------------------------------------------------
  1085. Increment/decrement functions.
  1086. ---------------------------------------------------------------------}
  1087. { TDateTime is not defined in the interval [-1.0..0.0[. Additionally, when
  1088. negative the time part must be treated using its absolute value (0.25 always
  1089. means "6 a.m.") -> skip the gap and convert the time part when crossing the
  1090. gap -- and take care of rounding errors }
  1091. Procedure MaybeSkipTimeWarp(OldDate: TDateTime; var NewDate: TDateTime);
  1092. begin
  1093. if (OldDate>=0) and (NewDate<-TDateTimeEpsilon) then
  1094. NewDate:=int(NewDate-1.0+TDateTimeEpsilon)-frac(1.0+frac(NewDate))
  1095. else if (OldDate<=-1.0) and (NewDate>-1.0+TDateTimeEpsilon) then
  1096. NewDate:=int(NewDate+1.0-TDateTimeEpsilon)+frac(1.0-abs(frac(1.0+NewDate)));
  1097. end;
  1098. function IncNegativeTime(AValue, Addend: TDateTime): TDateTime;
  1099. var
  1100. newtime: tdatetime;
  1101. begin
  1102. newtime:=-frac(Avalue)+frac(Addend);
  1103. { handle rounding errors }
  1104. if SameValue(newtime,int(newtime)+1,TDateTimeEpsilon) then
  1105. newtime:=int(newtime)+1
  1106. else if SameValue(newtime,int(newtime),TDateTimeEpsilon) then
  1107. newtime:=int(newtime);
  1108. { time underflow -> previous day }
  1109. if newtime<-TDateTimeEpsilon then
  1110. begin
  1111. newtime:=1.0+newtime;
  1112. avalue:=int(avalue)-1;
  1113. end
  1114. { time overflow -> next day }
  1115. else if newtime>=1.0-TDateTimeEpsilon then
  1116. begin
  1117. newtime:=newtime-1.0;
  1118. avalue:=int(avalue)+1;
  1119. end;
  1120. Result:=int(AValue)+int(Addend)-newtime;
  1121. end;
  1122. Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
  1123. Var
  1124. Y,M,D,H,N,S,MS : Word;
  1125. begin
  1126. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  1127. Y:=Y+ANumberOfYears;
  1128. If (M=2) and (D=29) And (Not IsLeapYear(Y)) then
  1129. D:=28;
  1130. Result:=EncodeDateTime(Y,M,D,H,N,S,MS);
  1131. end;
  1132. Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
  1133. begin
  1134. Result:=IncYear(Avalue,1);
  1135. end;
  1136. Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
  1137. begin
  1138. Result:=AValue+ANumberOfWeeks*7;
  1139. MaybeSkipTimeWarp(AValue,Result);
  1140. end;
  1141. Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
  1142. begin
  1143. Result:=IncWeek(Avalue,1);
  1144. end;
  1145. Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
  1146. begin
  1147. Result:=AValue+ANumberOfDays;
  1148. MaybeSkipTimeWarp(AValue,Result);
  1149. end;
  1150. Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
  1151. begin
  1152. Result:=IncDay(Avalue,1);
  1153. end;
  1154. Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
  1155. begin
  1156. if AValue>=0 then
  1157. Result:=AValue+ANumberOfHours/HoursPerDay
  1158. else
  1159. Result:=IncNegativeTime(Avalue,ANumberOfHours/HoursPerDay);
  1160. MaybeSkipTimeWarp(AValue,Result);
  1161. end;
  1162. Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
  1163. begin
  1164. Result:=IncHour(AValue,1);
  1165. end;
  1166. Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
  1167. begin
  1168. if AValue>=0 then
  1169. Result:=AValue+ANumberOfMinutes/MinsPerDay
  1170. else
  1171. Result:=IncNegativeTime(Avalue,ANumberOfMinutes/MinsPerDay);
  1172. MaybeSkipTimeWarp(AValue,Result);
  1173. end;
  1174. Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
  1175. begin
  1176. Result:=IncMinute(AValue,1);
  1177. end;
  1178. Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
  1179. begin
  1180. if AValue>=0 then
  1181. Result:=AValue+ANumberOfSeconds/SecsPerDay
  1182. else
  1183. Result:=IncNegativeTime(Avalue,ANumberOfSeconds/SecsPerDay);
  1184. MaybeSkipTimeWarp(AValue,Result);
  1185. end;
  1186. Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
  1187. begin
  1188. Result:=IncSecond(Avalue,1);
  1189. end;
  1190. Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
  1191. begin
  1192. if Avalue>=0 then
  1193. Result:=AValue+ANumberOfMilliSeconds/MSecsPerDay
  1194. else
  1195. Result:=IncNegativeTime(Avalue,ANumberOfMilliSeconds/MSecsPerDay);
  1196. MaybeSkipTimeWarp(AValue,Result);
  1197. end;
  1198. Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
  1199. begin
  1200. Result:=IncMilliSecond(AValue,1);
  1201. end;
  1202. { ---------------------------------------------------------------------
  1203. Encode/Decode of complete timestamp
  1204. ---------------------------------------------------------------------}
  1205. Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1206. begin
  1207. If Not TryEncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond,Result) then
  1208. InvalidDateTimeError(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond)
  1209. end;
  1210. Procedure DecodeDateTime(const AValue: TDateTime; out AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
  1211. begin
  1212. DecodeTime(AValue,AHour,AMinute,ASecond,AMilliSecond);
  1213. if AHour=24 then // can happen due rounding issues mantis 17123
  1214. begin
  1215. AHour:=0; // rest is already zero
  1216. DecodeDate(round(AValue),AYear,AMonth,ADay);
  1217. end
  1218. else
  1219. DecodeDate(AValue,AYear,AMonth,ADay);
  1220. end;
  1221. Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; out AValue: TDateTime): Boolean;
  1222. Var
  1223. tmp : TDateTime;
  1224. begin
  1225. Result:=TryEncodeDate(AYear,AMonth,ADay,AValue);
  1226. Result:=Result and TryEncodeTime(AHour,AMinute,ASecond,Amillisecond,Tmp);
  1227. If Result then
  1228. Avalue:=ComposeDateTime(AValue,Tmp);
  1229. end;
  1230. { ---------------------------------------------------------------------
  1231. Encode/decode date, specifying week of year and day of week
  1232. ---------------------------------------------------------------------}
  1233. Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
  1234. begin
  1235. If Not TryEncodeDateWeek(AYear,AWeekOfYear,Result,ADayOfWeek) then
  1236. InvalidDateWeekError(AYear,AWeekOfYear,ADayOfWeek);
  1237. end;
  1238. Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
  1239. begin
  1240. Result := EncodeDateWeek(AYear,AWeekOfYear,1);
  1241. end;
  1242. Procedure DecodeDateWeek(const AValue: TDateTime; out AYear, AWeekOfYear, ADayOfWeek: Word);
  1243. var
  1244. DOY : Integer;
  1245. D: Word;
  1246. YS : TDateTime;
  1247. YSDOW, YEDOW: Word;
  1248. begin
  1249. AYear:=YearOf(AValue);
  1250. // Correct to ISO DOW
  1251. ADayOfWeek:=DayOfWeek(AValue)-1;
  1252. If ADAyOfWeek=0 then
  1253. ADayofweek:=7;
  1254. YS:=StartOfAYear(AYear);
  1255. DOY:=Trunc(AValue-YS)+1;
  1256. YSDOW:=DayOfTheWeek(YS);
  1257. // Correct week if later than wednesday. First week never starts later than wednesday
  1258. if (YSDOW<5) then
  1259. Inc(DOY,YSDOW-1)
  1260. else
  1261. Dec(DOY,8-YSDOW);
  1262. if (DOY<=0) then // Day is in last week of previous year.
  1263. DecodeDateWeek(YS-1,AYear,AWeekOfYear,D)
  1264. else
  1265. begin
  1266. AWeekOfYear:=DOY div 7;
  1267. if ((DOY mod 7)<>0) then
  1268. Inc(AWeekOfYear);
  1269. if (AWeekOfYear>52) then // Maybe in first week of next year ?
  1270. begin
  1271. YEDOW:=YSDOW;
  1272. if IsLeapYear(AYear) then
  1273. begin
  1274. Inc(YEDOW);
  1275. if (YEDOW>7) then
  1276. YEDOW:=1;
  1277. end;
  1278. if (YEDOW<4) then // Really next year.
  1279. begin
  1280. Inc(AYear);
  1281. AWeekOfYear:=1;
  1282. end;
  1283. end;
  1284. end;
  1285. end;
  1286. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; out AValue: TDateTime; const ADayOfWeek: Word): Boolean;
  1287. Var
  1288. DOW : Word;
  1289. Rest : Integer;
  1290. begin
  1291. Result:=IsValidDateWeek(Ayear,AWeekOfYear,ADayOfWeek);
  1292. If Result then
  1293. begin
  1294. AValue:=EncodeDate(AYear,1,1)+(7*(AWeekOfYear-1));
  1295. DOW:=DayOfTheWeek(AValue);
  1296. Rest:=ADayOfWeek-DOW;
  1297. If (DOW>4) then
  1298. Inc(Rest,7);
  1299. AValue:=AValue+Rest;
  1300. end;
  1301. end;
  1302. Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; out AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
  1303. begin
  1304. Result:=TryEncodeDateWeek(AYear,AWeekOfYear,AValue,1);
  1305. end;
  1306. { ---------------------------------------------------------------------
  1307. Encode/decode date, specifying day of year
  1308. ---------------------------------------------------------------------}
  1309. Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
  1310. begin
  1311. If Not TryEncodeDateDay(AYear,ADayOfYear,Result) then
  1312. InvalidDateDayError(AYear,ADayOfYear);
  1313. end;
  1314. Procedure DecodeDateDay(const AValue: TDateTime; out AYear, ADayOfYear: Word);
  1315. Var
  1316. M,D : Word;
  1317. begin
  1318. DecodeDate(AValue,AYear,M,D);
  1319. ADayOfyear:=Trunc(AValue-EncodeDate(AYear,1,1))+1;
  1320. end;
  1321. Function TryEncodeDateDay(const AYear, ADayOfYear: Word; out AValue: TDateTime): Boolean;
  1322. begin
  1323. Result:=(ADayOfYear<>0) and (ADayOfYear<=DaysPerYear [IsleapYear(AYear)]);
  1324. If Result then
  1325. AValue:=EncodeDate(AYear,1,1)+ADayOfYear-1;
  1326. end;
  1327. { ---------------------------------------------------------------------
  1328. Encode/decode date, specifying week of month
  1329. ---------------------------------------------------------------------}
  1330. Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
  1331. begin
  1332. If Not TryEncodeDateMonthWeek(Ayear,AMonth,AWeekOfMonth,ADayOfWeek,Result) then
  1333. InvalidDateMonthWeekError(AYear,AMonth,AWeekOfMonth,ADayOfWeek);
  1334. end;
  1335. Procedure DecodeDateMonthWeek(const AValue: TDateTime; out AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  1336. Var
  1337. D,SDOM,EDOM : Word;
  1338. SOM : TdateTime;
  1339. DOM : Integer;
  1340. begin
  1341. DecodeDate(AValue,AYear,AMonth,D);
  1342. ADayOfWeek:=DayOfTheWeek(AValue);
  1343. SOM:=EncodeDate(Ayear,Amonth,1);
  1344. SDOM:=DayOfTheWeek(SOM);
  1345. DOM:=D-1+SDOM;
  1346. If SDOM>4 then
  1347. Dec(DOM,7);
  1348. // Too early in the month. First full week is next week, day is after thursday.
  1349. If DOM<=0 Then
  1350. DecodeDateMonthWeek(SOM-1,AYear,AMonth,AWeekOfMonth,D)
  1351. else
  1352. begin
  1353. AWeekOfMonth:=(DOM div 7)+Ord((DOM mod 7)<>0);
  1354. EDOM:=DayOfTheWeek(EndOfAMonth(Ayear,AMonth));
  1355. // In last days of last long week, so in next month...
  1356. If (EDOM<4) and ((DaysInAMonth(AYear,Amonth)-D)<EDOM) then
  1357. begin
  1358. AWeekOfMonth:=1;
  1359. Inc(AMonth);
  1360. If (AMonth=13) then
  1361. begin
  1362. AMonth:=1;
  1363. Inc(AYear);
  1364. end;
  1365. end;
  1366. end;
  1367. end;
  1368. Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; out AValue: TDateTime): Boolean;
  1369. var
  1370. S : Word;
  1371. DOM : Integer;
  1372. begin
  1373. Result:=IsValidDateMonthWeek(AYear,AMonth,AWeekOfMonth,ADayOfWeek);
  1374. if Result then
  1375. begin
  1376. AValue:=EncodeDate(AYear,AMonth,1);
  1377. DOM:=(AWeekOfMonth-1)*7+ADayOfWeek-1;
  1378. { Correct for first week in last month.}
  1379. S:=DayOfTheWeek(AValue);
  1380. Dec(DOM,S-1);
  1381. if S in [DayFriday..DaySunday] then
  1382. Inc(DOM,7);
  1383. AValue:=AValue+DOM;
  1384. end;
  1385. end;
  1386. { ---------------------------------------------------------------------
  1387. Encode time interval, allowing hours>24
  1388. ---------------------------------------------------------------------}
  1389. function TryEncodeTimeInterval(Hour, Min, Sec, MSec: word; out Time: TDateTime): boolean;
  1390. begin
  1391. Result:= (Min<60) and (Sec<60) and (MSec<1000);
  1392. If Result then
  1393. Time:=TDateTime(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;
  1394. end;
  1395. function EncodeTimeInterval(Hour, Minute, Second, MilliSecond: word): TDateTime;
  1396. begin
  1397. If not TryEncodeTimeInterval(Hour,Minute,Second,MilliSecond,Result) then
  1398. Raise EConvertError.CreateFmt(SerrInvalidHourMinuteSecMsec,
  1399. [Hour,Minute,Second,MilliSecond]);
  1400. end;
  1401. { ---------------------------------------------------------------------
  1402. Replace given element with supplied value.
  1403. ---------------------------------------------------------------------}
  1404. Const
  1405. LFAI = RecodeLeaveFieldAsIS; // Less typing, readable code
  1406. {
  1407. Note: We have little choice but to implement it like Borland did:
  1408. If AValue contains some 'wrong' value, it will throw an error.
  1409. To simulate this we'd have to check in each function whether
  1410. both arguments are correct. To avoid it, all is routed through
  1411. the 'central' RecodeDateTime function as in Borland's implementation.
  1412. }
  1413. Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
  1414. begin
  1415. Result := RecodeDateTime(AValue,AYear,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI);
  1416. end;
  1417. Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
  1418. begin
  1419. Result := RecodeDateTime(AValue,LFAI,AMonth,LFAI,LFAI,LFAI,LFAI,LFAI);
  1420. end;
  1421. Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
  1422. begin
  1423. Result := RecodeDateTime(AValue,LFAI,LFAI,ADay,LFAI,LFAI,LFAI,LFAI);
  1424. end;
  1425. Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
  1426. begin
  1427. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,LFAI,LFAI,LFAI);
  1428. end;
  1429. Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
  1430. begin
  1431. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,AMinute,LFAI,LFAI);
  1432. end;
  1433. Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
  1434. begin
  1435. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,ASecond,LFAI);
  1436. end;
  1437. Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
  1438. begin
  1439. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI,AMilliSecond);
  1440. end;
  1441. Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
  1442. begin
  1443. Result := RecodeDateTime(AValue,AYear,AMonth,ADay,LFAI,LFAI,LFAI,LFAI);
  1444. end;
  1445. Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1446. begin
  1447. Result := RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,AMinute,ASecond,AMilliSecond);
  1448. end;
  1449. Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
  1450. begin
  1451. If Not TryRecodeDateTime(AValue,AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,Result) then
  1452. InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,AValue);
  1453. end;
  1454. Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; out AResult: TDateTime): Boolean;
  1455. Procedure FV (Var AV : Word; Arg : Word);
  1456. begin
  1457. If (Arg<>LFAI) then
  1458. AV:=Arg;
  1459. end;
  1460. Var
  1461. Y,M,D,H,N,S,MS : Word;
  1462. begin
  1463. DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
  1464. FV(Y,AYear);
  1465. FV(M,AMonth);
  1466. FV(D,ADay);
  1467. FV(H,AHour);
  1468. FV(N,AMinute);
  1469. FV(S,ASecond);
  1470. FV(MS,AMillisecond);
  1471. Result:=TryEncodeDateTime(Y,M,D,H,N,S,MS,AResult);
  1472. end;
  1473. { ---------------------------------------------------------------------
  1474. Comparision of date/time
  1475. ---------------------------------------------------------------------}
  1476. Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
  1477. begin
  1478. If SameDateTime(A,B) then
  1479. Result:=EqualsValue
  1480. else if trunc(a)=trunc(b) then
  1481. begin
  1482. if abs(frac(a))>abs(frac(b)) then
  1483. result:=GreaterThanValue
  1484. else
  1485. result:=LessThanValue;
  1486. end
  1487. else
  1488. begin
  1489. if a>b then
  1490. result:=GreaterThanValue
  1491. else
  1492. result:=LessThanValue;
  1493. end;
  1494. end;
  1495. Function CompareDate(const A, B: TDateTime): TValueRelationship;
  1496. begin
  1497. If SameDate(A,B) then
  1498. Result:=EQualsValue
  1499. else if A<B then
  1500. Result:=LessThanValue
  1501. else
  1502. Result:=GreaterThanValue;
  1503. end;
  1504. Function CompareTime(const A, B: TDateTime): TValueRelationship;
  1505. begin
  1506. If SameTime(A,B) then
  1507. Result:=EQualsValue
  1508. else If Frac(A)<Frac(B) then
  1509. Result:=LessThanValue
  1510. else
  1511. Result:=GreaterThanValue;
  1512. end;
  1513. Function SameDateTime(const A, B: TDateTime): Boolean;
  1514. begin
  1515. Result:=Abs(A-B)<OneMilliSecond;
  1516. end;
  1517. Function SameDate(const A, B: TDateTime): Boolean; inline;
  1518. begin
  1519. Result:=Trunc(A)=Trunc(B);
  1520. end;
  1521. Function SameTime(const A, B: TDateTime): Boolean;
  1522. begin
  1523. Result:=Frac(Abs(A-B))<OneMilliSecond;
  1524. end;
  1525. Function InternalNthDayOfWeek(DoM : Word) : Word;
  1526. begin
  1527. Result:=(Dom-1) div 7 +1;
  1528. end;
  1529. Function NthDayOfWeek(const AValue: TDateTime): Word;
  1530. begin
  1531. Result:=InternalNthDayOfWeek(DayOfTheMonth(AValue));
  1532. end;
  1533. Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; out AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  1534. var
  1535. D: Word;
  1536. begin
  1537. DecodeDate(AValue,AYear,AMonth,D);
  1538. ADayOfWeek:=DayOfTheWeek(AValue);
  1539. ANthDayOfWeek:=InternalNthDayOfWeek(D);
  1540. end;
  1541. Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
  1542. begin
  1543. If Not TryEncodeDayOfWeekInMonth(AYear,AMonth,ANthDayOfWeek,ADayOfWeek,Result) then
  1544. InvalidDayOfWeekInMonthError(AYear,AMonth,ANthDayOfWeek,ADayOfWeek);
  1545. end;
  1546. Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; out AValue: TDateTime): Boolean;
  1547. Var
  1548. SOM,D : Word;
  1549. begin
  1550. SOM:=DayOfTheWeek(EncodeDate(Ayear,AMonth,1));
  1551. D:=1+ADayOfWeek-SOM+7*(ANthDayOfWeek-1);
  1552. If SOM>ADayOfWeek then
  1553. D:=D+7; // Clearer would have been Inc(ANthDayOfweek) but it's a const
  1554. Result:=TryEncodeDate(Ayear,AMonth,D,AValue);
  1555. end;
  1556. { ---------------------------------------------------------------------
  1557. Exception throwing routines
  1558. ---------------------------------------------------------------------}
  1559. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
  1560. Function DoField(Arg,Def : Word; Unknown: String) : String;
  1561. begin
  1562. If (Arg<>LFAI) then
  1563. Result:=Format('%.*d',[Length(Unknown),Arg])
  1564. else if (ABaseDate=0) then
  1565. Result:=Unknown
  1566. else
  1567. Result:=Format('%.*d',[Length(Unknown),Arg]);
  1568. end;
  1569. Var
  1570. Y,M,D,H,N,S,MS : Word;
  1571. Msg : String;
  1572. begin
  1573. DecodeDateTime(ABasedate,Y,M,D,H,N,S,MS);
  1574. Msg:=DoField(AYear,Y,'????');
  1575. Msg:=Msg+DefaultFormatSettings.DateSeparator+DoField(AMonth,M,'??');
  1576. Msg:=Msg+DefaultFormatSettings.DateSeparator+DoField(ADay,D,'??');
  1577. Msg:=Msg+' '+DoField(AHour,H,'??');
  1578. Msg:=Msg+DefaultFormatSettings.TimeSeparator+DoField(AMinute,N,'??');
  1579. Msg:=Msg+DefaultFormatSettings.TimeSeparator+Dofield(ASecond,S,'??');
  1580. Msg:=Msg+DefaultFormatSettings.DecimalSeparator+DoField(AMilliSecond,MS,'???');
  1581. Raise EConvertError.CreateFmt(SErrInvalidTimeStamp,[Msg]);
  1582. end;
  1583. Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
  1584. begin
  1585. InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,0);
  1586. end;
  1587. Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
  1588. begin
  1589. Raise EConvertError.CreateFmt(SErrInvalidDateWeek,[AYear,AWeekOfYear,ADayOfWeek]);
  1590. end;
  1591. Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
  1592. begin
  1593. Raise EConvertError.CreateFmt(SErrInvalidDayOfYear,[AYear,ADayOfYear]);
  1594. end;
  1595. Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
  1596. begin
  1597. Raise EConvertError.CreateFmt(SErrInvalidDateMonthWeek,[Ayear,AMonth,AWeekOfMonth,ADayOfWeek]);
  1598. end;
  1599. Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
  1600. begin
  1601. Raise EConvertError.CreateFmt(SErrInvalidDayOfWeekInMonth,[AYear,AMonth,ANthDayOfWeek,ADayOfWeek]);
  1602. end;
  1603. { ---------------------------------------------------------------------
  1604. Julian and Modified Julian Date conversion support
  1605. ---------------------------------------------------------------------}
  1606. {$push}
  1607. {$R-}
  1608. {$Q-}
  1609. Function DateTimeToJulianDate(const AValue: TDateTime): Double;
  1610. var
  1611. day,month,year: word;
  1612. a,y,m: longint;
  1613. begin
  1614. DecodeDate ( AValue, year, month, day );
  1615. a := (14-month) div 12;
  1616. y := year + 4800 - a;
  1617. m := month + (12*a) - 3;
  1618. result := day + ((153*m+2) div 5) + (365*y)
  1619. + (y div 4) - (y div 100) + (y div 400) - 32045.5 + frac(avalue);
  1620. end;
  1621. Function JulianDateToDateTime(const AValue: Double): TDateTime;
  1622. begin
  1623. if not TryJulianDateToDateTime(AValue, Result) then
  1624. raise EConvertError.CreateFmt(SInvalidJulianDate, [AValue]);
  1625. end;
  1626. Function TryJulianDateToDateTime(const AValue: Double; out ADateTime: TDateTime): Boolean;
  1627. var
  1628. a,b,c,d,e,m:longint;
  1629. day,month,year: word;
  1630. begin
  1631. a := trunc(AValue + 32044.5);
  1632. b := (4*a + 3) div 146097;
  1633. c := a - (146097*b div 4);
  1634. d := (4*c + 3) div 1461;
  1635. e := c - (1461*d div 4);
  1636. m := (5*e+2) div 153;
  1637. day := e - ((153*m + 2) div 5) + 1;
  1638. month := m + 3 - 12 * ( m div 10 );
  1639. year := (100*b) + d - 4800 + ( m div 10 );
  1640. result := TryEncodeDate ( Year, Month, Day, ADateTime );
  1641. if Result then
  1642. // ADateTime:=IncMilliSecond(IncHour(ADateTime,-12),MillisecondOfTheDay(Abs(Frac(aValue))));
  1643. ADateTime:=ADateTime+frac(AValue-0.5);
  1644. end;
  1645. Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
  1646. begin
  1647. result := DateTimeToJulianDate(AValue) - 2400000.5;
  1648. end;
  1649. Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
  1650. begin
  1651. result := JulianDateToDateTime(AValue + 2400000.5);
  1652. end;
  1653. Function TryModifiedJulianDateToDateTime(const AValue: Double; out ADateTime: TDateTime): Boolean;
  1654. begin
  1655. Result:=TryJulianDateToDateTime(AValue + 2400000.5, ADateTime);
  1656. end;
  1657. {$pop}//{$R-}{$Q-} for Julian conversion functions
  1658. { ---------------------------------------------------------------------
  1659. Unix timestamp support.
  1660. ---------------------------------------------------------------------}
  1661. Function DateTimeToUnix(const AValue: TDateTime; AInputIsUTC: Boolean = True): Int64;
  1662. Var
  1663. T : TDateTime;
  1664. begin
  1665. T:=aValue;
  1666. if Not aInputisUTC then
  1667. T:=IncMinute(T,GetLocalTimeOffset(AValue));
  1668. Result:=Round(DateTimeDiff(RecodeMillisecond(T,0),UnixEpoch)*SecsPerDay);
  1669. end;
  1670. Function UnixToDateTime(const AValue: Int64; aReturnUTC : Boolean = true): TDateTime;
  1671. begin
  1672. Result:=IncSecond(UnixEpoch, AValue);
  1673. if Not aReturnUTC then
  1674. Result:=IncMinute(Result,-GetLocalTimeOffset(Result));
  1675. end;
  1676. Function UnixTimeStampToMac(const AValue: Int64): Int64;
  1677. const
  1678. Epoch=24107 * 24 * 3600;
  1679. begin
  1680. Result:=AValue + Epoch;
  1681. end;
  1682. { ---------------------------------------------------------------------
  1683. Mac timestamp support.
  1684. ---------------------------------------------------------------------}
  1685. Function DateTimeToMac(const AValue: TDateTime): Int64;
  1686. var
  1687. Epoch:TDateTime;
  1688. begin
  1689. Epoch:=EncodeDateTime( 1904, 1, 1, 0, 0, 0, 0 );
  1690. Result:=SecondsBetween( Epoch, AValue );
  1691. end;
  1692. Function MacToDateTime(const AValue: Int64): TDateTime;
  1693. var
  1694. Epoch:TDateTime;
  1695. begin
  1696. Epoch:=EncodeDateTime( 1904, 1, 1, 0, 0, 0, 0 );
  1697. Result:=IncSecond( Epoch, AValue );
  1698. end;
  1699. Function MacTimeStampToUnix(const AValue: Int64): Int64;
  1700. const
  1701. Epoch=24107 * 24 * 3600;
  1702. begin
  1703. Result:=AValue - Epoch;
  1704. end;
  1705. Function DateTimeToDosDateTime(const AValue: TDateTime): longint;
  1706. var year,month,day,hour,min,sec,msec : word;
  1707. zs : longint;
  1708. begin
  1709. decodedatetime(avalue,year,month,day,hour,min,sec,msec);
  1710. result:=-1980;
  1711. result:=result+year and 127;
  1712. result:=result shl 4;
  1713. result:=result+month;
  1714. result:=result shl 5;
  1715. result:=result+day;
  1716. result:=result shl 16;
  1717. zs:=hour;
  1718. zs:=zs shl 6;
  1719. zs:=zs+min;
  1720. zs:=zs shl 5;
  1721. zs:=zs+sec div 2;
  1722. result:=result+(zs and $ffff);
  1723. end;
  1724. Function DosDateTimeToDateTime( AValue: longint): TDateTime;
  1725. var year,month,day,hour,min,sec : integer;
  1726. begin
  1727. sec:=(AValue and 31) * 2;
  1728. avalue:=AValue shr 5;
  1729. min:=AValue and 63;
  1730. avalue:=AValue shr 6;
  1731. hour:=AValue and 31;
  1732. avalue:=AValue shr 5;
  1733. day:=AValue and 31;
  1734. avalue:=AValue shr 5;
  1735. month:=AValue and 15;
  1736. avalue:=AValue shr 4;
  1737. year:=AValue+1980;
  1738. result:=EncodeDateTime(year,month,day,hour,min,sec,0);
  1739. end;
  1740. {
  1741. Inverse of formatdatetime, destined for the dateutils unit of FPC.
  1742. Limitations/implementation details:
  1743. - An inverse of FormatDateTime is not 100% an inverse, simply because one can put e.g. time tokens twice in the format string,
  1744. and scandatetime wouldn't know which time to pick.
  1745. - Strings like hn can't be reversed safely. E.g. 1:2 (2 minutes after 1) delivers 12 which is parsed as 12:00 and then
  1746. misses chars for the "n" part.
  1747. - trailing characters are ignored.
  1748. - no support for Eastern Asian formatting characters since they are windows only.
  1749. - no MBCS support.
  1750. Extensions
  1751. - #9 eats whitespace.
  1752. - whitespace at the end of a pattern is optional.
  1753. - ? matches any char.
  1754. - Quote the above chars to really match the char.
  1755. }
  1756. const whitespace = [' ',#13,#10];
  1757. hrfactor = 1/(24);
  1758. minfactor = 1/(24*60);
  1759. secfactor = 1/(24*60*60);
  1760. mssecfactor = 1/(24*60*60*1000);
  1761. const AMPMformatting : array[0..2] of string =('am/pm','a/p','ampm');
  1762. procedure raiseexception(const s:string);
  1763. begin
  1764. raise EConvertError.Create(s);
  1765. end;
  1766. function scandatetime(const pattern:string;const s:string;const fmt:TFormatSettings;startpos:integer=1) : tdatetime;
  1767. var len ,ind : integer;
  1768. yy,mm,dd : integer;
  1769. timeval : TDateTime;
  1770. activequote: char;
  1771. procedure intscandate(ptrn:pchar;plen:integer;poffs:integer);
  1772. // poffs is the offset to
  1773. var
  1774. pind : integer;
  1775. function findimatch(const mnts:array of string;p:pchar):integer;
  1776. var i : integer;
  1777. plen, findlen: integer;
  1778. begin
  1779. result:=-1;
  1780. i:=0;
  1781. plen := strlen(p);
  1782. while (i<=high(mnts)) and (result=-1) do
  1783. begin
  1784. findlen := length(mnts[i]);
  1785. if (findlen > 0) and (findlen <= plen) then // protect against buffer over-read
  1786. if AnsiStrLIComp(p,@(mnts[i][1]),findlen)=0 then
  1787. result:=i;
  1788. inc(i);
  1789. end;
  1790. end;
  1791. procedure arraymatcherror;
  1792. begin
  1793. raiseexception(format(SNoArrayMatch,[pind+1,ind]))
  1794. end;
  1795. function scanmatch(const mnts : array of string;p:pchar; patlen: integer):integer;
  1796. begin
  1797. result:=findimatch(mnts,p);
  1798. if result=-1 then
  1799. arraymatcherror
  1800. else
  1801. begin
  1802. inc(ind,length(mnts[result]));
  1803. inc(pind,patlen);
  1804. inc(result); // was 0 based.
  1805. end;
  1806. end;
  1807. var
  1808. pivot,
  1809. i : integer;
  1810. function scanfixedint(maxv:integer):integer;
  1811. var c : char;
  1812. oi:integer;
  1813. begin
  1814. result:=0;
  1815. oi:=ind;
  1816. c:=ptrn[pind];
  1817. while (pind<plen) and (ptrn[pind]=c) do inc(pind);
  1818. while (maxv>0) and (ind<=len) and (s[ind] IN ['0'..'9']) do
  1819. begin
  1820. result:=result*10+ord(s[ind])-48;
  1821. inc(ind);
  1822. dec(maxv);
  1823. end;
  1824. if oi=ind then
  1825. raiseexception(format(SPatternCharMismatch,[c,oi]));
  1826. end;
  1827. procedure matchchar(c:char);
  1828. begin
  1829. if (ind>len) or (s[ind]<>c) then
  1830. raiseexception(format(SNoCharMatch,[s[ind],c,pind+poffs+1,ind]));
  1831. inc(pind);
  1832. inc(ind);
  1833. end;
  1834. function scanpatlen:integer;
  1835. var c : char;
  1836. lind : Integer;
  1837. begin
  1838. result:=pind;
  1839. lind:=pind;
  1840. c:=ptrn[lind];
  1841. while (lind<=plen) and (ptrn[lind]=c) do
  1842. inc(lind);
  1843. result:=lind-result;
  1844. end;
  1845. procedure matchpattern(const lptr:string);
  1846. var len:integer;
  1847. begin
  1848. len:=length(lptr);
  1849. if len>0 then
  1850. intscandate(@lptr[1],len,pind+poffs);
  1851. end;
  1852. var lasttoken,lch : char;
  1853. begin
  1854. pind:=0; lasttoken:=' ';
  1855. while (ind<=len) and (pind<plen) do
  1856. begin
  1857. lch:=upcase(ptrn[pind]);
  1858. if activequote=#0 then
  1859. begin
  1860. if (lch='M') and (lasttoken='H') then
  1861. begin
  1862. i:=scanpatlen;
  1863. if i>2 then
  1864. raiseexception(format(Shhmmerror,[poffs+pind+1]));
  1865. timeval:=timeval+scanfixedint(2)* minfactor;
  1866. end
  1867. else
  1868. case lch of
  1869. 'H': timeval:=timeval+scanfixedint(2)* hrfactor;
  1870. 'D': begin
  1871. i:=scanpatlen;
  1872. case i of
  1873. 1,2 : dd:=scanfixedint(2);
  1874. 3 : dd:=scanmatch(fmt.shortDayNames,@s[ind],i);
  1875. 4 : dd:=scanmatch(fmt.longDayNames,@s[ind],i);
  1876. 5 :
  1877. begin
  1878. matchpattern(fmt.shortdateformat);
  1879. inc(pind, i);
  1880. end;
  1881. 6 :
  1882. begin
  1883. matchpattern(fmt.longdateformat);
  1884. inc(pind, i);
  1885. end;
  1886. end;
  1887. end;
  1888. 'N': timeval:=timeval+scanfixedint(2)* minfactor;
  1889. 'S': timeval:=timeval+scanfixedint(2)* secfactor;
  1890. 'Z': timeval:=timeval+scanfixedint(3)* mssecfactor;
  1891. 'Y': begin
  1892. i:=scanpatlen;
  1893. case i of
  1894. 1,2 : yy:=scanfixedint(2);
  1895. else yy:=scanfixedint(i);
  1896. end;
  1897. if i<=2 then
  1898. begin
  1899. pivot:=YearOf(now)-fmt.TwoDigitYearCenturyWindow;
  1900. inc(yy, pivot div 100 * 100);
  1901. if (fmt.TwoDigitYearCenturyWindow > 0) and (yy < pivot) then
  1902. inc(yy, 100);
  1903. end;
  1904. end;
  1905. 'M': begin
  1906. i:=scanpatlen;
  1907. case i of
  1908. 1,2: mm:=scanfixedint(2);
  1909. 3: mm:=scanmatch(fmt.ShortMonthNames,@s[ind],i);
  1910. 4: mm:=scanmatch(fmt.LongMonthNames,@s[ind],i);
  1911. end;
  1912. end;
  1913. 'T' : begin
  1914. i:=scanpatlen;
  1915. case i of
  1916. 1:
  1917. begin
  1918. matchpattern(fmt.shorttimeformat);
  1919. inc(pind, i);
  1920. end;
  1921. 2:
  1922. begin
  1923. matchpattern(fmt.longtimeformat);
  1924. inc(pind, i);
  1925. end;
  1926. end;
  1927. end;
  1928. 'A' : begin
  1929. i:=findimatch(AMPMformatting,@ptrn[pind]);
  1930. case i of
  1931. 0: begin
  1932. i:=findimatch(['AM','PM'],@s[ind]);
  1933. case i of
  1934. 0: ;
  1935. 1: timeval:=timeval+12*hrfactor;
  1936. else
  1937. arraymatcherror
  1938. end;
  1939. inc(pind,length(AMPMformatting[0]));
  1940. inc(ind,2);
  1941. end;
  1942. 1: begin
  1943. case upcase(s[ind]) of
  1944. 'A' : ;
  1945. 'P' : timeval:=timeval+12*hrfactor;
  1946. else
  1947. arraymatcherror
  1948. end;
  1949. inc(pind,length(AMPMformatting[1]));
  1950. inc(ind);
  1951. end;
  1952. 2: begin
  1953. i:=findimatch([fmt.timeamstring,fmt.timepmstring],@s[ind]);
  1954. case i of
  1955. 0: inc(ind,length(fmt.timeamstring));
  1956. 1: begin
  1957. timeval:=timeval+12*hrfactor;
  1958. inc(ind,length(fmt.timepmstring));
  1959. end;
  1960. else
  1961. arraymatcherror
  1962. end;
  1963. inc(pind,length(AMPMformatting[2]));
  1964. end;
  1965. else // no AM/PM match. Assume 'a' is simply a char
  1966. matchchar(ptrn[pind]);
  1967. end;
  1968. end;
  1969. '/' : matchchar(fmt.dateSeparator);
  1970. ':' : begin
  1971. matchchar(fmt.TimeSeparator);
  1972. lch:=lasttoken;
  1973. end;
  1974. #39,'"' : begin
  1975. activequote:=lch;
  1976. inc(pind);
  1977. end;
  1978. 'C' : begin
  1979. intscandate(@fmt.shortdateformat[1],length(fmt.ShortDateFormat),pind+poffs);
  1980. intscandate(@fmt.longtimeformat[1],length(fmt.longtimeformat),pind+poffs);
  1981. inc(pind);
  1982. end;
  1983. '?' : begin
  1984. inc(pind);
  1985. inc(ind);
  1986. end;
  1987. #9 : begin
  1988. while (ind<=len) and (s[ind] in whitespace) do
  1989. inc(ind);
  1990. inc(pind);
  1991. end;
  1992. else
  1993. matchchar(ptrn[pind]);
  1994. end; {case}
  1995. lasttoken:=lch;
  1996. end
  1997. else
  1998. begin
  1999. if activequote=lch then
  2000. begin
  2001. activequote:=#0;
  2002. inc(pind);
  2003. end
  2004. else
  2005. matchchar(ptrn[pind]);
  2006. end;
  2007. end;
  2008. if (pind<plen) and (plen>0) and not (ptrn[plen-1] in [#9, '"']) then // allow omission of trailing whitespace
  2009. RaiseException(format(SFullpattern,[poffs+pind+1]));
  2010. end;
  2011. var plen:integer;
  2012. begin
  2013. activequote:=#0;
  2014. yy:=0; mm:=0; dd:=0;
  2015. timeval:=0.0;
  2016. len:=length(s); ind:=startpos;
  2017. plen:=length(pattern);
  2018. intscandate(@pattern[1],plen,0);
  2019. result:=timeval;
  2020. if (yy>0) and (mm>0) and (dd>0) then
  2021. result:=result+encodedate(yy,mm,dd);
  2022. end;
  2023. function scandatetime(const pattern:string;const s:string;startpos:integer=1) : tdatetime; overload;
  2024. begin
  2025. result:=scandatetime(pattern,s,defaultformatsettings,startpos);
  2026. end;
  2027. { Conversion of UTC to local time and vice versa }
  2028. function UniversalTimeToLocal(UT: TDateTime): TDateTime;
  2029. begin
  2030. Result:=SysUtils.UniversalTimeToLocal(UT,-GetLocalTimeOffset(UT));
  2031. end;
  2032. function UniversalTimeToLocal(UT: TDateTime; TZOffset : Integer): TDateTime;
  2033. begin
  2034. Result:=SysUtils.UniversalTimeToLocal(UT,TZOffset);
  2035. end;
  2036. Function LocalTimeToUniversal(LT: TDateTime): TDateTime;
  2037. begin
  2038. Result:=SysUtils.LocalTimeToUniversal(LT,-GetLocalTimeOffset(LT));
  2039. end;
  2040. Function LocalTimeToUniversal(LT: TDateTime;TZOffset: Integer): TDateTime;
  2041. begin
  2042. Result:=SysUtils.LocalTimeToUniversal(LT,TZOffset);
  2043. end;
  2044. Const
  2045. FmtUTC = 'yyyy"-"mm"-"dd"T"hh":"nn":"ss"."zzz';
  2046. FmtUTCTZ = 'hh":"mm';
  2047. function DateToISO8601(const ADate: TDateTime; AInputIsUTC: Boolean = True): string;
  2048. const
  2049. FmtOffset: string = '%.02d:%.02d';
  2050. Sign: array[Boolean] of Char = ('+', '-');
  2051. var
  2052. Offset: Integer;
  2053. begin
  2054. Result := FormatDateTime(FmtUTC, ADate);
  2055. Offset := GetLocalTimeOffset(ADate);
  2056. if AInputIsUTC or (Offset=0) then
  2057. Result:=Result+'Z'
  2058. else
  2059. begin
  2060. Result:=Result+Sign[Offset>0];
  2061. Offset := Abs(Offset);
  2062. Result:= Result+Format(FmtOffset, [Offset div MinsPerHour, Offset mod MinsPerHour]);
  2063. end;
  2064. end;
  2065. function TryISOStrToDate(const aString: string; out outDate: TDateTime): Boolean;
  2066. var
  2067. xYear, xMonth, xDay: LongInt;
  2068. begin
  2069. case Length(aString) of
  2070. 8: Result :=
  2071. TryStrToInt(Copy(aString, 1, 4), xYear) and
  2072. TryStrToInt(Copy(aString, 5, 2), xMonth) and
  2073. TryStrToInt(Copy(aString, 7, 2), xDay) and
  2074. TryEncodeDate(xYear, xMonth, xDay, outDate);
  2075. 10: Result :=
  2076. TryStrToInt(Copy(aString, 1, 4), xYear) and
  2077. TryStrToInt(Copy(aString, 6, 2), xMonth) and
  2078. TryStrToInt(Copy(aString, 9, 2), xDay) and
  2079. TryEncodeDate(xYear, xMonth, xDay, outDate);
  2080. else
  2081. Result := False;
  2082. end;
  2083. if not Result then
  2084. outDate := 0;
  2085. end;
  2086. function TryISOStrToTime(const aString: string; Out outTime: TDateTime): Boolean;
  2087. var
  2088. xHour, xMinute, xSecond, xMillisecond, xLength: LongInt;
  2089. begin
  2090. Result := True;
  2091. xLength := Length(aString);
  2092. if (xLength>0) and (aString[xLength] = 'Z') then
  2093. begin
  2094. Dec(xLength);
  2095. end else
  2096. if (xLength>6) and CharInSet(aString[xLength-5], ['+', '-']) then
  2097. begin
  2098. Result :=
  2099. TryStrToInt(Copy(aString, xLength-4, 2), xHour) and
  2100. (aString[xLength-2] = ':') and
  2101. TryStrToInt(Copy(aString, xLength-1, 2), xMinute);
  2102. Dec(xLength, 6);
  2103. end else
  2104. if (xLength>5) and CharInSet(aString[xLength-4], ['+', '-']) then
  2105. begin
  2106. Result :=
  2107. TryStrToInt(Copy(aString, xLength-3, 2), xHour) and
  2108. TryStrToInt(Copy(aString, xLength-1, 2), xMinute);
  2109. Dec(xLength, 5);
  2110. end else
  2111. if (xLength>3) and CharInSet(aString[xLength-2], ['+', '-']) then
  2112. begin
  2113. Result :=
  2114. TryStrToInt(Copy(aString, xLength-1, 2), xHour);
  2115. Dec(xLength, 3);
  2116. end;
  2117. if not Result then
  2118. begin
  2119. outTime := 0;
  2120. Exit;
  2121. end;
  2122. case xLength of
  2123. 2: Result :=
  2124. TryStrToInt(aString, xHour) and
  2125. TryEncodeTime(xHour, 0, 0, 0, outTime);
  2126. 4: Result :=
  2127. TryStrToInt(Copy(aString, 1, 2), xHour) and
  2128. TryStrToInt(Copy(aString, 3, 2), xMinute) and
  2129. TryEncodeTime(xHour, xMinute, 0, 0, outTime);
  2130. 5: Result :=
  2131. TryStrToInt(Copy(aString, 1, 2), xHour) and
  2132. (aString[3] = ':') and
  2133. TryStrToInt(Copy(aString, 4, 2), xMinute) and
  2134. TryEncodeTime(xHour, xMinute, 0, 0, outTime);
  2135. 6: Result :=
  2136. TryStrToInt(Copy(aString, 1, 2), xHour) and
  2137. TryStrToInt(Copy(aString, 3, 2), xMinute) and
  2138. TryStrToInt(Copy(aString, 5, 2), xSecond) and
  2139. TryEncodeTime(xHour, xMinute, xSecond, 0, outTime);
  2140. 8: Result :=
  2141. TryStrToInt(Copy(aString, 1, 2), xHour) and
  2142. (aString[3] = ':') and
  2143. TryStrToInt(Copy(aString, 4, 2), xMinute) and
  2144. (aString[6] = ':') and
  2145. TryStrToInt(Copy(aString, 7, 2), xSecond) and
  2146. TryEncodeTime(xHour, xMinute, xSecond, 0, outTime);
  2147. 10: Result :=
  2148. TryStrToInt(Copy(aString, 1, 2), xHour) and
  2149. TryStrToInt(Copy(aString, 3, 2), xMinute) and
  2150. TryStrToInt(Copy(aString, 5, 2), xSecond) and
  2151. (aString[7] = '.') and
  2152. TryStrToInt(Copy(aString, 8, 3), xMillisecond) and
  2153. TryEncodeTime(xHour, xMinute, xSecond, xMillisecond, outTime);
  2154. 12: Result :=
  2155. TryStrToInt(Copy(aString, 1, 2), xHour) and
  2156. (aString[3] = ':') and
  2157. TryStrToInt(Copy(aString, 4, 2), xMinute) and
  2158. (aString[6] = ':') and
  2159. TryStrToInt(Copy(aString, 7, 2), xSecond) and
  2160. (aString[9] = '.') and
  2161. TryStrToInt(Copy(aString, 10, 3), xMillisecond) and
  2162. TryEncodeTime(xHour, xMinute, xSecond, xMillisecond, outTime);
  2163. else
  2164. Result := False;
  2165. end;
  2166. if not Result then
  2167. outTime := 0;
  2168. end;
  2169. function TryISOStrToDateTime(const aString: string; out outDateTime: TDateTime): Boolean;
  2170. var
  2171. xLength: Integer;
  2172. sDate,sTime : String;
  2173. xDate, xTime: TDateTime;
  2174. begin
  2175. xLength := Length(aString);
  2176. if (xLength>11) and CharInSet(aString[11], [' ', 'T']) then
  2177. begin
  2178. sDate:=Copy(aString, 1, 10);
  2179. sTime:=Copy(aString, 12, Length(aString))
  2180. end
  2181. else if (xLength>9) and CharInSet(aString[9], [' ', 'T']) then
  2182. begin
  2183. sDate:=Copy(aString, 1, 8);
  2184. sTime:=Copy(aString, 10, Length(aString));
  2185. end
  2186. else
  2187. exit(False);
  2188. Result:=TryISOStrToDate(sDate, xDate) and TryISOStrToTime(sTime, xTime);
  2189. if Result then
  2190. outDateTime := xDate+xTime
  2191. else
  2192. outDateTime := 0;
  2193. end;
  2194. Function TryISOTZStrToTZOffset(const TZ : String; Out TZOffset : Integer) : boolean;
  2195. Var
  2196. H,M : LongInt;
  2197. begin
  2198. Result:=(TZ='Z') or (TZ='');
  2199. if Result then
  2200. TZOffset:=0
  2201. else
  2202. begin
  2203. Result:=TZ[1] in ['+','-'];
  2204. if Not Result then
  2205. Exit;
  2206. case Length(TZ) of
  2207. 3: begin
  2208. Result:=TryStrToInt(Copy(TZ,2,2),H);
  2209. M := 0;
  2210. end;
  2211. 5: Result:=TryStrToInt(Copy(TZ,2,2),H) and TryStrToInt(Copy(TZ,4,2),M);
  2212. 6: Result:=TryStrToInt(Copy(TZ,2,2),H) and TryStrToInt(Copy(TZ,5,2),M);
  2213. else
  2214. Result := False;
  2215. end;
  2216. if not Result then
  2217. exit;
  2218. TZOffset:=H*60+M;
  2219. if (TZ[1]='+') then
  2220. TZOffset:=-TZOffset;
  2221. end;
  2222. end;
  2223. Function ISOTZStrToTZOffset(TZ : String) : Integer;
  2224. begin
  2225. if not TryISOTZStrToTZOffSet(TZ,Result) then
  2226. Raise EConvertError.CreateFmt('Invalid ISO timezone string',[TZ]);
  2227. end;
  2228. Function TryISO8601ToDate(const DateString: string; out ADateTime: TDateTime;ReturnUTC : Boolean = True) : Boolean;
  2229. Var
  2230. S,TZ : String;
  2231. L,Offset,TZOffset : Integer;
  2232. begin
  2233. S:=DateString;
  2234. L:=Length(S);
  2235. if L=0 then
  2236. exit(False);
  2237. if S[L]='Z' then
  2238. begin
  2239. TZ:='Z';
  2240. S:=Copy(S,1,L-1);
  2241. end
  2242. else If (L>2) and (S[L-2] in ['+','-']) then
  2243. begin
  2244. TZ:=Copy(S,L-2,3);
  2245. S:=Copy(S,1,L-3);
  2246. end
  2247. else If (L>4) and (S[L-4] in ['+','-']) then
  2248. begin
  2249. TZ:=Copy(S,L-4,5);
  2250. S:=Copy(S,1,L-5);
  2251. end
  2252. else If (L>5) and (S[L-5] in ['+','-']) then
  2253. begin
  2254. TZ:=Copy(S,L-5,6);
  2255. S:=Copy(S,1,L-6);
  2256. end;
  2257. Result:=TryIsoStrToDateTime(S,aDateTime) and TryISOTZStrToTZOffset(TZ,TZOffset);
  2258. if not Result then
  2259. exit;
  2260. aDateTime:=IncMinute(aDateTime,TZOffSet);
  2261. // offset for UTC or not
  2262. if ReturnUTC then
  2263. Offset:=0
  2264. else
  2265. OffSet:=-GetLocalTimeOffset(ADateTime);
  2266. aDateTime:=IncMinute(aDateTime,Offset);
  2267. Result:=True;
  2268. end;
  2269. Function ISO8601ToDate(const DateString: string; ReturnUTC : Boolean = True): TDateTime;
  2270. begin
  2271. if not TryISO8601ToDate(DateString,Result,ReturnUTC) then
  2272. Raise EConvertError.CreateFmt(SErrInvalidTimeStamp,[DateString]);
  2273. end;
  2274. Function ISO8601ToDateDef(const DateString: string; ReturnUTC : Boolean; aDefault : TDateTime): TDateTime;
  2275. begin
  2276. if not TryISO8601ToDate(DateString,Result,ReturnUTC) then
  2277. Result:=aDefault;
  2278. end;
  2279. Function ISO8601ToDateDef(const DateString: string; aDefault : TDateTime; ReturnUTC : Boolean = True): TDateTime;
  2280. begin
  2281. if not TryISO8601ToDate(DateString,Result,ReturnUTC) then
  2282. Result:=aDefault;
  2283. end;
  2284. {$else}
  2285. implementation
  2286. {$endif FPUNONE}
  2287. end.