Auch noch eine Mail, die steckengeblieben ist. ----- Forwarded message from alexanderkluth@xxxxxx ----- Date: Wed, 12 Aug 2009 05:23:04 +0200 From: alexanderkluth@xxxxxx To: tyndur-devel@xxxxxxxxxx Subject: [tyndur-devel] [PATCH] strftime() From: Alexander Hartmut Kluth <alexanderkluth@xxxxxx> + strftime() hinzugefügt + enum's und nen const-Array hinzugefügt, die das Leben leichter machen Signed-off-by: Alexander Hartmut Kluth <alexanderkluth@xxxxxx> --- src/modules/include/time.h | 2 + src/modules/lib/stdlibc/time.c | 233 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 235 insertions(+), 0 deletions(-) diff --git a/src/modules/include/time.h b/src/modules/include/time.h index 4ee4b2e..b6c928e 100644 --- a/src/modules/include/time.h +++ b/src/modules/include/time.h @@ -46,6 +46,8 @@ struct tm { time_t time(time_t* t); +size_t strftime(char *str, size_t maxs, const char *datestr, const struct tm *tm); + #ifndef CONFIG_LIBC_NO_STUBS /// Datum und Uhrzeit in einen String umwandeln char* ctime(const time_t* time_ptr); diff --git a/src/modules/lib/stdlibc/time.c b/src/modules/lib/stdlibc/time.c index 76e82d0..70b4902 100644 --- a/src/modules/lib/stdlibc/time.c +++ b/src/modules/lib/stdlibc/time.c @@ -46,6 +46,27 @@ static char* asctime_buf = "Wed Jun 42 13:37:42 1337\n"; static char* ctime_string = "Wed Jun 42 13:37:42 1337\n"; #endif +enum { + aJan, aFeb, aMar, aApr, aMay, aJun, aJul, aAug, aSep, aOct, aNov, aDec, + Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, + aSun, aMon, aTue, aWed, aThu, aFri, aSat, + Sun, Mon, Tue, Wed, Thu, Fri, Sat, + Local_time, Local_date, DFL_FMT, + AM, PM, DATE_FMT, + LAST +}; + +static const char * __time[] = { + "Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug", "Sep","Oct", "Nov", "Dec", + "January", "February", "March","April", + "May","June", "July", "August", "September", + "October", "November", "December", + "Sun","Mon", "Tue", "Wed","Thu", "Fri","Sat", + "Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday", + "%H:%M:%S","%m/%d/%y", "%a %b %d %H:%M:%S %Y", + "AM", "PM", "%a %b %e %T %Z %Y", NULL +}; + /** * Unix-Timestamp aus der aktuellen Zeit generieren. * @@ -128,3 +149,215 @@ time_t mktime(struct tm* time_ptr) #endif +/** + * Erzeugt einen String anhand eines Datumsformats aus einer bestimmten + * Anzahl Zeichen. + * + * TODO: %z für Zeitzone, %c und %C für lokalisierte Datums- und Zeitformat, + * %x und %X für lokalisiertes Datums-, bzw. Zeitformat + * + * @param str Pointer auf einen String + * @param maxs Maximal zu erzeugende Anzahl an Zeichen + * @param datestr Pointer auf den Datumsformatstring + * @param tm Struktur auf aktuelle Datums- und Zeitangaben + * + * @return 0 bei Misserfolg, ansonsten Anzahl erzeugter Zeichen + **/ +size_t strftime(char *str, size_t maxs, const char *datestr, const struct tm *tm) +{ + char i, c; + size_t size = 0, n; + char *nstr; + char dflcase[] = "%?"; + + // TODO: Aktuelle Zeit ermitteln + if (datestr == NULL) { + nstr = ""; + } + + // Datumsformatstring parsen + while ((c = *datestr++) != '\0') { + if (c != '%') { + if (++size >= maxs) { + return 0; + } + + *str++ = c; + continue; + } + + switch (*datestr++) { + case '%': + nstr = "%"; + break; + + case 'a': // Abgekürzter Wochentagsname + nstr = (char *)__time[aSun + tm->tm_wday]; + break; + + case 'A': // Wochentagsname + nstr = (char *)__time[Sun + tm->tm_wday]; + break; + + case 'b': // Abgekürzter Montatsname + case 'h': + nstr = (char *)__time[aJan + tm->tm_mon]; + break; + + + case 'B': // Monatsname + nstr = (char *)__time[Jan + tm->tm_mon]; + + if ((n = strftime(str, maxs-size, nstr, tm)) == 0) { + return 0; + } + + str += n; + size += n; + break; + + case 'd': // Tagesnummer + //p = itoa(tm->tm_mday, nstr, 2); + break; + + case 'D': + nstr = "%m/%d/%y"; + if ((n = strftime(str, maxs-size, nstr, tm)) == 0) { + return 0; + } + + str += n; + size += n; + break; + + case 'e': + itoa(tm->tm_mday, nstr, 2); + if (tm->tm_mday < 10) { + nstr[0] = ' '; + } + + break; + + case 'H': // 24-Stunden + itoa(tm->tm_hour, nstr, 2); + break; + + case 'I': // 12-Stunden + if ((i = tm->tm_hour % 12) == 0) + i = 12; + itoa(i, nstr, 2); + break; + + case 'j': // Julianisches Datum + itoa(tm->tm_yday + 1, nstr, 3); + break; + + case 'm': // Monats-Nummer + itoa(tm->tm_mon + 1, nstr, 2); + break; + + case 'M': // Minute + itoa(tm->tm_min, nstr, 2); + break; + + case 'n': // Newline + nstr = "\n"; + break; + + case 'p': // AM oder PM + if (tm->tm_hour >= 12) + nstr = (char *)__time[PM]; + else + nstr = (char *)__time[AM]; + break; + + case 'r': + if (tm->tm_hour >= 12) + nstr = "%I:%M:%S PM"; + else + nstr = "%I:%M:%S AM"; + + if ((n = strftime(str, maxs-size, nstr, tm)) == 0) { + return 0; + } + + str += n; + size += n; + break; + + case 'R': + nstr = "%H:%M"; + if ((n = strftime(str, maxs-size, nstr, tm)) == 0) { + return 0; + } + + str += n; + size += n; + break; + + case 'S': // Sekunden + //p = itoa(tm->tm_sec, nstr, 2); + break; + + case 't': // Tabulator + nstr = "\t"; + break; + + case 'T': + nstr = "%H:%M:%S"; + if ((n = strftime(str, maxs-size, nstr, tm)) == 0) { + return 0; + } + + str += n; + size += n; + break; + + case 'U': // Tagesnummer des Jahres, bei Sonntag als 1. Tag + itoa((tm->tm_yday - tm->tm_wday + 7)/7, nstr, 2); + break; + + case 'w': // Wochentagsnummer + itoa(tm->tm_wday, nstr, 1); + break; + + case 'W': // Tagesnummer des Jahres, bei Montag als 1. Tag + if ((i = 8 - tm->tm_wday) == 8) { + i = 1; + } + itoa((tm->tm_yday + i)/7, nstr, 2); + break; + + + case 'y': // Jahr in Form von "xx" + itoa(tm->tm_year, nstr, 2); + break; + + case 'Y': // Jahr in Form von "xxxx" + itoa(1900 + tm->tm_year, nstr, 4); + break; + + default: + dflcase[1] = *(datestr - 1); + nstr = (char *)dflcase; + break; + + continue; + } + + n = strlen(nstr); + + if ((size += n) >= maxs) { + return 0; + } + + strcpy(str, nstr); + str += n; + } + + *str = '\0'; + return size; +} + + + -- 1.6.0.4 ----- End forwarded message ----- -- Antoine Kaufmann <toni@xxxxxxxxxxxxxxxx>
Attachment:
pgpzEx5J_OOce.pgp
Description: PGP signature