-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
76 lines (63 loc) · 2.95 KB
/
main.c
File metadata and controls
76 lines (63 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include "utc_timestamp.h"
/* Test function */
void test_timestamp_functions() {
/* Test case 1: Base time (2000-01-01 00:00:00) */
utc_time_t time1 = {2000, 1, 1, 0, 0, 0};
unsigned int ts1 = utc2timestamp(&time1);
utc_time_t time1_back = timestamp2utc(ts1);
/* Test case 2: One day later (2000-01-02 00:00:00) */
utc_time_t time2 = {2000, 1, 2, 0, 0, 0};
unsigned int ts2 = utc2timestamp(&time2);
utc_time_t time2_back = timestamp2utc(ts2);
/* Test case 3: Including leap year (2004-02-29 12:30:45) */
utc_time_t time3 = {2004, 2, 29, 12, 30, 45};
unsigned int ts3 = utc2timestamp(&time3);
utc_time_t time3_back = timestamp2utc(ts3);
/* Test case 4: Maximum timestamp test (close to 32-bit limit) */
unsigned int max_ts = 4294967295u; /* 2^32 - 1 */
utc_time_t time4 = timestamp2utc(max_ts);
unsigned int ts4 = utc2timestamp(&time4);
/* Test case 5: Random time point (2023-12-25 18:30:00) */
utc_time_t time5 = {2023, 12, 25, 18, 30, 0};
unsigned int ts5 = utc2timestamp(&time5);
utc_time_t time5_back = timestamp2utc(ts5);
/* Verify test case 1 */
if (ts1 == 0 && time1_back.year == 2000 && time1_back.month == 1 && time1_back.day == 1 && time1_back.hour == 0
&& time1_back.minute == 0 && time1_back.second == 0) {
printf("1 pass\n");
}
/* Verify test case 2 */
if (ts2 == 86400 && time2_back.year == 2000 && time2_back.month == 1 && time2_back.day == 2 && time2_back.hour == 0
&& time2_back.minute == 0 && time2_back.second == 0) {
printf("2 pass\n");
}
/* Verify test case 3 */
if (time3.year == time3_back.year && time3.month == time3_back.month && time3.day == time3_back.day
&& time3.hour == time3_back.hour && time3.minute == time3_back.minute && time3.second == time3_back.second) {
printf("3 pass\n");
}
/* Verify bidirectional conversion consistency of maximum timestamp */
if (ts4 == max_ts) {
printf("4 pass\n");
}
/* Verify bidirectional conversion consistency of test case 5 */
if (time5.year == time5_back.year && time5.month == time5_back.month && time5.day == time5_back.day
&& time5.hour == time5_back.hour && time5.minute == time5_back.minute && time5.second == time5_back.second) {
printf("5 pass\n");
}
}
/* Main function example */
int main() {
/* Run tests */
test_timestamp_functions();
/* Example usage: Convert current time to timestamp */
utc_time_t current_time = {2024, 6, 15, 14, 30, 45};
unsigned int current_timestamp = utc2timestamp(¤t_time);
/* Example usage: Convert timestamp back to UTC time */
utc_time_t converted_time = timestamp2utc(current_timestamp);
printf("timestamp: %u\n", current_timestamp);
printf("%u, %u, %u, %u, %u, %u\n", converted_time.year, converted_time.month, converted_time.day,
converted_time.hour, converted_time.minute, converted_time.second);
return 0;
}