- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.c
More file actions
Latest commit
55 lines (47 loc) · 1.21 KB
/
Copy pathtimestamp.c
File metadata and controls
55 lines (47 loc) · 1.21 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
#include"timestamp.h"
#include<assert.h>
#include<string.h>
//see: https://tools.ietf.org/html/rfc2616#section-3.3.1
//example: Sun, 06 Nov 1994 08:49:37 GMT
staticconstchar*RFC_1123_DATE="%a, %d %b %Y %T GMT";
inttime2str(consttime_t*timestamp, char*buf, size_tsize) {
assert(timestamp);
assert(buf);
assert(size>31);
structtmgmt_timestamp;
if(!gmtime_r(timestamp, &gmt_timestamp)) {
return-1;
}
if(!strftime(buf, size-1, RFC_1123_DATE, &gmt_timestamp)) {
return-1;
}
return0;
}
//OBSOLETE formats for backward compatibility
//example: Sunday, 06-Nov-94 08:49:37 GMT
staticconstchar*RFC_850_DATE="%A, %d-%b-%y %T GMT";
//example: Sun Nov 6 08:49:37 1994
staticconstchar*ANSI_C_DATE="%a %d %e %T %Y";
intstr2time(constchar*buf, time_t*timestamp) {
assert(buf);
assert(timestamp);
structtmtm;
char*r;
constchar**fmt;
constchar*DATE_FORMATS[] = {
RFC_1123_DATE, RFC_850_DATE, ANSI_C_DATE, NULL
};
//try each format
for(fmt=DATE_FORMATS; *fmt; ++fmt) {
memset(&tm, 0, sizeof(structtm));
r=strptime(buf, *fmt, &tm);
if(r&&*r=='\0') {
//timegm is nonstandard
*timestamp=timegm(&tm);
if(*timestamp!=-1) {
return0;
}
}
}
return-1;
}