/* Copyright (C) 2008 Emmanuel Varoquaux This file is part of XOS. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "time.h" #include struct output_struct { char *s; size_t max; }; static const char *abbr_weekday_name[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; static const char *full_weekday_name[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; static const char *abbr_month_name[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; static const char *full_month_name[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; static void output_init(struct output_struct *output, char *s, size_t max) { output->s = s; output->max = max; } static int output_add(struct output_struct *output, char c) { if (!output->max) return 0; *output->s++ = c; output->max--; return 1; } static int output_adds(struct output_struct *output, const char *s) { while (*s) { if (!output->max) return 0; *output->s++ = *s; output->max--; s++; } return 1; } size_t __attribute__ ((weak, visibility ("default"))) strftime(char *s, size_t max, const char *format, const struct tm *tm) { struct output_struct output; int state; char modifier; char c; char buf[256]; __tzset(); output_init(&output, s, max); state = 0; modifier = 0; do { c = *format++; switch (state) { case 0: if (c == '%') { state = 1; modifier = 0; continue; } if (!output_add(&output, c)) return 0; continue; case 1: if (c == 'E' || c == '0') { modifier = c; state = 2; continue; } case 2: switch (c) { case 'a': if (!output_adds(&output, abbr_weekday_name[tm->tm_wday])) return 0; break; case 'A': if (!output_adds(&output, full_weekday_name[tm->tm_wday])) return 0; break; case 'b': if (!output_adds(&output, abbr_month_name[tm->tm_mon])) return 0; break; case 'B': if (!output_adds(&output, full_month_name[tm->tm_mon])) return 0; break; case 'c': sprintf(buf, "%s %s %2d %02d:%02d:%02d %d", abbr_weekday_name[tm->tm_wday], abbr_month_name[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, 1900 + tm->tm_year); if (!output_adds(&output, buf)) return 0; break; case 'C': sprintf(buf, "%02d", (1900 + tm->tm_year) / 100); if (!output_adds(&output, buf)) return 0; break; case 'd': sprintf(buf, "%02d", tm->tm_mday); if (!output_adds(&output, buf)) return 0; break; case 'D': sprintf(buf, "%02d/%02d/%02d", 1 + tm->tm_mon, tm->tm_mday, tm->tm_year % 100); if (!output_adds(&output, buf)) return 0; break; case 'e': sprintf(buf, "%2d", tm->tm_mday); if (!output_adds(&output, buf)) return 0; break; case 'F': sprintf(buf, "%d-%02d-%02d", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday); if (!output_adds(&output, buf)) return 0; break; case 'h': if (!output_adds(&output, abbr_month_name[tm->tm_mon])) return 0; break; case 'H': sprintf(buf, "%02d", tm->tm_hour); if (!output_adds(&output, buf)) return 0; break; case 'I': sprintf(buf, "%02d", ((tm->tm_hour + 11) % 12) + 1); if (!output_adds(&output, buf)) return 0; break; case 'j': sprintf(buf, "%03d", 1 + tm->tm_yday); if (!output_adds(&output, buf)) return 0; break; case 'm': sprintf(buf, "%02d", 1 + tm->tm_mon); if (!output_adds(&output, buf)) return 0; break; case 'M': sprintf(buf, "%02d", tm->tm_min); if (!output_adds(&output, buf)) return 0; break; case 'n': if (!output_add(&output, '\n')) return 0; break; case 'p': if (!output_adds(&output, tm->tm_hour < 12 ? "AM" : "PM")) return 0; break; case 'r': sprintf(buf, "%02d:%02d:%02d %s", ((tm->tm_hour + 11) % 12) + 1, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "AM" : "PM"); if (!output_adds(&output, buf)) return 0; break; case 'R': sprintf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min); if (!output_adds(&output, buf)) return 0; break; case 'S': sprintf(buf, "%02d", tm->tm_sec); if (!output_adds(&output, buf)) return 0; break; case 't': if (!output_add(&output, '\t')) return 0; break; case 'T': sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); if (!output_adds(&output, buf)) return 0; break; case 'u': sprintf(buf, "%d", (tm->tm_wday + 6) % 7 + 1); if (!output_adds(&output, buf)) return 0; break; case 'w': sprintf(buf, "%d", tm->tm_wday); if (!output_adds(&output, buf)) return 0; break; case 'x': sprintf(buf, "%02d/%02d/%02d", 1 + tm->tm_mon, tm->tm_mday, tm->tm_year % 100); if (!output_adds(&output, buf)) return 0; break; case 'X': sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); if (!output_adds(&output, buf)) return 0; break; case 'y': sprintf(buf, "%02d", tm->tm_year % 100); if (!output_adds(&output, buf)) return 0; break; case 'Y': sprintf(buf, "%d", 1900 + tm->tm_year); if (!output_adds(&output, buf)) return 0; break; case 'z': if (tm->tm_isdst >= 0) { sprintf(buf, "%+.4ld", -(tm->tm_isdst ? __current_tz->dst->offset : __current_tz->offset) / 36); if (!output_adds(&output, buf)) return 0; } break; case 'Z': if (tm->tm_isdst >= 0) if (!output_adds(&output, tm->tm_isdst ? __current_tz->dst->name : __current_tz->name)) return 0; break; case '%': if (!output_add(&output, '%')) return 0; break; default: if (!output_add(&output, '%')) return 0; if (modifier) if (!output_add(&output, modifier)) return 0; if (!output_add(&output, c)) return 0; } state = 0; continue; } } while (c); return max - output.max - 1; }