-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc822_date.c
36 lines (33 loc) · 1004 Bytes
/
rfc822_date.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
#include <stdio.h>
#include <time.h>
#include <locale.h>
char *
rfc822_date(when)
time_t when;
{
char *old_locale;
struct tm *lt;
struct tm gmt;
int gmtoff, end, len;
static char arpadate[BUFSIZ];
old_locale = setlocale(LC_ALL, "");
gmt = *gmtime(&when);
lt = localtime(&when);
/* GMT offset calculation adapted from sendmail */
gmtoff = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
if (lt->tm_year < gmt.tm_year)
gmtoff -= 24 * 60;
else if (lt->tm_year > gmt.tm_year)
gmtoff += 24 * 60;
else if (lt->tm_yday < gmt.tm_yday)
gmtoff -= 24 * 60;
else if (lt->tm_yday > gmt.tm_yday)
gmtoff += 24 * 60;
/* %e is not in ANSI X3.159-1989 */
end = strftime(arpadate, sizeof(arpadate)-1, "%a, %e %b %Y %H:%M:%S ", lt);
(void) sprintf(&arpadate[end], "%+03d%02d ", (int) (gmtoff / 60), (int) (gmtoff % 60));
len = strlen(arpadate);
(void) strftime(&arpadate[len], sizeof(arpadate) - len - 1, "(%Z)", lt);
(void) setlocale(LC_ALL, old_locale);
return arpadate;
}