-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPancakeDateTime.c
65 lines (47 loc) · 1.74 KB
/
PancakeDateTime.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
57
58
59
60
61
62
63
64
65
#include "PancakeDateTime.h"
/*
* Pancake date and time formatting API
*/
static UByte *RFC1123Days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static UByte *RFC1123Months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
PANCAKE_API String PancakeFormatDate(Native time) {
struct tm *timeStruct = localtime(&time);
String formatted;
PancakeAssert(timeStruct != NULL);
formatted.value = PancakeAllocate(sizeof("1970-01-01"));
formatted.length = strftime(formatted.value, sizeof("1970-01-01"), "%Y-%m-%d", timeStruct);
return formatted;
}
PANCAKE_API String PancakeFormatDateTime(Native time) {
struct tm *timeStruct = localtime(&time);
String formatted;
PancakeAssert(timeStruct != NULL);
formatted.value = PancakeAllocate(sizeof("1970-01-01 01:00:00"));
formatted.length = strftime(formatted.value, sizeof("1970-01-01 01:00:00"), "%Y-%m-%d %H:%M:%S", timeStruct);
return formatted;
}
PANCAKE_API void PancakeRFC1123Date(Native time, UByte *buf) {
struct tm *now = localtime(&time);
strftime(buf, 30, "___, %d ___ %Y %H:%M:%S GMT", now);
memcpy(buf, RFC1123Days[now->tm_wday], 3);
memcpy(buf + 8, RFC1123Months[now->tm_mon], 3);
}
PANCAKE_API void PancakeRFC1123CurrentDate(UByte *buf) {
struct tm *now;
static Native cachedTime = 0;
static UByte cache[29];
Native currentTime;
currentTime = time(NULL);
if(cachedTime == currentTime) {
// Cached timestamp equals current timestamp, copy from cache
memcpy(buf, cache, 29);
return;
}
now = localtime(¤tTime);
strftime(buf, 30, "___, %d ___ %Y %H:%M:%S GMT", now);
memcpy(buf, RFC1123Days[now->tm_wday], 3);
memcpy(buf + 8, RFC1123Months[now->tm_mon], 3);
// Cache date
memcpy(cache, buf, 29);
cachedTime = currentTime;
}