View Raw SPL
/*****************************************************************************
*                                                                            *
*   DATE2DT.SPL  Copyright (C) 2024 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts a fractional Julian date to date time              *
*                                                                            *
*   Revisions:    2 Dec 2024  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_DATE2DT

    DATE2DT

    Purpose: Converts fractional Julian date series to date time.

    Syntax:  DATE2DT(juldate)

             (date, time) = DATE2DT(juldate)

              juldate - A scalar or series, the fractional Julian date values.


    Returns: A series or array of date time values.

             (date, time) = DATE2DT(juldate) returns the date and time
             components in separate variables.

    Example:
             W1: date2dt(2462806.0)

             W1 contains the date time:

               10-31-2030 12:00:00

            
            The time is 12:00:00 because fractional Julian dates begin at
            12:00 noon UTC time.

    Example:
             W1: {2462806.75}
             W2: date2dt(w1)
             W3: (d, t) = date2dt(w1);ravel(d, t)
             W4: {w1, d, floor(w1 + 0.5)};setvunits("Days")

             The fractional Julian date value in W1 is 2462806.75.

             W2 indicates the fractional Julian date represents the date time
             11-01-2030 06:00:00.

             The variable D in W3 contains the date time integer Julian
             value 2462807, the Julian day starting at midnight. The variable
             T contains 21600 or 6:00:00, the date time seconds since
             midnight. The two variables D and T represent the date time
             11-01-2030 06:00:00.

             W4 displays the original fractional Julian date, the integer
             Julian date and the integer Julian date derived from the
             fractional Julian date. The integer Julian date starting at
             midnight is obtained from the fractional Julian date starting
             at noon with:

             i_jul = FLOOR(f_jul + 0.5)

             The fractional Julian date starting at noon and integer Julian
             date starting at midnight plus elapsed seconds since midnight
             represent the same calendar date.

    Example:
             W1: {2462805.25,
                  2462805.50, 
                  2462805.75,
                  2462806.00,
                  2462806.25,
                  2462806.50,
                  2462806.75, 
                  2462807.00}

             W2: date2dt(w1)

             W1 contains a series of fractional Julian date values.

             W2 converts the fractional Julian date values to a column of
             integer Julian date values and a column of seconds starting at
             midnight.

             W2 contains the date times:

               10-30-2030 18:00:00
               10-31-2030 00:00:00
               10-31-2030 06:00:00
               10-31-2030 12:00:00
               10-31-2030 18:00:00
               11-01-2030 00:00:00
               11-01-2030 06:00:00
               11-01-2030 12:00:00

    Example:
             W1: {2462805.25,
                  2462805.50, 
                  2462805.75,
                  2462806.00,
                  2462806.25,
                  2462806.50,
                  2462806.75, 
                  2462807.00}

             W2: date2dt(w1)

             W3: dt2date(w2)

             W4: w1 == w3

             Same as above except W3 converts the date time values in W2
             back to fractional Julian days.

             W4 is all ones indicating the transformation from and to
             fractional Julian days is successful and invertible.

    Remarks:
             DATE2DT converts fractional or astronomical Julian date values
             into date time values.

             A fractional Julian day is the number of days since
             January 1, 4713 BC at noon UTC time. For example, a fraction
             of 0.25 represents time 18:00:00 or 6 hours (one quarter of a
             day) after noon.

             A date time series consists of 2 columns of values where the
             first column is an integer Julian date starting at midnight
             and the second column is time in seconds starting from
             midnight.

             See DT2DATE to convert date time values to fractional Julian
             date values.
             
    See Also:
             Date2ymd
             Dt2date
             Julstr
             Strjul
             Ymdhms2dt
#endif



/* fractional julian date to date time */
date2dt(date = getdate())
{
        local dt, time;

        if (isstring(date))
        {
                date = julstr(date);
        }

        /* handle fractional julians - time starting at noon */
        time = (date - int(date)) * 86400 + 43200;

        /* get date/time */
        dt = ymdhms2dt(dt2ymdhms(int(date), time));

        if (outargc > 1)
        {
                return(col(dt, 1), col(dt, 2));
        }
        else
        {
                return(dt);
        }
}