From e23cf6db4aba7a0831388faab91249c1d086f082 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Sun, 12 Feb 2006 06:34:37 +0000 Subject: [PATCH] More conversion of date/time strings to strftime-based functions. Still need to tweak a few things here and there. --- webcit/calendar.c | 2 +- webcit/calendar_tools.c | 2 +- webcit/calendar_view.c | 41 ++++++++----- webcit/event.c | 5 +- webcit/fmt_date.c | 133 ++++++++++++++++------------------------ webcit/summary.c | 2 +- webcit/webcit.h | 8 +-- webcit/webserver.c | 1 - 8 files changed, 86 insertions(+), 108 deletions(-) diff --git a/webcit/calendar.c b/webcit/calendar.c index 37621ef65..09735ea79 100644 --- a/webcit/calendar.c +++ b/webcit/calendar.c @@ -167,7 +167,7 @@ void cal_process_object(icalcomponent *cal, wprintf(_("Date:")); wprintf("" "%s %d, %d", - months[t.month - 1], + monthname(t.month - 1), t.day, t.year ); } diff --git a/webcit/calendar_tools.c b/webcit/calendar_tools.c index 72ee508a6..58e62146f 100644 --- a/webcit/calendar_tools.c +++ b/webcit/calendar_tools.c @@ -73,7 +73,7 @@ void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) { wprintf("\n", ((tm.tm_mon == i) ? "SELECTED" : ""), i+1, - months[i] + monthname(i) ); } wprintf("\n"); diff --git a/webcit/calendar_view.c b/webcit/calendar_view.c index d071ca4a2..a82e731b9 100644 --- a/webcit/calendar_view.c +++ b/webcit/calendar_view.c @@ -196,10 +196,8 @@ void calendar_month_view_brief_events(time_t thetime, const char *daycolor) { escputs((char *) icalproperty_get_comment(p)); /** \todo: allso ammitime format */ - strftime_l(&sbuf[0], sizeof(sbuf), timeformat, &event_tms, - wc_locales[WC->selected_language]); - strftime_l(&ebuf[0], sizeof(sbuf), timeformat, &event_tme, - wc_locales[WC->selected_language]); + wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms); + wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme); wprintf("" "%s%s", @@ -231,6 +229,9 @@ void calendar_month_view(int year, int month, int day) { int i; time_t previous_month; time_t next_month; + time_t colheader_time; + struct tm colheader_tm; + char colheader_label[32]; /** Determine what day to start. * First, back up to the 1st of the month... @@ -272,11 +273,12 @@ void calendar_month_view(int year, int month, int day) { (int)(tm.tm_year)+1900, tm.tm_mon + 1); wprintf("\n"); + wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm); wprintf("  " "" "%s %d" "" - "  ", months[month-1], year); + "  ", colheader_label, year); localtime_r(&next_month, &tm); wprintf("", @@ -288,9 +290,13 @@ void calendar_month_view(int year, int month, int day) { /** Inner table (the real one) */ wprintf(""); + colheader_time = thetime; for (i=0; i<7; ++i) { + colheader_time = thetime + (i * 86400) ; + localtime_r(&colheader_time, &colheader_tm); + wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm); wprintf("\n"); @@ -309,7 +315,8 @@ void calendar_month_view(int year, int month, int day) { "FFFFFF")) ); if ((i==0) || (tm.tm_mday == 1)) { - wprintf("%s ", months[tm.tm_mon]); + wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm); + wprintf("%s ", colheader_label); } wprintf("" "%d
", @@ -349,6 +356,7 @@ void calendar_brief_month_view(int year, int month, int day) { int i; time_t previous_month; time_t next_month; + char month_label[32]; /** Determine what day to start. * First, back up to the 1st of the month... @@ -390,11 +398,12 @@ void calendar_brief_month_view(int year, int month, int day) { (int)(tm.tm_year)+1900, tm.tm_mon + 1); wprintf("\n"); + wc_strftime(month_label, sizeof month_label, "%B", &tm); wprintf("  " "" "%s %d" "" - "  ", months[month-1], year); + "  ", month_label, year); localtime_r(&next_month, &tm); wprintf("", @@ -412,13 +421,14 @@ void calendar_brief_month_view(int year, int month, int day) { /** Now do 35 days */ for (i = 0; i < 35; ++i) { char weeknumber[255]; + char weekday_name[32]; char *daycolor; localtime_r(&thetime, &tm); /** Before displaying Sunday, start a new CELL */ if ((i % 7) == 0) { - strftime_l(&weeknumber[0], sizeof(weeknumber), "%U", &tm, wc_locales[WC->selected_language]); + wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm); wprintf("
" - "%s", wdays[i]); + "%s", colheader_label); } wprintf("
" " \n", _("Week"), @@ -435,10 +445,12 @@ void calendar_brief_month_view(int year, int month, int day) { "FFFFFF")); /** Day Header */ - wprintf("\n", - daycolor, - wdays[i%7], - daycolor); + wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm); + wprintf("\n", + daycolor, + weekday_name, + daycolor); /** put the data of one day here, stupid */ calendar_month_view_brief_events(thetime, daycolor); @@ -675,7 +687,8 @@ void calendar_day_view(int year, int month, int day) { wprintf("%s
" "%d
" "%d
", - months[month-1], day, year); + monthname(month-1), + day, year); wprintf(""); /** Right arrow */ diff --git a/webcit/event.c b/webcit/event.c index 565ae727c..94fac68db 100644 --- a/webcit/event.c +++ b/webcit/event.c @@ -270,17 +270,14 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) * Determine who is the organizer of this event. * We need to determine "me" or "not me." */ - organizer = icalcomponent_get_first_property(vevent, - ICAL_ORGANIZER_PROPERTY); + organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY); if (organizer != NULL) { strcpy(organizer_string, icalproperty_get_organizer(organizer)); if (!strncasecmp(organizer_string, "MAILTO:", 7)) { strcpy(organizer_string, &organizer_string[7]); striplt(organizer_string); - lprintf(9, "ISME %s\n", organizer_string); serv_printf("ISME %s", organizer_string); serv_getln(buf, sizeof buf); - lprintf(9, "%s\n", buf); if (buf[0] == '2') { organizer_is_me = 1; } diff --git a/webcit/fmt_date.c b/webcit/fmt_date.c index cdc4f1803..b5681e8ee 100644 --- a/webcit/fmt_date.c +++ b/webcit/fmt_date.c @@ -10,12 +10,29 @@ #include "webserver.h" typedef unsigned char byte; /**< a byte. */ -char *wdays[7]; -char *months[12]; #define FALSE 0 /**< no. */ #define TRUE 1 /**< yes. */ +/** + * \brief Wrapper around strftime() or strftime_l() + * depending upon how our build is configured. + * + * \param s String target buffer + * \param max Maximum size of string target buffer + * \param format strftime() format + * \param tm Input date/time + */ +size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm) +{ +#ifdef ENABLE_NLS + return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]); +#else + return strftime(s, max, format, tm); +#endif +} + + /** * \brief Format a date/time stamp for output * \param buf the output buffer @@ -29,22 +46,6 @@ void fmt_date(char *buf, time_t thetime, int brief) time_t today_timet; int hour; char calhourformat[16]; - static char *ascmonths[12] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ; - - if (ascmonths[0] == NULL) { - ascmonths[0] = _("Jan"); - ascmonths[1] = _("Feb"); - ascmonths[2] = _("Mar"); - ascmonths[3] = _("Apr"); - ascmonths[4] = _("May"); - ascmonths[5] = _("Jun"); - ascmonths[6] = _("Jul"); - ascmonths[7] = _("Aug"); - ascmonths[8] = _("Sep"); - ascmonths[9] = _("Oct"); - ascmonths[10] = _("Nov"); - ascmonths[11] = _("Dec"); - }; get_preference("calhourformat", calhourformat, sizeof calhourformat); @@ -66,51 +67,49 @@ void fmt_date(char *buf, time_t thetime, int brief) if ((tm.tm_year == today_tm.tm_year) &&(tm.tm_mon == today_tm.tm_mon) &&(tm.tm_mday == today_tm.tm_mday)) { - if (!strcasecmp(calhourformat, "24")) { - sprintf(buf, "%2d:%02d", - tm.tm_hour, tm.tm_min - ); - } - else { - sprintf(buf, "%2d:%02d%s", - hour, tm.tm_min, - ((tm.tm_hour >= 12) ? "pm" : "am") - ); - } + wc_strftime(buf, 32, "%l:%M%p", &tm); } - /** Otherwise, for messages up to 6 months old, show the * month and day, and the time */ else if (today_timet - thetime < 15552000) { - if (!strcasecmp(calhourformat, "24")) { - sprintf(buf, "%s %d %2d:%02d", - ascmonths[tm.tm_mon], - tm.tm_mday, - tm.tm_hour, tm.tm_min - ); - } - else { - sprintf(buf, "%s %d %2d:%02d%s", - ascmonths[tm.tm_mon], - tm.tm_mday, - hour, tm.tm_min, - ((tm.tm_hour >= 12) ? "pm" : "am") - ); - } + wc_strftime(buf, 32, "%b %d %l:%M%p", &tm); } - /** older than 6 months, show only the date */ else { - sprintf(buf, "%s %d %d", - ascmonths[tm.tm_mon], - tm.tm_mday, - tm.tm_year + 1900 - ); + wc_strftime(buf, 32, "%b %d %Y", &tm); } } else { - strftime_l(buf, 32, "%c", &tm, wc_locales[WC->selected_language]); + wc_strftime(buf, 32, "%c", &tm); + } +} + +/** + * \brief Convenience function to return a month name + * + * \param m Numeric month + */ +char *monthname(int m) +{ + static char months[12][32]; + static int initialized = 0; + + time_t tt; + struct tm tm; + int i; + + if (!initialized) { + for (i=0; i<12; ++i) { + tt = 1137997451 + (i * 2592000); + localtime_r(&tt, &tm); + wc_strftime(months[i], 32, "%B", &tm); + lprintf(9, "%s\n", months[i]); + } } + initialized = 1; + + return months[m]; + } @@ -159,7 +158,7 @@ void fmt_time(char *buf, time_t thetime) * \param buf time to parse * \return the time found in buf */ -time_t httpdate_to_timestamp(const char *buf) +time_t httpdate_to_timestamp(char *buf) { time_t t = 0; struct tm tt; @@ -246,33 +245,5 @@ time_t httpdate_to_timestamp(const char *buf) -/** - * /brief Initialize the strings used to display months and weekdays. - */ -void initialize_months_and_days(void) { - wdays[0] = _("Sunday"); - wdays[1] = _("Monday"); - wdays[2] = _("Tuesday"); - wdays[3] = _("Wednesday"); - wdays[4] = _("Thursday"); - wdays[5] = _("Friday"); - wdays[6] = _("Saturday"); - - months[0] = _("January"); - months[1] = _("February"); - months[2] = _("March"); - months[3] = _("April"); - months[4] = _("May"); - months[5] = _("June"); - months[6] = _("July"); - months[7] = _("August"); - months[8] = _("September"); - months[9] = _("October"); - months[10] = _("November"); - months[11] = _("December"); -} - - - /*@}*/ diff --git a/webcit/summary.c b/webcit/summary.c index 0c309364f..b7b9084b5 100644 --- a/webcit/summary.c +++ b/webcit/summary.c @@ -19,7 +19,7 @@ void output_date(void) { time(&now); localtime_r(&now, &tm); - strftime_l(buf, 32, "%A, %x", &tm, wc_locales[WC->selected_language]); + wc_strftime(buf, 32, "%A, %x", &tm); wprintf("%s", buf); } diff --git a/webcit/webcit.h b/webcit/webcit.h index 0684163d2..5d89ebebc 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -550,10 +550,12 @@ void display_menubar(int); void smart_goto(char *); void worker_entry(void); void session_loop(struct httprequest *); +size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm); void fmt_date(char *buf, time_t thetime, int brief); void fmt_time(char *buf, time_t thetime); +char *monthname(int m); void httpdate(char *buf, time_t thetime); -time_t httpdate_to_timestamp(const char *buf); +time_t httpdate_to_timestamp(char *buf); void end_webcit_session(void); void page_popup(void); void chat_recv(void); @@ -720,12 +722,8 @@ void utf8ify_rfc822_string(char *buf); void begin_burst(void); void end_burst(void); -extern char *ascmonths[]; /**< Short (three letter) month names */ -extern char *months[]; /**< Long (full) month names */ extern char *hourname[]; /**< Names of hours (12am, 1am, etc.) */ -extern char *wdays[]; /**< Days of the week */ -void initialize_months_and_days(void); void http_datestring(char *buf, size_t n, time_t xtime); diff --git a/webcit/webserver.c b/webcit/webserver.c index 4824d5a95..40b52a6f6 100644 --- a/webcit/webserver.c +++ b/webcit/webserver.c @@ -577,7 +577,6 @@ int main(int argc, char **argv) initialize_viewdefs(); initialize_axdefs(); - initialize_months_and_days(); /** * Set up a place to put thread-specific data. -- 2.30.2
%s %s
%s%s%s%s
%s
%s" + "