summaryrefslogtreecommitdiff
blob: 3c872374545109cc6b348a8bb69a970bfddfc7a5 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software 
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/

/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
* 
******************************************************************************/

#include <string.h>
#include <stdio.h>
#include <time.h>

/* epoch time -> 8.4.26.1 */
void epochToLongString(time_t epoch, char* longString)
{
    struct tm* timeStruct;
    
    localtime(&epoch);
    timeStruct = gmtime(&epoch);
    
    sprintf(longString, "%4d%02d%02d%02d%02d%02d%02d",
                        timeStruct->tm_year + 1900,
                        timeStruct->tm_mon + 1,
                        timeStruct->tm_mday,
                        timeStruct->tm_hour,
                        timeStruct->tm_min,
                        timeStruct->tm_sec,
                        0);
    
    /* this may not be 7.1.1 but since it's 0 who cares */
    longString[16] = 0;
}

/* epoch time -> 9.1.5 */
void epochToShortString(time_t epoch, char* shortString)
{
    struct tm* timeStruct;
    
    localtime(&epoch);
    timeStruct = gmtime(&epoch);
    
    shortString[0] = timeStruct->tm_year;
    shortString[1] = timeStruct->tm_mon + 1;
    shortString[2] = timeStruct->tm_mday;
    shortString[3] = timeStruct->tm_hour;
    shortString[4] = timeStruct->tm_min;
    shortString[5] = timeStruct->tm_sec;
    
    /* gmt offset */
    shortString[6] = 0;
}

/* 8.4.26.1 -> epoch time */
void longStringToEpoch(const char* longString, time_t* epoch)
{
    char str[5];
    int number;
    struct tm timeStruct;
    
    /* no daylight savings setting available */
    timeStruct.tm_isdst = -1;
    
    /* YEAR */
    strncpy(str, longString, 4);
    str[4] = '\0';
    
    sscanf(str, "%d", &number);
    
    timeStruct.tm_year = number - 1900;
    /* END YEAR */
    
    /* MONTH */
    strncpy(str, longString + 4, 2);
    str[2] = '\0';
    
    sscanf(str, "%d", &number);
    
    timeStruct.tm_mon = number - 1;
    /* END MONTH */
    
    /* DAY */
    strncpy(str, longString + 6, 2);
    str[2] = '\0';
    
    sscanf(str, "%d", &number);
    
    timeStruct.tm_mday = number;
    /* END DAY */
    
    /* HOUR */
    strncpy(str, longString + 8, 2);
    str[2] = '\0';
    
    sscanf(str, "%d", &number);
    
    timeStruct.tm_hour = number;
    /* END HOUR */
    
    /* MINUTE */
    strncpy(str, longString + 10, 2);
    str[2] = '\0';
    
    sscanf(str, "%d", &number);
    
    timeStruct.tm_min = number;
    /* END MINUTE */
    
    /* SECOND */
    strncpy(str, longString + 12, 2);
    str[2] = '\0';
    
    sscanf(str, "%d", &number);
    
    timeStruct.tm_sec = number;
    /* END SECOND */
    
    *epoch = mktime(&timeStruct);
}