View Raw SPL
/****************************************************************************
* *
* DT2UNIX.SPL Copyright 2021 (C) DSP Development Corporation *
* *
* Author: Randy Race *
* *
* Synopsis: Converts Julian date and seconds to UNIX timestamp s *
* *
* Revisions: 18 Jan 2021 RRR Creation *
* *
****************************************************************************/
#if @HELP_DT2UNIX
DT2UNIX
Purpose: Converts Julian date and clock seconds to UNIX timestamps.
Syntax: DT2UNIX(date, time)
date - Optional. A string or integer Julian date, the
input date. Defaults to the current date.
time - Optional. A string or real TOD, the input time. Defaults
to the current time.
Returns: A scalar or series of Unix timestamps
Example:
udt = dt2unix("1-1-2021", "12:00:00");
dtu = unix2dt(edt);
udt == 1609502400.0
dtu == "1/1/2021 12:00:00.000"
Example:
W1: {julstr("1-1-1999"), julstr("1-10-1999"), julstr("4-2-1999")}
W2: {todstr("12:00"), todstr("14:00"), todstr("9:35")}
W3: dt2unix(w1, w2)
W4: unix2dt(w3)
W3 == {915192000.0, 915976800.0, 923045700.0}
W4 contains the date and time values:
01/01/1999 12:00:00.000
01/10/1999 14:00:00:000
04/02/1999 9:35:00.000
Remarks:
A Unix timestamp is a real value that represents the number
of seconds that have elapsed since midnight January 1, 1970 UTC
time. The integer part is the whole number of days and the
fractional part is the fraction of a day.
See UNIX2DT to convert an Unix timestamp to a Julian date and
clock time value.
See DT2TIMESTAMP to convert date time values to internal
timestamps suitable for date time arithmetic.
See Also:
DT2TIMESTAMP
DTXY
JULSTR
LOCAL2UTC
STRJUL
STRTOD
TIMESTAMP2DT
TODSTR
UNIX2DT
UTC2LOCAL
XYDT
#endif
/* convert date and time to unix timestamp */
dt2unix(date = getdate(), time = getutctime(), startdate = "1-1-1970")
{
local unixdt;
/* parse inputs */
(date, time, startdate) = dt2_parse_args(date, time, startdate);
/* start date as julian */
startjul = julstr(startdate);
/* unix seconds for each pair of date/time columns */
unixdt = dt2unix_iterate(date, time, startjul);
return(unixdt);
}
/* iterate on pairs of date/time series */
ITERATE dt2unix_iterate(date, time, startjul)
{
local unixdt;
/* unix date offset */
unixdt = (date - startjul) * 86400 + time;
if (isarray(unixdt))
{
setmatrix(unixdt, 0);
setvunits(unixdt, "s");
}
return(unixdt);
}