Back to index

webcit  8.12-dfsg
http_datestring.c
Go to the documentation of this file.
00001 #include "webcit.h"
00002 
00004 static char *httpdate_months[] = {
00005        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00006        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
00007 };
00008 
00010 static char *httpdate_weekdays[] = {
00011        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
00012 };
00013 
00014 
00021 void http_datestring(char *buf, size_t n, time_t xtime) {
00022        struct tm t;
00023 
00024        long offset;
00025        char offsign;
00026 
00027        localtime_r(&xtime, &t);
00028 
00030 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
00031        offset = t.tm_gmtoff;
00032 #else
00033        offset = timezone;
00034 #endif
00035        if (offset > 0) {
00036               offsign = '+';
00037        }
00038        else {
00039               offset = 0L - offset;
00040               offsign = '-';
00041        }
00042        offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
00043 
00044        snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
00045               httpdate_weekdays[t.tm_wday],
00046               t.tm_mday,
00047               httpdate_months[t.tm_mon],
00048               t.tm_year + 1900,
00049               t.tm_hour,
00050               t.tm_min,
00051               t.tm_sec,
00052               offsign, offset
00053        );
00054 }
00055 
00056 
00057 void tmplput_nowstr(StrBuf *Target, WCTemplputParams *TP)
00058 {
00059        char buf[64];
00060        long bufused;
00061        time_t now;
00062        
00063        now = time(NULL);
00064 #ifdef HAVE_SOLARIS_LOCALTIME_R
00065        asctime_r(localtime(&now), buf, sizeof(buf));
00066 #else
00067        asctime_r(localtime(&now), buf);
00068 #endif
00069        bufused = strlen(buf);
00070        if ((bufused > 0) && (buf[bufused - 1] == '\n')) {
00071               buf[bufused - 1] = '\0';
00072               bufused --;
00073        }
00074        StrEscAppend(Target, NULL, buf, 0, 0);
00075 }
00076 void tmplput_nowno(StrBuf *Target, WCTemplputParams *TP)
00077 {
00078        time_t now;
00079        now = time(NULL);
00080        StrBufAppendPrintf(Target, "%ld", now);
00081 }
00082 
00083 void 
00084 InitModule_DATE
00085 (void)
00086 {
00087        RegisterNamespace("DATE:NOW:STR", 0, 0, tmplput_nowstr, NULL, CTX_NONE);
00088        RegisterNamespace("DATE:NOW:NO", 0, 0, tmplput_nowno, NULL, CTX_NONE);
00089 }