View Raw SPL
/****************************************************************************
*                                                                           *
*   IDXTODT.SPL  Copyright 2026 (C) DSP Development Corporation             *
*                              All Rights Reserved                          *
*                                                                           *
*   Author:      Randy Race                                                 *
*                                                                           *
*   Synopsis:    Converts indices to date/time values                       *
*                                                                           *
*   Revisions:   22 Jan 2026     RRR     Creation                           *
*                                                                           *
****************************************************************************/


#if @HELP_IDXTODT

    IDXTODT

    Purpose: Converts indices to date and times values.

    Syntax:  IDXTODT(series, idx)

                series - A series, the input series.

                   idx - A series, the integer index values.
  
    Returns: A series of date time values.

    Example:
             W1: gnorm(10, 1);setdate(w0, "1-1-2030");settime(w0, "12:00:00")
             W2: getdt(w1)
             W3: idxtodt(w1, 1..10)

             W2 returns the date/time values of W1.

             W3 returns the same values using the indices 1..10.

    Example:
             W1: gnorm(10, 1);setdate(w0, "1-1-2030");settime(w0, "12:00:00")
             W2: idxtodt(w1, {1, 5, 10})
             W3: dttoidx(w1, w2)

             W2 returns the date/time values of W1 using the indices
             {1, 5, 10}.

             W3 returns the indices {1, 5, 10} from the date/time values
             in W1.
 
    Remarks:
             IDXTODT converts the indices of an input series to the
             equivalent date/time values based on the start date and start
             time of the series.

             See DTTOIDX to convert date/time values of a series to indices.
             
    See Also:
             DT2IDX
             DT2UNIX
             DT2YMDHMS
             GETDT
             IDXTOX
             XTOIDX
#endif


/* convert indices to date/time values */
ITERATE idxtodt(series, idx, limit = 1)
{
        local x, dt, date, time;

        if (not(isarray(series)))
        {
                error(sprintf("%s - input series required", __FUNC__));
        }

        if (argc < 2)
        {
                error(sprintf("%s - index values required", __FUNC__));
        }

        x  = idxtox(series, idx);
        dt = xtodt(series, x);

        if (outargc > 1)
        {
                date = col(dt, 1);
                time = col(dt, 2);

                return(date, time);
        }
        else
        {
                return(dt);
        }
}


/* convert indices to date/time values */
idxtodt2(series, idx)
{
        local is_scalar, dt, date, time;

        if (not(isarray(series)))
        {
                error(sprintf("%s - input series required", __FUNC__));
        }

        if (argc < 2)
        {
                error(sprintf("%s - index values required", __FUNC__));
        }

        is_scalar = isscalar(idx);

        dt = getdt(series);
        dt = dt[idx, ..];

        if (outargc > 1)
        {
                date = col(dt, 1);
                time = col(dt, 2);

                return(date, time);
        }
        else
        {
                return(dt);
        }
}