Example Code
The following example code illustrates the capabilities and usage of the µC/Clk module. This code simply initializes µC/Clk, create date/time structure, set Clock timestamp and time zone, get Clock timestamp and time zone. Also the example shows timestamp and date/time conversions.
static void AppTaskStart (void *p_arg) { CLK_TS_SEC ts_sec; CLK_TS_SEC ts_unix_sec; CLK_TZ_SEC tz_sec; CLK_DATE_TIME date_time; CPU_BOOLEAN valid; CPU_CHAR str[128]; CLK_ERR err; Clk_Init(&ClkTaskCfg, DEF_NULL, &err); (1) if (err == CLK_ERR_NONE) { printf("Clock module successfully initialized\r\n"); } else { printf("Clock module initialization failed\r\n"); return; } tz_sec = 0; valid = Clk_DateTimeMake(&date_time, 2010, 10, 18, 11, 11, 11, tz_sec); (2) if (valid != DEF_OK) { printf("Clock make date/time failed\r\n"); return; } date_time.Yr = 2010; date_time.Month = 10; date_time.Day = 18; date_time.DayOfWk = Clk_GetDayOfWk(2010, 10, 18); (3) date_time.DayOfYr = 291; date_time.Hr = 11; date_time.Min = 11; date_time.Sec = 11; date_time.TZ_sec = tz_sec; valid = Clk_IsDateTimeValid(&date_time); (4) if (valid != DEF_OK) { printf("Clock date/time not valid\r\n"); return; } valid = Clk_SetDateTime(&date_time); (5) if (valid != DEF_OK) { printf("Clock set date/time failed\r\n"); return; } valid = Clk_DateTimeToStr(&date_time, CLK_STR_FMT_YYYY_MM_DD_HH_MM_SS_UTC, str, 128); (6) if (valid == DEF_OK) { printf("Current Date/time :%s\r\n", str); } else { printf("Clock date/time to string failed\r\n"); return; } Clk_DateTimeToTS(&ts_sec, &date_time); (7) if (valid == DEF_OK) { printf("Clock timestamp = %u\r\n", ts_sec); } else { printf("Clock date/time to timestamp failed\r\n"); return; } tz_sec = (-5 * 60 * 60); valid = Clk_SetTZ(tz_sec); (8) if (valid != DEF_OK) { printf("Clock set timezone unix failed\r\n"); return; } valid = Clk_GetDateTime(&date_time); (9) if (valid != DEF_OK) { printf("Clock get date/time failed\r\n"); return; } valid = Clk_DateTimeToStr(&date_time, CLK_STR_FMT_YYYY_MM_DD_HH_MM_SS_UTC, str, 128); (10) if (valid == DEF_OK) { printf("Current Date/time :%s\r\n", str); } else { printf("Clock date/time to string failed\r\n"); return; } ts_sec = Clk_GetTS(); (11) printf("Clock timestamp = %u\r\n", ts_sec); valid = Clk_TS_ToDateTime(ts_sec, 0, &date_time); (12) if (valid != DEF_OK) { printf("Clock convert timestamp to date/time failed\r\n"); return; } valid = Clk_GetTS_Unix(&ts_unix_sec); (13) if (valid != DEF_OK) { printf("Clock get timestamp unix failed\r\n"); return; } valid = Clk_TS_UnixToDateTime(ts_unix_sec, tz_sec, &date_time); (14) if (valid != DEF_OK) { printf("Clock timestamp unix to date/time failed\r\n"); return; } valid = Clk_DateTimeToStr(&date_time, CLK_STR_FMT_YYYY_MM_DD_HH_MM_SS_UTC, str, 128); (15) if (valid == DEF_OK) { printf("Current Date/time :%s\r\n", str); } else { printf("Clock date/time to string failed\r\n"); return; } ts_unix_sec = 126316799uL; valid = Clk_TS_UnixToDateTime(ts_unix_sec, tz_sec, &date_time); (16) if (valid != DEF_OK) { printf("Clock set date/time failed\r\n"); return; } valid = Clk_DateTimeToStr(&date_time, CLK_STR_FMT_DAY_MONTH_DD_HH_MM_SS_YYYY, str, 128); (17) if (valid == DEF_OK) { printf("Unix timestamp = %s", str); } else { printf("Clock date/time to string failed\r\n"); return; } }
(1) Initialize the µC/Clk. If the process is successful, the µC/Clk task is started, and its various data are initialized.
(2) Build a date/time structure.
(3) Get the day of week of 2010, october 18.
(4) Validate date/time structure fields.
(5) Set current timestamp of Clock module with date/time structure.
(6) Get a foramted string via a date/time structure.
(7) Convert date/time structure to a timestamp.
(8) Set Clock time zone.
(9) Get current Click date/time into a date/time structures.
(10) Get a foramted string via a date/time structure.
(11) Get current timestamp of Clock module.
(12) Convert timestamp to a date/time structure.
(13) Get current Clock timestamp as a Unix timestamp.
(14) Convert a Unix timestamp to a date/time structures.
(15) Get a foramted string via a date/time structure.
(16) Unix timestamp is equivalent of 1974, January 1 23:59:59 UTC+0
(17) Convert Unix timestamp to a date/time structure.
(18) Get a formatted string via a date/time structure.