View Raw SPL
/****************************************************************************
*                                                                           *
*   DECTIME.SPL Copyright 2007 (C) DSP Development Corporation              *
*                                                                           *
*   Author:     Randy Race                                                  *
*                                                                           *
*   Synopsis:   Converts decimal time (1715.10) to time (17:15:10)          *
*                                                                           *
*   Revisions:   2 Nov 2007     RRR     Creation                            *
*                8 Nov 2007     RRR     optional wrap parameter             *
*                                                                           *
****************************************************************************/

#include 

#if @HELP_DECTIME

    DECTIME

    Purpose: Converts decimal time HHMM.SS to time data

    Syntax:  DECTIME(t, wrap, date)

                 t - A series of decimal time values where
                     1715.10 represents the time 17:15:10

              wrap - Optional integer. Day wrap option:

                      0: do not wrap days

                      1: wrap sequential time changes from 23:59:59
                         to 0:0:0 to the next day (default).

              date - Optional string, the starting date. Defaults
                     to the date of t.

    Returns: A series of time values.

    Example:
             W1: {1812.56, 2030.10, 110.00, 1310.00}
             W2: dectime(w1)

             W2 contains the time values:

             18:12:56, 20:30:10, 1:10:00, 13:10:00

    Remarks:
             The decimal time value are in the format HHMM.SS where
             HH ranges from 0 to 23, MM ranges from 0 to 59 and
             SS ranges from 0 to 59. Thus, the minimum time value is
             0.0 (midnight) and the maximum is 2359.59 (one minute
             before midnight).

             If wrap is 1 (the default),the time values are considered
             sequential such that a change in time from 2359.59 to 0.0
             indicates a change in day.

    See Also:
             DTXY
             JULSTR
             STRJUL
             STRTOD
             TODSTR
             XYDT
#endif


/* convert decimal time to our time */
dectime(t, wrap, date)
{
        local h, m, s, time, change;

        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1) error("dectime - input series required");
                        
                        wrap = 1;
                        date = getdate(t);
                }
                
                if (isstring(wrap))
                {
                        date = wrap;
                        wrap = 0;
                }
                else
                {
                        date = getdate(t);
                }
        }

        /* get hours */
        h = int(t / 100);

        /* minutes */
        m = int(rem(t / 100, 1) * 100 + 0.001);

        /* seconds */
        s = int(rem(t, 1) * 100 + 0.001);

        /* combine */
        time = h * 60 * 60 + m * 60 + s;

        if (wrap)
        {
                /* find discontinuities when time changes from 24:00 to 0:00 */
                change = extract(time - delay(time, 1), 1, length(time));

                /* first point doesn't change */
                change[1] = 0;

                /* changes occur where value is < 0 */
                change = change < 0;

                /* accumulate and scale by seconds in a day */
                change = partsum(change) * 86400;

                /* add changes to time data */
                time += change;
        }

        /* set units */
        setvunits(time, "time");
        settime(time, strtod(time[1]));
        setdate(time, date);

        return(time);
}