File: http_datestring.c

package info (click to toggle)
webcit 917-dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,164 kB
  • sloc: ansic: 34,213; sh: 4,455; makefile: 346; xml: 91; sed: 9
file content (89 lines) | stat: -rw-r--r-- 1,960 bytes parent folder | download | duplicates (4)
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
#include "webcit.h"

/** HTTP Months - do not translate - these are not for human consumption */
static char *httpdate_months[] = {
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

/** HTTP Weekdays - do not translate - these are not for human consumption */
static char *httpdate_weekdays[] = {
	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};


/**
 * \brief Supplied with a unix timestamp, generate a textual time/date stamp
 * \param buf the return buffer
 * \param n the size of the buffer
 * \param xtime the time to format as string
 */
void http_datestring(char *buf, size_t n, time_t xtime) {
	struct tm t;

	long offset;
	char offsign;

	localtime_r(&xtime, &t);

	/** Convert "seconds west of GMT" to "hours/minutes offset" */
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
	offset = t.tm_gmtoff;
#else
	offset = timezone;
#endif
	if (offset > 0) {
		offsign = '+';
	}
	else {
		offset = 0L - offset;
		offsign = '-';
	}
	offset = ( (offset / 3600) * 100 ) + ( offset % 60 );

	snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
		httpdate_weekdays[t.tm_wday],
		t.tm_mday,
		httpdate_months[t.tm_mon],
		t.tm_year + 1900,
		t.tm_hour,
		t.tm_min,
		t.tm_sec,
		offsign, offset
	);
}


void tmplput_nowstr(StrBuf *Target, WCTemplputParams *TP)
{
	char buf[64];
	long bufused;
	time_t now;
	
	now = time(NULL);
#ifdef HAVE_SOLARIS_LOCALTIME_R
	asctime_r(localtime(&now), buf, sizeof(buf));
#else
	asctime_r(localtime(&now), buf);
#endif
	bufused = strlen(buf);
	if ((bufused > 0) && (buf[bufused - 1] == '\n')) {
		buf[bufused - 1] = '\0';
		bufused --;
	}
	StrEscAppend(Target, NULL, buf, 0, 0);
}
void tmplput_nowno(StrBuf *Target, WCTemplputParams *TP)
{
	time_t now;
	now = time(NULL);
	StrBufAppendPrintf(Target, "%ld", now);
}

void 
InitModule_DATE
(void)
{
	RegisterNamespace("DATE:NOW:STR", 0, 0, tmplput_nowstr, NULL, CTX_NONE);
	RegisterNamespace("DATE:NOW:NO", 0, 0, tmplput_nowno, NULL, CTX_NONE);
}