fmt_date.c

00001 /*
00002  * $Id: fmt_date.c 5147 2007-05-08 15:36:22Z ajc $
00003  */
00009 #include "webcit.h"
00010 #include "webserver.h"
00011 
00012 typedef unsigned char byte; 
00014 #define FALSE 0 
00015 #define TRUE 1 
00026 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
00027 {
00028 #ifdef ENABLE_NLS
00029         if (wc_locales[WC->selected_language] == NULL) {
00030                 return strftime(s, max, format, tm);
00031         }
00032         else {
00033                 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
00034         }
00035 #else
00036         return strftime(s, max, format, tm);
00037 #endif
00038 }
00039 
00040 
00047 void fmt_date(char *buf, time_t thetime, int brief)
00048 {
00049         struct tm tm;
00050         struct tm today_tm;
00051         time_t today_timet;
00052         int hour;
00053         char calhourformat[16];
00054 
00055         get_preference("calhourformat", calhourformat, sizeof calhourformat);
00056 
00057         today_timet = time(NULL);
00058         localtime_r(&today_timet, &today_tm);
00059 
00060         localtime_r(&thetime, &tm);
00061         hour = tm.tm_hour;
00062         if (hour == 0)
00063                 hour = 12;
00064         else if (hour > 12)
00065                 hour = hour - 12;
00066 
00067         buf[0] = 0;
00068 
00069         if (brief) {
00070 
00072                 if ((tm.tm_year == today_tm.tm_year)
00073                   &&(tm.tm_mon == today_tm.tm_mon)
00074                   &&(tm.tm_mday == today_tm.tm_mday)) {
00075                         wc_strftime(buf, 32, "%l:%M%p", &tm);
00076                 }
00079                 else if (today_timet - thetime < 15552000) {
00080                         wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
00081                 }
00083                 else {
00084                         wc_strftime(buf, 32, "%b %d %Y", &tm);
00085                 }
00086         }
00087         else {
00088                 wc_strftime(buf, 32, "%c", &tm);
00089         }
00090 }
00091 
00092 
00098 void fmt_time(char *buf, time_t thetime)
00099 {
00100         struct tm *tm;
00101         int hour;
00102         char calhourformat[16];
00103 
00104         get_preference("calhourformat", calhourformat, sizeof calhourformat);
00105 
00106         buf[0] = 0;
00107         tm = localtime(&thetime);
00108         hour = tm->tm_hour;
00109         if (hour == 0)
00110                 hour = 12;
00111         else if (hour > 12)
00112                 hour = hour - 12;
00113 
00114         if (!strcasecmp(calhourformat, "24")) {
00115                 sprintf(buf, "%2d:%02d",
00116                         tm->tm_hour, tm->tm_min
00117                 );
00118         }
00119         else {
00120                 sprintf(buf, "%d:%02d%s",
00121                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
00122                 );
00123         }
00124 }
00125 
00126 
00127 
00128 
00137 time_t httpdate_to_timestamp(char *buf)
00138 {
00139         time_t t = 0;
00140         struct tm tt;
00141         char *c;
00142         char tz[256];
00143 
00145         for (c = buf; *c != ' '; c++)
00146                 ;
00147         c++;
00148 
00149         /* Get day of month */
00150         tt.tm_mday = atoi(c);
00151         for (; *c != ' ' && *c != '-'; c++);
00152         c++;
00153 
00155         switch (*c) {
00156         case 'A':       
00157                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
00158                 break;
00159         case 'D':       
00160                 tt.tm_mon = 11;
00161                 break;
00162         case 'F':       
00163                 tt.tm_mon = 1;
00164                 break;
00165         case 'M':       
00166                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
00167                 break;
00168         case 'J':       
00169                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
00170                 break;
00171         case 'N':       
00172                 tt.tm_mon = 10;
00173                 break;
00174         case 'O':       
00175                 tt.tm_mon = 9;
00176                 break;
00177         case 'S':       
00178                 tt.tm_mon = 8;
00179                 break;
00180         default:
00181                 return 42;
00182                 break;  
00183         }
00184         c += 4;
00185 
00186         tt.tm_year = 0;
00188         tt.tm_year = atoi(c);
00189         for (; *c != ' '; c++);
00190         c++;
00191         if (tt.tm_year >= 1900)
00192                 tt.tm_year -= 1900;
00193 
00195         tt.tm_hour = atoi(c);
00196         for (; *c != ':'; c++);
00197         c++;
00198 
00200         tt.tm_min = atoi(c);
00201         for (; *c != ':'; c++);
00202         c++;
00203 
00205         tt.tm_sec = atoi(c);
00206         for (; *c && *c != ' '; c++);
00207 
00210         if (getenv("TZ"))
00211                 sprintf(tz, "TZ=%s", getenv("TZ"));
00212         else
00213                 strcpy(tz, "TZ=");
00214         putenv("TZ=UTC");
00215         tzset();
00216         t = mktime(&tt);
00217         putenv(tz);
00218         tzset();
00219         return t;
00220 }
00221 
00222 
00223 
00224 

Generated on Wed Jun 20 23:13:08 2007 for webcit by  doxygen 1.5.2