Date and Timestamp
Format string
Default formatting string:
timestamp: "%Y-%m-%d %H:%M:%S" = "%F %T" date : "%Y-%m-%d" = "%F"
All possible format strings:
%%
a literal ‘%’
%a
locale’s abbreviated weekday name (e.g. ‘Sun’)
%A
locale’s full weekday name (e.g. ‘Sunday’)
%b
locale’s abbreviated month name (e.g. ‘Jan’)
%B
locale’s full month name (e.g. ‘January’)
%c
locale’s date and time (e.g. ‘Thu Mar 3 23:05:25 2005’)
%C
century; like ‘%Y’, except omit last two digits (e.g. ‘20’)
%d
day of month (e.g. ‘01’)
%D
date; same as ‘%m/%d/%y’
%e
day of month, space padded; same as ‘%_d’
%F
full date; same as ‘%Y-%m-%d’
%g
last two digits of year of ISO week number (see ‘%G’)
%G
year of ISO week number (see ‘%V’); normally useful only with ‘%V’
%h
same as ‘%b’
%H
hour (‘00’..‘23’)
%I
hour (‘01’..‘12’)
%j
day of year (‘001’..‘366’)
%k
hour, space padded (‘ 0’..‘23’); same as ‘%_H’
%l
hour, space padded (‘ 1’..‘12’); same as ‘%_I’
%m
month (‘01’..‘12’)
%M
minute (‘00’..‘59’)
%n
a newline
%p
locale’s equivalent of either ‘AM’ or ‘PM’; blank if not known
%P
like ‘%p’, but lower case
%r
locale’s 12-hour clock time (e.g. ‘11:11:04 PM’)
%R
24-hour hour and minute; same as ‘%H:%M’
%s
seconds since ‘1970-01-01 00:00:00 UTC’
%S
second (‘00’..‘60’)
%t
a tab
%T
time; same as ‘%H:%M:%S’
%u
day of week (‘1’..‘7’); ‘1’ is Monday
%U
week number of year, with Sunday as first day of week (‘00’..‘53’)
%V
ISO week number, with Monday as first day of week (‘01’..‘53’)
%w
day of week (‘0’..‘6’); ‘0’ is Sunday
%W
week number of year, with Monday as first day of week (‘00’..‘53’)
%x
locale’s date representation (e.g. ‘12/31/99’)
%X
locale’s time representation (e.g. ‘23:13:48’)
%y
last two digits of year (‘00’..‘99’)
%Y
year
By default, date pads numeric fields with zeroes. The following optional flags may follow ‘%’.
-
(hyphen) do not pad the field
_
(underscore) pad with spaces
0
(zero) pad with zeros
^
use upper case if possible
#
use opposite case if possible
EVD definition
Example of definition date and timestamp in evd file. The following dates data types definition are equivalent.
valid_from date valid_from date("%F") valid_from date("%Y-%m-%d")
Following timestamps are all the same.
request_dt timestamp request_dt timestamp("%F %T") request_dt timestamp("%Y-%m-%d %H:%M:%S")
Declaration in mapping
The following declarations are equivalent.
static timestamp min_date(1970,1,1,0,0,0); static timestamp min_date("1970-01-01 00:00:00"); static timestamp min_date("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S"); static timestamp min_date = timestamp::from_epoch_time(0);
static date min_date(1970,1,1); static date min_date("1970-01-01"); static date min_date("1970-01-01", "%Y-%m-%d");
Manipulation, comparison
Lets have ‘timestamp ts(2017,5,31,19,37,0)’ and ‘date dt(2018,1,14)’ in the following examples.
Methods switching date and timestamp data type:
ts.to_date()
returns ‘2017-05-31’, i.e. cut off time and return date data type
dt.to_timestamp()
returns ‘2018-01-14 00:00:00’, i.e. add ‘00:00:00’ and return timestamp data type
Following methods return appropriate values as ‘int’.
ts.year() -- 2017 dt.year() -- 2018 ts.month() -- 5 dt.month() -- 1 ts.day() -- 31 dt.day() -- 14 ts.hour() -- 19 ts.minute() -- 37 ts.second() -- 0 ts.epoch_time() -- 1496169420 ts.yearday() -- 151 dt.yearday() -- 14 ts.weekday() -- 3 (Wednesday) dt.weekday() -- 0 (Sunday)
In the context of string, method ‘weekday()’ returns ‘sunday’, ...
‘weekday()’ returns ‘0’ for Sunday, ‘1’ for Monday, …, ‘6’ for Saturday.
These methods convert date and timestamp to string:
string str1 = ts.to_string(); // 2017-05-31 19:37:00, // i.e. uses default format string string str2 = ts.to_string("%Y%m%d%H"); // 2017053119 string str3 = min_date.to_string("%Y%m%d"); // 19700101
Comparison: ‘==’, ‘!=’, ‘<’, ‘>’, ‘<=’, ‘>=’.
if (ts >= timestamp(1990,1,1) { ... }
Addition, subtraction:
ts += 65; // add 65 seconds, i.e. 2017-05-31 19:38:05 ts--; // 2017-05-31 19:38:04 date d(2017,5,31); d -= 35; // subtract 35 days, i.e. 2017-04-26 ts.add_year(1); // 2018-05-31 19:38:04 d.add_month(-1); // 2017-03-26 ts.add_day(6); // 2018-06-06 19:38:04 ts.add_hour(-2); // 2018-05-31 17:38:04 ts.add_minute(3); // 2018-05-31 19:41:04 ts.add_second(-6); // 2018-05-31 19:37:58
The difference between ‘ts.add_second(10)’ and ‘ts+10’ is that in the first case we modify the object itself, but in the second case new value is returned. One can use then for example ‘ts.add_hour(2).add_minute(3)’.
Difference:
auto diff = ts - timestamp(2018,5,31,19,36,57); // 61 (seconds) auto diff = d - date("2017-04-02"); // -6 (days)
Let’s summarize the logic:
date - int => date timestamp - int => timestamp date - date => int timestamp - timestamp => int