-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimeManager.c
56 lines (50 loc) · 1.34 KB
/
timeManager.c
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
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
/* additional imports */
#include "timeManager.h"
/*
Convert a string to a time_t struct
The string should be in the format "YYYYMMDDTHHMMSS"
*/
time_t processStringToTimeStruct(char *timeString)
{
char* dateStartToken;
struct tm tm = {0};
time_t time;
if (strstr(timeString, "TZID") != NULL) {
dateStartToken = strchr(timeString, ':') + 1;
} else {
dateStartToken = timeString;
}
if (sscanf(dateStartToken, "%4d%2d%2dT%2d%2d%2d",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
return -1;
}
tm.tm_year -= 1900;
tm.tm_mon -= 1;
time = mktime(&tm);
if (time == -1) {
return -1;
}
return time;
}
/*
Convert time_t back to string YYYYMMDDTHHMMSS
*/
char *processTimeStructToString(const time_t *time)
{
struct tm *timeStruct = localtime(time);
char *timeString = (char *)malloc(15);
snprintf(timeString, 15, "%d%02d%02dT%02d%02d%02d", timeStruct->tm_year + 1900, timeStruct->tm_mon + 1, timeStruct->tm_mday, timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec);
return timeString;
}
/*
Print time_t to stdout
*/
void printTime(const time_t *time)
{
printf("Time: %s", ctime(time));
}