-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime.cpp.in
81 lines (63 loc) · 2.23 KB
/
datetime.cpp.in
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
#include <ctime>
#include <cstdint>
#define int_type @SOUFFLE_INT_TYPE@
extern "C" {
/*
Timestamp based on C's time_t converted to int_type.
Currently ignores struct tm (except as an intermediate format).
One issue with struct tm is that we would need multiple functor calls to return the individual
components.
Primarily use localtime time_t values (also known as Unix timestamps).
Adjust for localdiff() when necessary -- see to_day().
TODO: select a standard date-time API.
*/
int_type localtimestamp() {
return time(0);
}
int_type to_timestamp(const char* string, const char* format) {
struct tm tmInfo = {0};
strptime(string, format, &tmInfo);
return mktime(&tmInfo); // localtime
}
int_type from_date(const char* date) {
return to_timestamp(date, "%Y-%m-%d");
}
int_type from_date_time(const char* date) {
return to_timestamp(date, "%Y-%m-%d %H:%M:%S");
}
int_type age(const int_type event_timestamp, const int_type birth_timestamp) {
time_t birth(birth_timestamp), event(event_timestamp);
struct tm tmBirth = *localtime(&birth), tmEvent = *localtime(&event);
int_type a = tmEvent.tm_year - tmBirth.tm_year;
if ((tmEvent.tm_mon < tmBirth.tm_mon) ||
((tmEvent.tm_mon == tmBirth.tm_mon) && (tmEvent.tm_mday < tmBirth.tm_mday)))
a -= 1;
return a;
}
int_type localdiff() {
time_t rawtime = time(NULL);
struct tm *ptm = gmtime(&rawtime);
int tm_isdst = ptm->tm_isdst;
// Request that mktime() looks up dst in timezone database
ptm->tm_isdst = -1;
time_t gmt = mktime(ptm);
return difftime(rawtime, gmt)-3600*ptm->tm_isdst;
}
int_type to_days(int_type timestamp) {
// localtime adjustment for correct day calculations
return (timestamp+localdiff())/86400;
}
const char* from_timestamp(int_type timestamp, const char *format) {
char * buf = new char[100]; // is this memory safe?
time_t t(timestamp);
strftime(buf, 100, format, localtime(&t));
return buf;
}
const char* to_date(int_type timestamp) {
return from_timestamp(timestamp, "%Y-%m-%d");
}
const char* to_date_time(int_type timestamp) {
return from_timestamp(timestamp, "%Y-%m-%d %H:%M:%S");
}
}
#undef int_type