d9898ee8 |
1 | /* |
2 | ** Copyright 1998 - 1999 Double Precision, Inc. |
3 | ** See COPYING for distribution information. |
4 | */ |
5 | |
6 | /* |
7 | ** $Id: rfc822_mkdate.c,v 1.4 2002/04/09 02:49:55 mrsam Exp $ |
8 | */ |
9 | |
10 | #include "rfc822.h" |
11 | |
12 | #include <sys/types.h> |
13 | #include <time.h> |
14 | #include <stdio.h> |
15 | #include <string.h> |
16 | #if HAVE_UNISTD_H |
17 | #include <unistd.h> |
18 | #endif |
19 | |
20 | static const char * const months[]={ |
21 | "Jan", |
22 | "Feb", |
23 | "Mar", |
24 | "Apr", |
25 | "May", |
26 | "Jun", |
27 | "Jul", |
28 | "Aug", |
29 | "Sep", |
30 | "Oct", |
31 | "Nov", |
32 | "Dec"}; |
33 | |
34 | static const char * const wdays[]={ |
35 | "Sun", |
36 | "Mon", |
37 | "Tue", |
38 | "Wed", |
39 | "Thu", |
40 | "Fri", |
41 | "Sat"}; |
42 | |
43 | void rfc822_mkdate_buf(time_t t, char *buf) |
44 | { |
45 | struct tm *p; |
46 | int offset; |
47 | |
48 | #if USE_TIME_ALTZONE |
49 | |
50 | p=localtime(&t); |
51 | offset= -(int)timezone; |
52 | |
53 | if (p->tm_isdst > 0) |
54 | offset= -(int)altzone; |
55 | |
56 | if (offset % 60) |
57 | { |
58 | offset=0; |
59 | p=gmtime(&t); |
60 | } |
61 | offset /= 60; |
62 | #else |
63 | #if USE_TIME_DAYLIGHT |
64 | |
65 | p=localtime(&t); |
66 | offset= -(int)timezone; |
67 | |
68 | if (p->tm_isdst > 0) |
69 | offset += 60*60; |
70 | if (offset % 60) |
71 | { |
72 | offset=0; |
73 | p=gmtime(&t); |
74 | } |
75 | offset /= 60; |
76 | #else |
77 | #if USE_TIME_GMTOFF |
78 | p=localtime(&t); |
79 | offset= p->tm_gmtoff; |
80 | |
81 | if (offset % 60) |
82 | { |
83 | offset=0; |
84 | p=gmtime(&t); |
85 | } |
86 | offset /= 60; |
87 | #else |
88 | p=gmtime(&t); |
89 | offset=0; |
90 | #endif |
91 | #endif |
92 | #endif |
93 | |
94 | offset = (offset % 60) + offset / 60 * 100; |
95 | |
96 | sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d %+05d", |
97 | wdays[p->tm_wday], |
98 | p->tm_mday, |
99 | months[p->tm_mon], |
100 | p->tm_year+1900, |
101 | p->tm_hour, |
102 | p->tm_min, |
103 | p->tm_sec, |
104 | offset); |
105 | } |
106 | |
107 | const char *rfc822_mkdate(time_t t) |
108 | { |
109 | static char buf[50]; |
110 | |
111 | rfc822_mkdate_buf(t, buf); |
112 | return (buf); |
113 | } |