HADD: Incrementing a Date-Time Value

How to:

The HADD function increments a date-time value by a given number of units.

Syntax: How to Increment a Date-Time Value

HADD(datetime, 'component', increment, length, output)

where:

datetime

Date-time

Is the date-time value to be incremented.

component

Alphanumeric

Is the name of the component to be incremented enclosed in single quotation marks. For a list of valid components, see Arguments for Use With Date and Time Functions.

Note: WEEKDAY is not a valid component for HADD.

increment

Integer

Is the number of units (positive or negative) by which to increment the component.

length

Integer

Is the number of characters returned. Valid values are:

  • 8 indicates a date-time value that includes one to three decimal digits (milliseconds).
  • 10 indicates a date-time value that includes four to six decimal digits (microseconds).
  • 12 indicates a date-time value that includes seven to nine decimal digits (nanoseconds).
output

Date-time

Example: Incrementing a Date-Time Value

The following example increments thirty months to some specific date-time in the past

HADD(DT(2001/09/11 08:54:34), 'MONTH', 30, 8, 'HYYMDS')

and returns the timestamp 2004/03/11 08:54:34.00.

Example: Converting Unix (Epoch) Time to a Date-Time Value

Unix time (also known as Epoch time) defines an instant in time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.

The following DEFINE FUNCTION takes a number representing epoch time and converts it to a date-time value by using the HADD function to add the number of seconds represented by the input value in epoch time to the epoch base date:

DEFINE FUNCTION UNIX2GMT(INPUT/I9)
   UNIX2GMT/HYYMDS = HADD(DT(1970 JAN 1),'SECONDS',INPUT,8,'HYYMDS');
END

The following request uses this DEFINE FUNCTION to convert the epoch time 1449068652 to a date-time value:

DEFINE FILE GGSALES
INPUT/I9=1449068652;
OUTDATE/HMTDYYSb = UNIX2GMT(INPUT);
END
TABLE FILE GGSALES
PRINT DATE NOPRINT INPUT OUTDATE
WHERE RECORDLIMIT EQ 1
ON TABLE SET PAGE NOLEAD
END

The output is shown in the following image:

WebFOCUS

Feedback