How to Convert a timestamp to and from a string

 View Only

How to Convert a timestamp to and from a string 

Mon August 24, 2020 11:32 PM


Many processing environment will include a timestamp in their tuple.  If your data uses the SPL timestamp type, this article will show you how to convert it to a string. 

Note: Since this article was written, the datetime toolkit has been released with additional utilities for working with dates and times.

Assuming a format of “YYYY-MM-DD HH:MM:SS”, the conversion from character to binary is simple with one of the versions of the toTimestamp function:

myTimestamp = toTimestamp(Sys.YYYYMMDDhhmmss, ts_string);


Converting from a timestamp to a string is slightly more difficult if you are looking for a specific format. The ctime() function would return a string such as “Wed Aug 28 21:49:08 2013”.

This may not be what you want. If you want to convert to, let’s say, “YYYY-MM-DD HH:MM:SS”, you first have to extract the values from the timestamp and then create the string. This is easily done as follows:

mutable Sys.tm tmres = { } ;
time(myTimestamp, tmres);
ts_string = strftime(tmres, "%Y-%m-%d %T");

The first line initialize the structure tmres. This is required if you are using it in a function as opposed to directly in the operator logic. The second line extracts the values from myTimestamp and sets tmres. The third line is similar in functionality to the the standard C++ strftime. The string argument follows the same format.

Resources



Written by Howard Naasgard.
#CloudPakforDataGroup

Statistics

0 Favorited
9 Views
0 Files
0 Shares
0 Downloads