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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
include <ctype.h>
include <time.h>
# DTMCNV.X -- Date and time conversions.
#
# The file contains the source for only the DTM routines listed below. All
# the related system date and time routines are also summarized so that the
# whole (rather scattered) interface can be viewed at a glance.
#
# FITS-Like Date and Time String Conversions
#
# status = dtm_decode (datestr, y,m,d, h, oldfits)
# nchars = dtm_encode (datestr,maxch, y,m,d, h, precision, flags)
# status = dtm_decode_hms (datestr, y,m,d, h,m,s, oldfits)
# nchars = dtm_encode_hms (datestr,maxch, y,m,d, h,m,s, precision, flags)
# status = dtm_ltime (datestr, ltime)
#
#
# General Date and Time Conversions
#
# cnvdate (ltime, outstr, maxch)
# cnvtime (ltime, outstr, maxch)
# brktime (ltime, tm)
#
# System Time
#
# lval = clktime (old_time) # returns local time
# lval = cputime (old_cputime) # process cpu time, seconds
# gmt = lsttogmt (lst) # lst/gmt are in seconds
# lst = gmttolst (gmt) # lst/gmt are in seconds
#
# sys_mtime (save_time) # mark/print cpu time used
# sys_ptime (fd, opstr, save_time)
#
#
# Kernel Support
#
# zgtime (clktime, cputime) # clock/cpu time in seconds
# zgmtco (gmtcor) # GMT = LST + gmtco (seconds)
#
# LST here means local standard time (clock time), including any correction
# for daylight savings time.
# DTM_DECODE -- Decode the FITS format DATE-OBS string value into year,
# month, day and time fields. OK is returned if the date string is
# successfully decoded, ERR if it is not. The DATE-OBS string value may be
# in any of the following forms: DD/MM/YY (flags = TF_OLDFITS), CCYY-MM-DD
# (flags = 0, time = INDEFD), or CCYY-MM-DDTHH:MM:SS[.SSS...] (flags=0,
# time = double precision number). This routine verifies only the syntax.
# Routines in the SLALIB or ASTUTIL libraries can be used to check for
# valid year, month, day, or time values.
int procedure dtm_decode (datestr, year, month, day, time, flags)
char datestr[ARB] #I the input date-obs string
int year #O the output year (INDEFI if undefined)
int month #O the output month (INDEFI if undefined)
int day #O the output day (INDEFI if undefined)
double time #O the output time in hours (INDEFD if undefined)
int flags #O see <time.h>
double dval
int oldfits, ip, nchars, ival
int ctoi(), ctod()
begin
# Initialize.
year = INDEFI
month = INDEFI
day = INDEFI
time = INDEFD
flags = 0
# Determine whether the format is old or new and get the day or
# month accordingly.
ip = 1
nchars = ctoi (datestr, ip, ival)
if (nchars == 2) {
flags = or (flags, TF_OLDFITS)
oldfits = YES
day = ival
} else if (nchars == 4) {
flags = and (flags, not(TF_OLDFITS))
oldfits = NO
year = ival
} else
return (ERR)
# Check syntax.
if (oldfits == NO && datestr[ip] == '-')
ip = ip + 1
else if (oldfits == YES && datestr[ip] == '/')
ip = ip + 1
else
return (ERR)
# Get the month
nchars = ctoi (datestr, ip, ival)
if (nchars == 2) {
month = ival
} else
return (ERR)
if (oldfits == NO && datestr[ip] == '-')
ip = ip + 1
else if (oldfits == YES && datestr[ip] == '/')
ip = ip + 1
else
return (ERR)
# Get the year or day.
nchars = ctoi (datestr, ip, ival)
if (nchars == 2) {
if (oldfits == YES)
year = 1900 + ival
else
day = ival
} else
return (ERR)
if (datestr[ip] != 'T' || oldfits == YES)
return (OK)
# Get the time.
ip = ip + 1
nchars = ctod (datestr, ip, dval)
if (nchars < 8)
return (ERR)
else
time = dval
# Check for trailing garbage in the input string. Ignore whitespace.
while (IS_WHITE(datestr[ip]))
ip = ip + 1
if (datestr[ip] != EOS)
return (ERR)
else
return (OK)
end
# DTM_DECODE_HMS -- Decode a FITS format DATE-OBS string into year, month,
# day, hours, minutes, and seconds fields. OK is returned if the date string
# is successfully decoded, ERR if it is not. The DATE-OBS string value may
# be in any of the following forms: DD/MM/YY (oldfits = YES), CCYY-MM-DD
# (oldfits = NO, hours = INDEFI, minutes = INDEFI, seconds = INDEFD), or
# CCYY-MM-DDTHH:MM:SS[.SSS...] (oldfits = NO, hours = integer, minutes =
# integer, seconds = double precision number). This routine verifies only
# that the syntax is correct. Routines in the SLALIB or ASTUTIL libraries
# can be used to check for valid year, month, day, or time values.
int procedure dtm_decode_hms (datestr,
year, month, day, hours, minutes, seconds, flags)
char datestr[ARB] #I the input date-obs string
int year #O the output year (INDEFI if undefined)
int month #O the output month (INDEFI if undefined)
int day #O the output day (INDEFI if undefined)
int hours #O the output hours (INDEFI if undefined)
int minutes #O the output minutes (INDEFI if undefined)
double seconds #O the output seconds (INDEFD if undefined)
int flags #O see <time.h>
double dval
int oldfits, ip, nchars, ival
int ctoi(), ctod()
begin
# Initialize.
year = INDEFI
month = INDEFI
day = INDEFI
hours = INDEFI
minutes = INDEFI
seconds = INDEFD
flags = 0
# Determine whether the format is old or new and get the day
# or month accordingly.
ip = 1
nchars = ctoi (datestr, ip, ival)
if (nchars == 2) {
flags = or (flags, TF_OLDFITS)
oldfits = YES
day = ival
} else if (nchars == 4) {
flags = and (flags, not(TF_OLDFITS))
oldfits = NO
year = ival
} else
return (ERR)
# Check syntax.
if (oldfits == NO && datestr[ip] == '-')
ip = ip + 1
else if (oldfits == YES && datestr[ip] == '/')
ip = ip + 1
else
return (ERR)
# Get the month.
nchars = ctoi (datestr, ip, ival)
if (nchars == 2) {
month = ival
} else
return (ERR)
if (oldfits == NO && datestr[ip] == '-')
ip = ip + 1
else if (oldfits == YES && datestr[ip] == '/')
ip = ip + 1
else
return (ERR)
# Get the year or day.
nchars = ctoi (datestr, ip, ival)
if (nchars == 2) {
if (oldfits == YES)
year = 1900 + ival
else
day = ival
} else
return (ERR)
if (datestr[ip] != 'T' || oldfits == YES)
return (OK)
# Get the hours.
ip = ip + 1
nchars = ctoi (datestr, ip, ival)
if (nchars == 2)
hours = ival
else
return (ERR)
if (datestr[ip] != ':')
return (ERR)
# Get the minutes.
ip = ip + 1
nchars = ctoi (datestr, ip, ival)
if (nchars == 2)
minutes = ival
else
return (ERR)
if (datestr[ip] != ':')
return (ERR)
# Get the seconds.
ip = ip + 1
nchars = ctod (datestr, ip, dval)
if (nchars < 2)
return (ERR)
else
seconds = dval
# Check for trailing garbage in the input string. Ignore whitespace.
while (IS_WHITE(datestr[ip]))
ip = ip + 1
if (datestr[ip] != EOS)
return (ERR)
else
return (OK)
end
# DTM_ENCODE -- Encode year, month, day and time fields into a valid FITS
# format DATE-OBS string value. The number of characters in the output
# string is returned as the function value. The returned DATE-OBS keyword
# value may be in any of the following formats: DD/MM/YY (oldfits = YES,
# 1900 <= year < 2000), CCYY-MM-DD (oldfits = NO, time = INDEFD), or
# CCYY-MM-DDTHH:MM:SS[.SSS...] (oldfits = NO, time = double precision
# number). This routine formats the string but does not check for valid
# input values. Routines in the SLALIB or ASTUTIL libraries can be used
# to create valid year, month, day, or time values.
int procedure dtm_encode (datestr, maxch,
year, month, day, time, precision, flags)
char datestr[ARB] #O the output date string
int maxch #I the maximum length of the output date string
int year #I the input year, e.g. 1999
int month #I the input month, e.g. 1-12
int day #I the input day, e.g. 1-31
double time #I the input time in hours, INDEFD if undefined
int precision #I the precision of the output time field
int flags #I see <time.h>
int oldfits, field
int strlen(), btoi()
begin
datestr[1] = EOS
oldfits = btoi (and (flags, TF_OLDFITS) != 0)
if (oldfits == YES) {
if (year >= 1900 && year < 2000) {
call sprintf (datestr, maxch, "%02d/%02d/%02d")
call pargi (day)
call pargi (month)
call pargi (mod (year, 1900))
}
} else if (IS_INDEFD(time)) {
call sprintf (datestr, maxch, "%04d-%02d-%02d")
call pargi (year)
call pargi (month)
call pargi (day)
} else {
if (precision <= 0)
field = 8
else
field = 9 + precision
call sprintf (datestr, maxch, "%04d-%02d-%02dT%0*.*h")
call pargi (year)
call pargi (month)
call pargi (day)
call pargi (field)
call pargi (precision)
call pargd (time)
}
return (strlen (datestr))
end
# DTM_ENCODE_HMS -- Encode year, month, day, hours, minutes, and seconds
# fields into a valid FITS format DATE-OBS string value. The number of
# characters in the output string is returned as the function value. The
# returned DATE-OBS keyword value may be in any of the following formats:
# DD/MM/YY (oldfits = YES, 1900 <= year < 2000), CCYY-MM-DD (oldfits = NO,
# time = INDEFD), or CCYY-MM-DDTHH:MM:SS[.SSS...] (oldfits = NO, time =
# double precision number). This routine formats the string but does not
# check for valid input values. Routines in the SLALIB or ASTUTIL libraries
# can be used to create valid year, month, day, or time values.
int procedure dtm_encode_hms (datestr, maxch,
year, month, day, hours, minutes, seconds, precision, flags)
char datestr[ARB] #O the output date string
int maxch #I the maximum length of the output date string
int year #I the input year, e.g. 1999
int month #I the input month, e.g. 1-12
int day #I the input day, e.g. 1-31
int hours #I the input hours field, INDEFI if undefined
int minutes #I the input minutes field, INDEFI if undefined
double seconds #I the input seconds field, INDEFD if undefined
int precision #I the precision of the output time field
int flags #I see <time.h>
int oldfits, field
int strlen(), btoi()
begin
datestr[1] = EOS
oldfits = btoi (and (flags, TF_OLDFITS) != 0)
if (oldfits == YES) {
if (year >= 1900 && year < 2000) {
call sprintf (datestr, maxch, "%02d/%02d/%02d")
call pargi (day)
call pargi (month)
call pargi (mod (year, 1900))
}
} else if (IS_INDEFI(hours) || IS_INDEFI(minutes) ||
IS_INDEFD(seconds)) {
call sprintf (datestr, maxch, "%04d-%02d-%02d")
call pargi (year)
call pargi (month)
call pargi (day)
} else {
if (precision <= 0)
field = 2
else
field = 3 + precision
call sprintf (datestr, maxch, "%04d-%02d-%02dT%02d:%02d:%0*.*f")
call pargi (year)
call pargi (month)
call pargi (day)
call pargi (hours)
call pargi (minutes)
call pargi (field)
call pargi (precision)
call pargd (seconds)
}
return (strlen (datestr))
end
# DTM_LTIME -- Decode a FITS format DATE-OBS string into the number of
# seconds since 00:00:00 01-Jan-1980. OK is returned if the date string
# is successfully decoded, ERR if it is not or if it is a negative value.
# The 'datestr' string value may be in any of the following forms: DD/MM/YY
# or CCYY-MM-DD (where time is INDEF and assumed to be midnight), or as
# CCYY-MM-DDTHH:MM:SS[.SSS...].
int procedure dtm_ltime (datestr, ltime)
char datestr[ARB] #I the input date string
long ltime #O seconds since 00:00:00 01-Jan-1980
double sec
int oldfits, ndays
int hr, min, yr, mon, day
double dtm_date_to_julday()
int dtm_decode_hms()
define START_IRAF_EPOCH 2444239.5 # JD of 00:00:00 01-Jan-1980
define SECONDS_PER_DAY 86400
define SECONDS_PER_HOUR 3600
define SECONDS_PER_MINUTE 60
begin
ltime = INDEFL # initialize
if (dtm_decode_hms (datestr, yr,mon,day, hr,min,sec, oldfits) == ERR)
return (ERR)
# Take care of the assumption that 2-digit years are 1900.
if (oldfits == YES)
yr = yr + 100
# If we had a time specified, convert it to the number of seconds
# that day.
if (IS_INDEFI(hr) || IS_INDEFI(min) || IS_INDEFD(sec))
ltime = 0
else
ltime = (hr * SECONDS_PER_HOUR) + (min * SECONDS_PER_MINUTE) + sec
# Compute the number of days since the start of the iraf epoch.
ndays = dtm_date_to_julday (yr, mon, day, 0.0d0) - START_IRAF_EPOCH
# Convert days to seconds, add to time from before.
ltime = ltime + (ndays * SECONDS_PER_DAY)
if (ltime >= 0)
return (OK)
else
return (ERR)
end
# DTM_DATE_TO_JULDAY -- Convert date to Julian day. Assumes dates after year 99.
double procedure dtm_date_to_julday (year, month, day, t)
int year # Year
int month # Month (1-12)
int day # Day of month
double t # Time for date (mean solar day)
double jd
int y, m, d
begin
if (year < 100)
y = 1900 + year
else
y = year
if (month > 2)
m = month + 1
else {
m = month + 13
y = y - 1
}
jd = int (365.25D0 * y) + int (30.6001 * m) + day + 1720995
if (day + 31 * (m + 12 * y) >= 588829) {
d = int (y / 100)
m = int (y / 400)
jd = jd + 2 - d + m
}
jd = jd - 0.5 + int (t * 360000. + 0.5) / 360000. / 24.
return (jd)
end
|