View Raw SPL
/****************************************************************************
*                                                                           *
*   YMDHMS2DT.SPL Copyright 2024 (C) DSP Development Corporation            *
*                                                                           *
*   Author:       Randy Race                                                *
*                                                                           *
*   Synopsis:     Converts YMDHMS to date time                              *
*                                                                           *
*   Revisions:    10 Nov 2024     RRR     Creation                          *
*                                                                           *
****************************************************************************/


#if @HELP_YMDHMS2DT

    YMDHMS2DT

    Purpose: Converts Years, Months, Days, Hours, Minutes, Seconds
             to date time values.

    Syntax:  YMDHMS2DT(years, months, days, hours, minutes, seconds)

                years - A scalar or series, the year values.

               months - A scalar or series, the month values.

                 days - A scalar or series, the day values.

                hours - A scalar or series, the hours values.

              minutes - A scalar or series, the minutes values.

              seconds - A scalar or series, the seconds values.


    Returns: A series of date time values.

    Example:
             W1: ymdhms2dt()

             W1 contains the current date time.

    Example:
             W2: ymdhms2dt(2030, 10, 31, 12, 0, 0)

             W2 contains the date time series:

               10-31-2030 12:00:00

    Example:
             W3: ymdhms2dt(2030, 10, 32, 12, 0, 70)

             W3 contains the date time series:

               11-01-2030 12:01:10

             The day value wraps around and the seconds value wraps
             around.

    Example:
             W4: ymdhms2dt(2030..2034, 4, 1..5, 12, 10..14, 0)

             W4 contains the date time values:

              4-01-2030 12:10:00
              4-02-2031 12:11:00
              4-03-2032 12:12:00
              4-04-2033 12:13:00
              4-05-2034 12:14:00

    Remarks:
             YMDHMS2DT converts years, months, days, hours, minutes and
             seconds to a date time series.

             See DT2YMDHMS to convert a date time series to series of years,
             months, days, hours, minutes, seconds.

    See Also:
             DATE2DT
             DT2DATE
             DT2YMDHMS
             YMD2DATE
             GETDT
             UNIX2DT
#endif


/* year, month, day, hour, minute, seconds to dt */
ymdhms2dt(years = {0}, months = {1}, days = {1}, hours = {0}, minutes = {0}, seconds = {0})
{
        local date = {}, time = {}, frac, dt;

        /* no input or string input */
        if (argc <= 2)
        {
                /* check for ymdhms2dt(), or ymdhms2dt("1/1/2024", "12:00:00") */
                if (argc < 2)
                {
                        /* default to current local date time */
                        if (argc < 1)
                        {
                                years = getdate();

                                /* time in this context */
                                months = gettime(13);
                        }
                }

                if (isdatestr(years))
                {
                        if (istimestr(months))
                        {
                                date = ymd2date(years);
                                time = hms2time(months);
                        }
                        else if (isstring(months))
                        {
                                error(sprinf("%s - unrecognized time '%s'", __FUNC__, months));
                        }
                }
                else if (isstring(years))
                {
                        error(sprinf("%s - unrecognized date '%s'", __FUNC__, years));
                }
        }

        if (isempty(date) || isempty(time))
        {
                (years, months, days, hours, minutes, seconds) = 
                        ymdhms2dt_parse_args(argc, years, months, days, hours, minutes, seconds);

                /* get julians */
                date = julfromymd(years, months, days);

                /* time vals */
                time = hours * 3600 + minutes * 60 + seconds;
        }

        /* convert to/from timestamps to handle rollovers */
        ts = dt2timestamp(date, time);

        if (outargc > 1)
        {
                (date, time) = timestamp2dt(ts);

                return(date, time);
        }
        else
        {
                dt = timestamp2dt(ts);

                settable(dt);

                return(dt);
        }
}


ymdhms2dt_parse_args(numargs, years, months, days, hours, minutes, seconds)
{
        if (numargs == 1)
        {
                if (not(isarray(years)))
                {
                        error(sprintf("%s - multi-column input series expected", __CALLER__));
                }

                nc = numcols(years);

                if (nc > 1)
                {
                        if (nc > 2)
                        {
                                if (nc > 3)
                                {
                                        if (nc > 4)
                                        {
                                                if (nc > 5)
                                                {
                                                        seconds = col(years, 6);
                                                }

                                                minutes = col(years, 5);
                                        }

                                        hours = col(years, 4);
                                }

                                days = col(years, 3);
                        }

                        months = col(years, 2);
                }

                years = col(years, 1);
        }

        /* force series */
        years   = {years};
        months  = {months};
        days    = {days};
        hours   = {hours};
        minutes = {minutes};
        seconds = {seconds};

        return(years, months, days, hours, minutes, seconds);
}