X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fgenstamp.c;h=9c5ba5f3d6e613c3c1f15ecb2781ac3438a49bb5;hb=2b80e75820618944e1c75b9c01aeeefc8b6b0c81;hp=4c7cf0d005aa51e00483e4c0a160bfbd4e83e917;hpb=83875cf99a93ec708989894477f3880cf4579630;p=citadel.git diff --git a/citadel/genstamp.c b/citadel/genstamp.c index 4c7cf0d00..9c5ba5f3d 100644 --- a/citadel/genstamp.c +++ b/citadel/genstamp.c @@ -1,15 +1,24 @@ /* - * $Id$ - * * Function to generate RFC822-compliant textual time/date stamp - * */ +#include "sysdep.h" #include #include #include #include -#include + +#if TIME_WITH_SYS_TIME +# include +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# endif +#endif + #include "genstamp.h" @@ -27,20 +36,60 @@ static char *weekdays[] = { * Supplied with a unix timestamp, generate an RFC822-compliant textual * time and date stamp. */ -void generate_rfc822_datestamp(char *buf, time_t xtime) { - struct tm *t; - - t = localtime(&xtime); - - sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d %s", - weekdays[t->tm_wday], - t->tm_mday, - months[t->tm_mon], - t->tm_year + 1900, - t->tm_hour, - t->tm_min, - t->tm_sec, - tzname[0] - ); -} +long datestring(char *buf, size_t n, time_t xtime, int which_format) { + struct tm t; + + long offset; + char offsign; + + localtime_r(&xtime, &t); + /* Convert "seconds west of GMT" to "hours/minutes offset" */ +#ifdef HAVE_STRUCT_TM_TM_GMTOFF + offset = t.tm_gmtoff; +#else + offset = timezone; +#endif + if (offset > 0) { + offsign = '+'; + } + else { + offset = 0L - offset; + offsign = '-'; + } + offset = ( (offset / 3600) * 100 ) + ( offset % 60 ); + + switch(which_format) { + + case DATESTRING_RFC822: + return snprintf( + buf, n, + "%s, %02d %s %04d %02d:%02d:%02d %c%04ld", + weekdays[t.tm_wday], + t.tm_mday, + months[t.tm_mon], + t.tm_year + 1900, + t.tm_hour, + t.tm_min, + t.tm_sec, + offsign, offset + ); + break; + + case DATESTRING_IMAP: + return snprintf( + buf, n, + "%02d-%s-%04d %02d:%02d:%02d %c%04ld", + t.tm_mday, + months[t.tm_mon], + t.tm_year + 1900, + t.tm_hour, + t.tm_min, + t.tm_sec, + offsign, offset + ); + break; + + } + return 0; +}