View Raw SPL
/****************************************************************************
*                                                                           *
*   TIMESTAMP2DT.SPL Copyright 2024 (C) DSP Development Corporation         *
*                                                                           *
*   Author:     Randy Race                                                  *
*                                                                           *
*   Synopsis:   Converts interbal time stamps to Julian date and seconds    *
*                                                                           *
*   Revisions:   2 Nov 2024     RRR     Creation                            *
*                                                                           *
****************************************************************************/


#if @HELP_TIMESTAMP2DT

    TIMESTAMP2DT

    Purpose: Converts internal timestamps to date time series.

    Syntax:  TIMESTAMP2DT(ts, "timezone")

             (d, t) = TIMESTAMP2DT(ts, offset, startdate)

                       ts - A series of internal time stamps.
  
               "timezone" - Optional. A string, the local time zone. Defaults
                            to the current time zone. If specified, the
                            resulting date time values are based on the UTC time
                            offset from the specified timezone.

    Returns: A two column series of dates and clock time.

             (d, t) = timestamp2dt(ts) returns the dates and clock
             time in two separate variables.

    Example:
             W1: {1407159000, 1408159082, 1411150163, 1417159180, 1427159240}
             W2: timestamp2dt(W1)

             W2 contains the date and time values:

              8-04-2014  13:30:00
              8-16-2014   3:18:02
              9-19-2014  18:09:23
             11-28-2014   7:19:40
              3-24-2015   1:07:20

    Example:
             W3: {1407159000, 1408159082, 1411150163, 1417159180, 1427159240}
             W4: (d, t) = timestamp2dt(W3); ravel(d, t)

             W4 contains the date and time values:

              8-04-2014  13:30:00
              8-16-2014   3:18:02
              9-19-2014  18:09:23
             11-28-2014   7:19:40
              3-24-2015   1:07:20

    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

    Remarks:
             Internal timestamps are in seconds elapsed since midnight
             January 1, 1970 UTC time. TIMESTAMP2DT converts the
             internal second values into date and clock time for the 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.

             If TIMEZONE is specified when using DT2TIMESTAMP and TIMESTAMP2DT,
             the input and output values are converted to and from UTC time.
             When the timestamps are used to perform date time arithmetic,
             the results properly account for Daylight Savings time changes,
             however at decreased performance for large inputs.              

             See DT2TIMESTAMP to convert date and clock time to internal
             timestamps.

    See Also:
             DT2TIMESTAMP
             DT2UNIX
             DTXY
             JULSTR
             STRJUL
             STRTOD
             TODSTR
#endif


/* convert internal timestamps to date/time */
ITERATE timestamp2dt(ut = 0, timezone = "")
{
        local date, time, isscalar, dt;

        if (argc < 1)
        {
                error(sprintf("%s - input timestamps required", __FUNC__));
        }

        isscalar = isscalar(ut);

        /* returns UTC */
        (date, time) = unix2dt({ut});

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

        if (isscalar)
        {
                /* return as string */
                dt = strjul(date[1]) + " " + strtodmsec(time[1]);

                return(dt);
        }
        else if (outargc > 1)
        {
                return(date, time);
        }
        else
        {
                /* return as table */
                dt = ravel(date, time);
                setplotstyle(dt, 4);

                return(dt);
        }
}