Some notes regarding timestamps.
Defining a Timestamp
A timestamp is stored as a 26 character field. But, the system looks at it as a special data type and enforces the rules it has set up for timestamps.
The format for a timestamp is is YYYY-MM-DD-HH.MM.SS.XXXXXX (Year-Month-Day-Hour.Minute.Second.Milliseconds).
An example would be ‘2011-09-14-22.06.48.592000’ (Year: 2011 / Month: 09 / Day: 14 / Hour: 22 / Minute: 06 / Second: 48 / Millisecond: 592000) .
The default value for a “*blank” time stamp is ‘0001-01-01-00.00.00.000000’.
In DDS a timestamp is defined with just the field name and a ‘Z’ in the data type column.
In RPG a timestamp is defined in the ‘D’ specs as the field name plus a ‘Z’ in the Internal Data Type column.
In SQL a timestamp is defined as ‘field-name TIMESTAMP NOT NULL WITH DEFAULT’.
Working with Time Stamps
As stated, the system will choke on an incorrect timestamp when it encounters one. So, a timestamp must always be handled as a timestamp and not a character field (no *blank, no *zeros, no character values other than those listed above).
DDS will automatically load ‘0001-01-01-00.00.00.000000’ into a timestamp when a record is written and the timestamp is *blank.
To initialize and load the current timestamp value in an RPG program: TimeStamp = %timestamp()
or you can use the TIME opcode in older RPG programs.
The preceding operations will load the timestamp value, but the last 3 digits of the millisecond segment will be ‘000. To get the complete millisecond value use: exec SQL set :TimeStamp = current_timestamp;
Of course, this means the program has to be SQLRPG or SQLRPGLE. If that’s an issue, you can always create a utility program or service program that executes that SQL, and call it from other programs to get the timestamp. That way you don’t have to change the program type on countless programs. Personally, I create most of my programs as SQLRPGLE to begin with, so it’s not an issue for me.
To extract the date (as date data type) from a timestamp use: Date = %date(TimeStamp)
Remember, although the timestamp “looks” like a character field, it is really not. So, to get it into a character field, you can either to a MOVE operation or these built-in functions: Char = %char(TimeStamp). (Move operations work with any data type).
- How to view timestamps for individual texts and iMessages in iOS 7(imore.com)
- Android Java – Tips (Convert time stamp to preferred date format)(ronscript.wordpress.com)