* Added a "preferences and settings" screen for each user.
[citadel.git] / webcit / fmt_date.c
1 /*
2  * $Id$
3  *
4  * Miscellaneous routines 
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <sys/time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30 typedef unsigned char byte;
31
32 #define FALSE 0
33 #define TRUE 1
34
35 char *ascmonths[] = {
36         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
37         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
38 };
39
40 char *ascdays[] = {
41         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
42 };
43
44 /*
45  * Format a date/time stamp for output 
46  */
47 void fmt_date(char *buf, time_t thetime, int brief)
48 {
49         struct tm tm;
50         struct tm today_tm;
51         time_t today_timet;
52         int hour;
53         char calhourformat[16];
54
55         get_preference("calhourformat", calhourformat, sizeof calhourformat);
56
57         today_timet = time(NULL);
58         localtime_r(&today_timet, &today_tm);
59
60         localtime_r(&thetime, &tm);
61         hour = tm.tm_hour;
62         if (hour == 0)
63                 hour = 12;
64         else if (hour > 12)
65                 hour = hour - 12;
66
67         buf[0] = 0;
68
69         if (brief) {
70
71                 if ((tm.tm_year == today_tm.tm_year)
72                   &&(tm.tm_mon == today_tm.tm_mon)
73                   &&(tm.tm_mday == today_tm.tm_mday)) {
74                         if (!strcasecmp(calhourformat, "24")) {
75                                 sprintf(buf, "%2d:%02d",
76                                         tm.tm_hour, tm.tm_min
77                                 );
78                         }
79                         else {
80                                 sprintf(buf, "%2d:%02d%s",
81                                         hour, tm.tm_min,
82                                         ((tm.tm_hour >= 12) ? "pm" : "am")
83                                 );
84                         }
85                 }
86                 else {
87                         sprintf(buf, "%s %d %d",
88                                 ascmonths[tm.tm_mon],
89                                 tm.tm_mday,
90                                 tm.tm_year + 1900
91                         );
92                 }
93         }
94         else {
95                 if (!strcasecmp(calhourformat, "24")) {
96                         sprintf(buf, "%s %d %d %2d:%02d",
97                                 ascmonths[tm.tm_mon],
98                                 tm.tm_mday,
99                                 tm.tm_year + 1900,
100                                 tm.tm_hour, tm.tm_min
101                         );
102                 }
103                 else {
104                         sprintf(buf, "%s %d %d %2d:%02d%s",
105                                 ascmonths[tm.tm_mon],
106                                 tm.tm_mday,
107                                 tm.tm_year + 1900,
108                                 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
109                         );
110                 }
111         }
112 }
113
114
115
116 /*
117  * Format TIME ONLY for output 
118  */
119 void fmt_time(char *buf, time_t thetime)
120 {
121         struct tm *tm;
122         int hour;
123         char calhourformat[16];
124
125         get_preference("calhourformat", calhourformat, sizeof calhourformat);
126
127         buf[0] = 0;
128         tm = localtime(&thetime);
129         hour = tm->tm_hour;
130         if (hour == 0)
131                 hour = 12;
132         else if (hour > 12)
133                 hour = hour - 12;
134
135         if (!strcasecmp(calhourformat, "24")) {
136                 sprintf(buf, "%2d:%02d",
137                         tm->tm_hour, tm->tm_min
138                 );
139         }
140         else {
141                 sprintf(buf, "%d:%02d%s",
142                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
143                 );
144         }
145 }
146
147
148
149
150 /*
151  * Format a date/time stamp to the format used in HTTP headers
152  */
153 void httpdate(char *buf, time_t thetime)
154 {
155         struct tm *tm;
156
157         buf[0] = 0;
158         tm = localtime(&thetime);
159
160         sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
161                 ascdays[tm->tm_wday],
162                 tm->tm_mday,
163                 ascmonths[tm->tm_mon],
164                 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
165 }
166
167
168
169
170