Back to index

citadel  8.12
genstamp.c
Go to the documentation of this file.
00001 /*
00002  * Function to generate RFC822-compliant textual time/date stamp
00003  */
00004 
00005 #include "sysdep.h"
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <ctype.h>
00009 #include <string.h>
00010 
00011 #if TIME_WITH_SYS_TIME
00012 # include <sys/time.h>
00013 # include <time.h>
00014 #else
00015 # if HAVE_SYS_TIME_H
00016 #  include <sys/time.h>
00017 # else
00018 #  include <time.h>
00019 # endif
00020 #endif
00021 
00022 #include "genstamp.h"
00023 
00024 
00025 static char *months[] = {
00026        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00027        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
00028 };
00029 
00030 static char *weekdays[] = {
00031        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
00032 };
00033 
00034 
00035 /*
00036  * Supplied with a unix timestamp, generate an RFC822-compliant textual
00037  * time and date stamp.
00038  */
00039 void datestring(char *buf, size_t n, time_t xtime, int which_format) {
00040        struct tm t;
00041 
00042        long offset;
00043        char offsign;
00044 
00045        localtime_r(&xtime, &t);
00046 
00047        /* Convert "seconds west of GMT" to "hours/minutes offset" */
00048 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
00049        offset = t.tm_gmtoff;
00050 #else
00051        offset = timezone;
00052 #endif
00053        if (offset > 0) {
00054               offsign = '+';
00055        }
00056        else {
00057               offset = 0L - offset;
00058               offsign = '-';
00059        }
00060        offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
00061 
00062        switch(which_format) {
00063 
00064               case DATESTRING_RFC822:
00065                      snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
00066                             weekdays[t.tm_wday],
00067                             t.tm_mday,
00068                             months[t.tm_mon],
00069                             t.tm_year + 1900,
00070                             t.tm_hour,
00071                             t.tm_min,
00072                             t.tm_sec,
00073                             offsign, offset
00074                             );
00075               break;
00076 
00077               case DATESTRING_IMAP:
00078                      snprintf(buf, n, "%02d-%s-%04d %02d:%02d:%02d %c%04ld",
00079                             t.tm_mday,
00080                             months[t.tm_mon],
00081                             t.tm_year + 1900,
00082                             t.tm_hour,
00083                             t.tm_min,
00084                             t.tm_sec,
00085                             offsign, offset
00086                             );
00087               break;
00088 
00089        }
00090 }