-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSDateTime.e
447 lines (331 loc) · 12.3 KB
/
TSDateTime.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
-----------------------------------------------------------------------------
-- --
-- DateTime.e - Routines for handling Date and Time - (c) 2001 CyrekSoft --
-- --
-----------------------------------------------------------------------------
--// Modified by Tone Skoda, code within "Start Tone Skoda" and "End Tone Skoda"
-- include machine.e -- segment:
constant M_CRASH_MESSAGE = 37
procedure crash_message(sequence msg)
machine_proc(M_CRASH_MESSAGE, msg & "\n--> see ex.err\n")
end procedure
-- Constants and Type Declarations ------------------------------------------
-- Change this to 1 for extended leap year rules
global integer XLEAP
XLEAP = 0
constant--s
Gregorian_Reformation = 1752,
Gregorian_Reformation00 = 1700,
DaysPerMonth = {
31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31
},
AverageDaysPerMonth = 30.43687604,
AverageDaysPerYear = 12 * AverageDaysPerMonth
global constant--s
-- Zero Date -- 1 Jan 1AD
Date_0 = {1, 1, 1},
-- Zero Time (or possibly Midnight)
Time_0 = {0, 0, 0},
-- Zero DateTime
DateTime_0 = {Date_0, Time_0},
-- Date/Time/DateTime datatype indices
DT_DATE = 1, YEAR = 1, MONTH = 2, DAY = 3, JDAY = 2,
DT_TIME = 2, HOURS = 1, MINUTES = 2, SECONDS = 3,
-- 1970 Epoch DateTime
EPOCH_1970 = { { 1970, 1, 1 }, Time_0 },
DayLengthInSeconds = 86400
-- Dates
type year_(object y) if integer(y) and
-2500000 <= y and y <= 2500000
then return 1 end if return 0 end type
type month_(object m) if integer(m) and
1 <= m and m <= 12
then return 1 end if return 0 end type
type day_(object d) if integer(d) and
1 <= d and d <= 31
then return 1 end if return 0 end type
type jday_(object d) if integer(d) and
1 <= d and d <= 366
then return 1 end if return 0 end type
global type Date(object ymd) if sequence(ymd) and length(ymd) = 3 and
year_(ymd[YEAR]) and month_(ymd[MONTH]) and day_(ymd[DAY])
then return 1 end if return 0 end type
global type JDate(object ymd) if sequence(ymd) and length(ymd) = 2 and
year_(ymd[YEAR]) and jday_(ymd[JDAY])
then return 1 end if return 0 end type
-- Times
type hour_(object h) if integer(h) and
0 <= h and h <= 23
then return 1 end if return 0 end type
type minute_(object m) if integer(m) and
0 <= m and m <= 59
then return 1 end if return 0 end type
type seconds_(object s) if atom(s) and
0 <= s and s < 60
then return 1 end if return 0 end type
global type Time(object hms) if sequence(hms) and length(hms) = 3 and
hour_(hms[HOURS]) and minute_(hms[MINUTES]) and seconds_(hms[SECONDS])
then return 1 end if return 0 end type
-- Date and Time combined data type
global type DateTime(object dt) if sequence(dt) and length(dt) = 2 and
Date(dt[DT_DATE]) and Time(dt[DT_TIME])
then return 1 end if return 0 end type
-- Date Handling ------------------------------------------------------------
-- 'Now'
global function nowDate() -- returns a Date
sequence now now = date()
return (now[1] + 1900) & now[2..3]
end function
-- Basic functions
global function isLeap(year_ year) -- returns integer (0 or 1)
sequence ly
ly = (remainder(year, {4, 100, 400, 3200, 80000})=0)
if not ly[1] then return 0 end if
if year <= Gregorian_Reformation then
return 1 -- ly[1] can't possibly be 0 here so set shortcut as '1'.
elsif XLEAP then
return ly[1] - ly[2] + ly[3] - ly[4] + ly[5]
else -- Standard Gregorian Calendar
return ly[1] - ly[2] + ly[3]
end if
end function
global function daysInMonth(year_ year, month_ month) -- returns a month_
if year = Gregorian_Reformation and month = 9 then
return 19
elsif month != 2 then
return DaysPerMonth[month]
else
return DaysPerMonth[month] + isLeap(year)
end if
end function
global function daysInYear(year_ year) -- returns a jday_ (355, 365 or 366)
if year = Gregorian_Reformation then
return 355
end if
return 365 + isLeap(year)
end function
-- Functions using the new data-types
global function julianDayOfYear(Date ymd) -- returns an integer
integer year, month, day
integer d
year = ymd[YEAR]
month = ymd[MONTH]
day = ymd[DAY]
if month = 1 then return day end if
d = 0
for i = 1 to month - 1 do
d += daysInMonth(year, i)
end for
d += day
if year = Gregorian_Reformation and month = 9 and
2 < day and day < 14
then
d -= 11
end if
return d
end function
global function julianDateInYear(JDate yd) -- returns a Date
integer year, d
year = yd[YEAR]
d = yd[JDAY]
-- guess month
if d <= daysInMonth(year, 1) then
return {year, 1, d}
end if
for month = 2 to 12 do
d -= daysInMonth(year, month-1)
if d <= daysInMonth(year, month) then
return {year, month, d}
end if
end for
-- we should never get here
crash_message("julianDateInYear(): oops! Things haven't quite worked out!")
end function
global function JDateToDate(object o) -- Converts a JDate to a Date
return julianDateInYear(o) -- we're just aliasing jDIY here
end function
global function DateToJDate(Date d) -- Converts a Date to a JDate
return d[YEAR] & julianDayOfYear(d)
end function
global function julianDay(Date ymd) -- returns an integer
integer year
integer j, greg00
year = ymd[YEAR]
j = julianDayOfYear(ymd)
year -= 1
greg00 = year - Gregorian_Reformation00
j += (
365 * year
+ floor(year/4)
+ (greg00 > 0)
* (
- floor(greg00/100)
+ floor(greg00/400+.25)
)
- 11 * (year >= Gregorian_Reformation)
)
if XLEAP then
j += (
- (year >= 3200) * floor(year/ 3200)
+ (year >= 80000) * floor(year/80000)
)
end if
return j
end function
global function julianDate(integer j) -- returns a Date
integer year, doy
-- Take a guesstimate at the year -- this is usually v.close
if j >= 0 then
year = floor(j / AverageDaysPerYear) + 1
else
year = -floor(-j / 365.25) + 1
end if
-- Calculate the day in the guessed year
doy = j - (julianDay({year, 1, 1}) - 1) -- = j - last day of prev year
-- Correct any errors
-- The guesstimate is usually so close that these whiles could probably
-- be made into ifs, but I haven't checked all possible dates yet... ;)
while doy <= 0 do -- we guessed too high for the year
year -= 1
doy += daysInYear(year)
end while
while doy > daysInYear(year) do -- we guessed too low
doy -= daysInYear(year)
year += 1
end while
return julianDateInYear({year, doy})
end function
-- Date Math I
with trace
global function daysDifference(Date past, Date future) -- returns an integer
--trace(1)
return julianDay(future) - julianDay(past)
end function
global function daysSince(Date past) -- returns an integer
Date now now = nowDate()
return daysDifference(past, now)
end function
global function daysUntil(Date future) -- returns an integer
return -daysSince(future)
end function
-- Date Math II
global function addToDate(Date a, integer b) -- returns a Date
return julianDate(julianDay(a) + b)
end function
global function subFromDate(Date a, integer b) -- returns a Date
return julianDate(julianDay(a) - b)
end function
-- Super-strict types -- also usable as boolean functions
global type ValidDate(object d) if Date(d) and
equal(d, julianDate(julianDay(d)))
then return 1 end if return 0 end type
global type ValidDateTime(object dt) if DateTime(dt) and
ValidDate(dt[DT_DATE]) -- and Time(dt[DT_TIME])
then return 1 end if return 0 end type
-- Day of week
function clock7(integer number) -- returns an integer (1..7)
return remainder(number+4094, 7)+1
-- modulo(number-1, 7)+1 would be better. Hence adding a few multiples
-- of 7 to the -1 in the remainder() call
end function
global function dayOfWeek(Date ymd) -- returns an integer
return clock7(julianDay(ymd)-1)
-- Sun..Sat = 1..7
end function
-- Time calculations --------------------------------------------------------
-- 'Now'
global function nowTime() -- returns a Time
sequence now now = date()
return now[4..6]
end function
-- Conversions to and from seconds
global function hmsToSeconds(Time hms) -- returns an atom
return (hms[HOURS] * 60 + hms[MINUTES]) * 60 + hms[SECONDS]
end function
global function secondsToHms(atom in) -- returns a Time
integer hours, minutes
atom secs
in = remainder(in, DayLengthInSeconds)
if in < 0 then in += DayLengthInSeconds end if
secs = remainder(in, 60)
in = floor(in / 60)
minutes = remainder(in, 60)
hours = remainder(floor(in / 60), 24)
--// secs = remainder (in, 60)
--// minutes = remainder (floor (in / 60), 60)
--// hours = floor (in / (60 * 60))
--// End Tone Skoda
return {hours, minutes, secs}
end function
-- Time Math I -- See DateTime Math I
-- Time Math II
global function addToTime(Time a, object b) -- returns a Time
if Time(b) then b = hmsToSeconds(b) end if
if atom(b) then
return secondsToHms(hmsToSeconds(a) + b)
end if
crash_message("Expected an atom or a Time as second parameter in addToTime()")
end function
global function subFromTime(Time a, object b) -- returns a Time
if Time(b) then b = hmsToSeconds(b) end if
if atom(b) then
return secondsToHms(hmsToSeconds(a) - b)
end if
crash_message("Expected an atom or a Time as second parameter in subFromTime()")
end function
-- DateTime Calculations ----------------------------------------------------
-- 'Now'
global function nowDateTime()
sequence now now = date()
return {now[1..3] + {1900,0,0}, now[4..6]}
end function
-- Conversions to and from seconds
global function DateTimeToSeconds(DateTime dt) -- returns an atom
return
+ julianDay(dt[DT_DATE]) * DayLengthInSeconds
+ hmsToSeconds(dt[DT_TIME])
end function
global function secondsToDateTime(atom seconds) -- returns a DateTime
integer days
days = floor(seconds / DayLengthInSeconds)
seconds = remainder(seconds, DayLengthInSeconds)
return {julianDate(days), secondsToHms(seconds)}
end function
-- Conversions to and from Dates
global function DateToDateTime(Date d) -- returns a DateTime
return {d, Time_0}
end function
global function DateTimeToDate(DateTime dt) -- returns a Date
return dt[DT_DATE] -- Warning! Loss of DT_TIME information!
end function
-- DateTime Math I
global function secondsDifference(object past, object future)
if Time(past) and Time(future) then
return hmsToSeconds(future) - hmsToSeconds(past)
elsif DateTime(past) and DateTime(future) then
return DateTimeToSeconds(future) - DateTimeToSeconds(past)
end if
crash_message("Expected matching DateTimes or Times in secondsDifference()")
end function
-- DateTime Math II
global function addToDateTime(DateTime a, object b) -- returns a DateTime
if Time(b) then b = hmsToSeconds(b) end if
if atom(b) then return secondsToDateTime(DateTimeToSeconds(a) + b) end if
crash_message("Expected an atom or a Time as second parameter in addToDateTime()")
end function
global function subFromDateTime(DateTime a, object b) -- returns a DateTime
if Time(b) then b = hmsToSeconds(b) end if
if atom(b) then return secondsToDateTime(DateTimeToSeconds(a) - b) end if
crash_message("Expected an atom or a Time as second parameter in subFromDateTime()")
end function
-- Variable Epoch calculations ----------------------------------------------
global DateTime Epoch
Epoch = EPOCH_1970 -- set default
global function secondsSinceEpoch(atom seconds) -- returns an atom
return seconds - DateTimeToSeconds(Epoch)
end function
global function EpochTimeTo1ADTime(atom eseconds) -- returns an atom
return eseconds + DateTimeToSeconds(Epoch)
end function