Clk_DateTimeToTS_Unix

Convert a date/time structure to Unix timestamp.

Files

clk.h/clk.c

Prototype

CPU_BOOLEAN  Clk_DateTimeToTS_Unix (CLK_TS_SEC     *p_ts_unix_sec,
                                    CLK_DATE_TIME  *p_date_time);

Arguments

p_ts_unix_sec

Pointer to variable that will receive the Unix timestamp:

In seconds UTC+00, if no errors;
CLK_TS_SEC_NONE, otherwise.

p_date_time

Date/time structure to convert.

Returned Values

DEF_OK, if date/time structure successfully converted.

DEF_FAIL, otherwise.

Required Configuration

Available only if CLK_CFG_UNIX_EN is DEF_ENABLED in clk_cfg.h (see Module Configuration).

Notes / Warnings

Date/time structure (p_date_time) must be representable in Unix timestamp. Thus date to convert must be greater than or equal to CLK_UNIX_EPOCH_YR_START and less than CLK_UNIX_EPOCH_YR_END. Date/time should be set to local time with correct time zone offset (p_date_time->TZ_sec). Clk_DateTimeToTS_Unix() removes the time zone offset from the date/time to calculate and return an Unix timestamp at UTC+00.

Example Usage

Listing - Clk_DateTimeToTS_Unix() Example Usage
CLK_TS_SEC     ts_unix_sec;
CLK_DATE_TIME  date_time;
CPU_BOOLEAN    valid;
 
                                  /* 2010/09/18   11:11:11 UTC-05:00  */
valid = Clk_DateTimeMake(&date_time, 2010, 9, 18, 11, 11, 11, -18000);
if (valid == DEF_OK) {
    printf("Date/time successfully created");
} else {
    printf("Clock Date/time error\n\r");
}
 
valid = Clk_DateTimeToTS_Unix(&ts_unix_sec, &date_time);
if (valid == DEF_OK) {
    printf("Timestamp = %u", ts_unix_sec);
} else {
    printf("Clock Date/time to NTP timestamp error\n\r");
}