View Raw SPL
/****************************************************************************
*                                                                           *
*   DT2TIMESTAMP.SPL Copyright 2024 (C) DSP Development Corporation         *
*                                                                           *
*   Author:      Randy Race                                                 *
*                                                                           *
*   Synopsis:    Converts date and seconds to internal timestamps           *
*                                                                           *
*   Revisions:   10 Nov 2024     RRR     Creation                           *
*                                                                           *
****************************************************************************/


#if @HELP_DT2TIMESTAMP

    DT2TIMESTAMP

    Purpose: Converts date and times to internal timestamps.

    Syntax:  DT2TIMESTAMP(date, time, "timezone")

                    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.

              "timezone" - Optional. A string, the local time zone. Defaults
                           to the current time zone. If specified, the
                           resulting timestamps are based on the UTC time
                           offset UTC time.

    Alternate Syntax:  DT2TIMESTAMP(datetime, "timezone")

              datetime - An array, the date and time values in adjacent
                         columns.

              "timezone" - Optional. A string, the local time zone. Defaults
                           to the current time zone. If specified, the
                           timestamps are based on UTC time.

    Returns: A scalar or series of internal timestamps

    Example:
             ts = dt2timestamp("1-1-2021", "12:00:00");
             dt = timestamp2dt(ts);

             ts == 1609502400.0
             dt == "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: dt2timestamp(w1, w2)
             W4: timestamp2dt(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

    Example:
             W1: {ymd2date(1999, 1, 1), ymd2date(1999, 1, 10), ymd2date(1999, 4, 2)}
             W2: {hms2time(12, 0, 0), hms2time(14, 0, 0), hms2time(9, 35, 0)}
             W3: dt2timestamp(w1, w2)
             W4: timestamp2dt(w3)

             Same as above except the original date and time data are
             entered in YMD and HMS format.

             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
    Example:
             W1: grand(3, 1);settime(w0, "0:00:00.125");setdate(w0, "12-01-2024")
             W2: getdt(w1)
             W3: dt2timestamp(w2) - 60 * 3
             W4: timestamp2dt(w3)

             W1 contains a 3 sample random series with a start date of 125
             milliseconds after midnight on 12/1/2024.

             W2 obtains the date and time values for each sample in W1 and
             contains:

              12-01-2024  0:00:00.125
              12-01-2024  0:00:01.125
              12-01-2024  0:00:02.125   

             W3 converts the date time values in W2 into timestamps. The
             timestamps are decreased by 180 seconds or 3 minutes.

             W3 == {1733011020.125, 1733011021.125, 1733011022.125}

             W4 converts the modified timestamps in W3 into date time values
             and contains:

              11-30-2024  23:57:00.125
              11-30-2024  23:57:01.125
              11-30-2024  23:57:02.125

             The resulting date and time values are 3 minutes earlier than
             the original timestamps in W2.

    Example:
             W1: grand(3, 1);settime(w0, "0:00:00.125");setdate(w0, "12-01-2024")
             W2: getdt(w1)
             W3: dt2timestamp(w2, "America/New_York") - 60 * 3
             W4: timestamp2dt(w3, "America/Los_Angeles")

             Same as above except now the original date time values are marked
             as having originated in the Eastern Time zone. The values are
             increased by 3 minutes and then converted into date time values
             in the Pacific Time zone. W4 contains the date time values:

              11-30-2024  20:57:00.125
              11-30-2024  20:57:01.125
              11-30-2024  20:57:02.125

    Example:
             W1: gnorm(1000, 1.0);settime(w0, "12:00:00")
             W2: gnorm(1000, 1/2);settime(w0, "12:00:00")
             W3: getdt(w1)
             W4: getdt(w2)
             W5: dt2timestamp(w3)
             W6: dt2timestamp(w4)
             W7: w5 - w6
             W8: diff(w7)

             W1 and W2 contain 1000 samples of random data both with a
             start time of noon.

             The sample rate of W1 is 1 Hz and the sample rate of W2 is 2 Hz.

             W3 and W4 obtain the date and time data for each sample of
             W1 and W2.

             W5 and W6 convert the date and time data to timestamps.

             W7 computes the time difference in seconds between each
             sample of W1 and W2. The time difference increases uniformly
             by 0.5 seconds indicating the samples of W1 lag the samples
             of W2 in time. For example, from W3 and W4:
             
               Sample 6 of W1 occurs at 12:00:05.000
               Sample 6 of W2 occurs at 12:00:02.500
             
             Thus, sample 6 of W1 occurs 2.5 seconds later than sample 6
             of W2.

             W7 reports the same time difference of 2.5 seconds between
             sample 6 of W1 and W2.

             W8 computes the difference between subsequent time lags. The
             difference is constant at 0.5 seconds indicating each
             sample of W1 lags W2 by an additional 0.5 seconds as time
             progresses. For example, from W3 and W4:
             
               Sample 7 of W1 occurs at 12:00:06.000
               Sample 7 of W2 occurs at 12:00:03.000

             Sample 7 of W1 occurs 3.0 seconds later than sample 7
             of W2. This is 0.5 seconds greater than the difference between
             samples 6 as previously shown.

    Remarks:
             An internal 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.

             The TIMEZONE string is case sensitive should adhere to the the
             IANA timezone identifiers available at:

               https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

             and:
      
               https://www.iana.org/time-zones

             If TIMEZONE is blank or "local", the local time zone is used.

             As shown in the last example, DT2TIMESTAMP can be used to perform
             simple date/time arithmetic.

             See TIMESTAMP2DT to convert an internal timestamp to a Julian date
             and clock time value.

    See Also:
             DT2UNIX
             DT2YMDHMS
             GETDT
             JULSTR
             LOCAL2UTC
             STRJUL
             STRTOD
             TIMESTAMP2DT
             TODSTR
             UNIX2DT
             UTC2LOCAL
             YMD2DT
             YMDHMS2DT
#endif


/* convert date and time to unix timestamp */
dt2timestamp(date = getdate(), time = gettime(13), timezone = "")
{
        local timestamp, isstring;

        (date, time, timezone, isstring) = utc_parse_args(date, time, timezone, TRUE, FALSE);

        if (strlen(timezone) > 0)
        {
                /* convert to UTC */
                (date, time) = local2utc(date, time, timezone);
        }

        /* use Unix timestamp */
        timestamp = dt2unix(date, time);

        if (isstring)
        {
                timestamp = timestamp[1];
        }
        
        return(timestamp);
}