As explained in the Datetime FIelds page, a field of
datetimetype is a 64 bit integer value used to store the date and time in databases and the precision ofdatetimevalues is determined by the runtime parameterDATETIME_PRECISIONwhich represents the number of ticks per second. The default value forDATETIME_PRECISIONis 1, which means that the database stores adatetimevalue as the number of seconds since the EPOCH (i.e. 00:00:00 UTC 1 January 1970).For example , the following schema snippet defines date/time fields and sequences of date/time values:
class T { date d; time t; datetime dt; sequence<date> sd; sequence<time> st; sequence<datetime> sdt; }The resolution of
datetimefields can be changed by an application via themco_runtime_setoption()API for option. The allowable precision (resolution) values range fromMCO_RT_OPTION_DATETIME_PRECISION1(for the number of seconds) to1000000000(for nanoseconds). For example the following code snippet sets the precision to1000000(1,000,000) to store the number of microseconds since the EPOCH:mco_runtime_setoption(MCO_RT_OPTION_DATETIME_PRECISION, 1000000);The two functions
mco_time2ticks()andmco_ticks2time()can be used to convert datetime between the application's and database's resolution. For example, consider an application storing time values in milliseconds (i.e. : MCO_RT_OPTION_DATETIME_PRECISION == 1000) in variableapp_time. The following would store the number of milliseconds since the EPOCH in the fieldT.dt:T_dt_put(&obj, mco_ticks2time(app_time, 1000));Similarly, the following sample stores the current time, returned by
mco_system_get_current_time():T_dt_put(&obj, mco_ticks2time(mco_system_get_current_time(), 1000*1000))Here the second argument in the call to
mco_ticks2time()is1000*1000becausemco_system_get_current_time()returns time in microseconds.Similarly, the
mco_time2ticks()is used to convert adatetimevalue loaded from the database to the desired resolution. For example:mco_datetime dt; T_dt_get(&obj, &dt); printf("dt (in msecs) = %llu, dt (in usecs) = %llu\n", mco_time2ticks(dt, 1000), mco_time2ticks(dt, 1000*1000));Mapping SQL Date and Time fields
In order to map C-language date or time data to the SQL data types, a C application should use an 8-byte
mco_datetimedata type (instead of themco_timeormco_date). The date and time types are represented via the 8-bytetpDatetimetype in SQL. (Use the %l or %*l substitution specifier as this is an 8-byte orint64_tvalue.)