* When translating the input from a REGI command into a vCard, set the telephone...
[citadel.git] / citadel / genstamp.c
blobcbf89d4488c0364ab792a98cbb00d2dd1e5f9449
1 /*
2 * $Id$
4 * Function to generate RFC822-compliant textual time/date stamp
6 */
8 #include "sysdep.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
16 # include <time.h>
17 #else
18 # if HAVE_SYS_TIME_H
19 # include <sys/time.h>
20 # else
21 # include <time.h>
22 # endif
23 #endif
25 #include "genstamp.h"
28 static char *months[] = {
29 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
30 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
33 static char *weekdays[] = {
34 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
39 * Supplied with a unix timestamp, generate an RFC822-compliant textual
40 * time and date stamp.
42 void datestring(char *buf, size_t n, time_t xtime, int which_format) {
43 struct tm t;
45 long offset;
46 char offsign;
48 localtime_r(&xtime, &t);
50 /* Convert "seconds west of GMT" to "hours/minutes offset" */
51 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
52 offset = t.tm_gmtoff;
53 #else
54 offset = timezone;
55 #endif
56 if (offset > 0) {
57 offsign = '+';
59 else {
60 offset = 0L - offset;
61 offsign = '-';
63 offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
65 switch(which_format) {
67 case DATESTRING_RFC822:
68 snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
69 weekdays[t.tm_wday],
70 t.tm_mday,
71 months[t.tm_mon],
72 t.tm_year + 1900,
73 t.tm_hour,
74 t.tm_min,
75 t.tm_sec,
76 offsign, offset
78 break;
80 case DATESTRING_IMAP:
81 snprintf(buf, n, "%02d-%s-%04d %02d:%02d:%02d %c%04ld",
82 t.tm_mday,
83 months[t.tm_mon],
84 t.tm_year + 1900,
85 t.tm_hour,
86 t.tm_min,
87 t.tm_sec,
88 offsign, offset
90 break;