00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include "ruby/ruby.h"
00051 #include "timev.h"
00052
00053 #ifndef GAWK
00054 #include <stdio.h>
00055 #include <ctype.h>
00056 #include <string.h>
00057 #include <time.h>
00058 #include <sys/types.h>
00059 #include <errno.h>
00060 #endif
00061 #if defined(TM_IN_SYS_TIME) || !defined(GAWK)
00062 #include <sys/types.h>
00063 #if HAVE_SYS_TIME_H
00064 #include <sys/time.h>
00065 #endif
00066 #endif
00067 #include <math.h>
00068
00069
00070 #define SYSV_EXT 1
00071 #define SUNOS_EXT 1
00072 #define POSIX2_DATE 1
00073 #define VMS_EXT 1
00074 #define MAILHEADER_EXT 1
00075 #define ISO_DATE_EXT 1
00076 #ifndef GAWK
00077 #define POSIX_SEMANTICS 1
00078 #endif
00079
00080 #if defined(ISO_DATE_EXT)
00081 #if ! defined(POSIX2_DATE)
00082 #define POSIX2_DATE 1
00083 #endif
00084 #endif
00085
00086 #if defined(POSIX2_DATE)
00087 #if ! defined(SYSV_EXT)
00088 #define SYSV_EXT 1
00089 #endif
00090 #if ! defined(SUNOS_EXT)
00091 #define SUNOS_EXT 1
00092 #endif
00093 #endif
00094
00095 #if defined(POSIX2_DATE)
00096 #define adddecl(stuff) stuff
00097 #else
00098 #define adddecl(stuff)
00099 #endif
00100
00101 #undef strchr
00102
00103 #if !defined __STDC__ && !defined _WIN32
00104 #define const
00105 static int weeknumber();
00106 adddecl(static int iso8601wknum();)
00107 static int weeknumber_v();
00108 adddecl(static int iso8601wknum_v();)
00109 #else
00110 static int weeknumber(const struct tm *timeptr, int firstweekday);
00111 adddecl(static int iso8601wknum(const struct tm *timeptr);)
00112 static int weeknumber_v(const struct vtm *vtm, int firstweekday);
00113 adddecl(static int iso8601wknum_v(const struct vtm *vtm);)
00114 #endif
00115
00116 #ifdef STDC_HEADERS
00117 #include <stdlib.h>
00118 #include <string.h>
00119 #else
00120 extern void *malloc();
00121 extern void *realloc();
00122 extern char *getenv();
00123 extern char *strchr();
00124 #endif
00125
00126 #define range(low, item, hi) max(low, min(item, hi))
00127
00128 #if defined __WIN32__ || defined _WIN32
00129 #define DLL_IMPORT __declspec(dllimport)
00130 #endif
00131 #ifndef DLL_IMPORT
00132 #define DLL_IMPORT
00133 #endif
00134 #if !defined(OS2) && defined(HAVE_TZNAME)
00135 extern DLL_IMPORT char *tzname[2];
00136 #ifdef HAVE_DAYLIGHT
00137 extern DLL_IMPORT int daylight;
00138 #endif
00139 #ifdef HAVE_VAR_TIMEZONE
00140 extern DLL_IMPORT TYPEOF_VAR_TIMEZONE timezone;
00141 #endif
00142 #ifdef HAVE_VAR_ALTZONE
00143 extern DLL_IMPORT TYPEOF_VAR_ALTZONE altzone;
00144 #endif
00145 #endif
00146
00147 #undef min
00148
00149
00150
00151 #ifndef __STDC__
00152 static inline int
00153 min(a, b)
00154 int a, b;
00155 #else
00156 static inline int
00157 min(int a, int b)
00158 #endif
00159 {
00160 return (a < b ? a : b);
00161 }
00162
00163 #undef max
00164
00165
00166
00167 #ifndef __STDC__
00168 static inline int
00169 max(a, b)
00170 int a, b;
00171 #else
00172 static inline int
00173 max(int a, int b)
00174 #endif
00175 {
00176 return (a > b ? a : b);
00177 }
00178
00179 #ifdef NO_STRING_LITERAL_CONCATENATION
00180 #error No string literal concatenation
00181 #endif
00182
00183 #define add(x,y) (rb_funcall((x), '+', 1, (y)))
00184 #define sub(x,y) (rb_funcall((x), '-', 1, (y)))
00185 #define mul(x,y) (rb_funcall((x), '*', 1, (y)))
00186 #define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
00187 #define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
00188 #define mod(x,y) (rb_funcall((x), '%', 1, (y)))
00189
00190
00191
00192 static size_t
00193 rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const struct vtm *vtm, VALUE timev, struct timespec *ts, int gmt)
00194 {
00195 char *endp = s + maxsize;
00196 char *start = s;
00197 const char *sp, *tp;
00198 auto char tbuf[100];
00199 long off;
00200 ptrdiff_t i;
00201 int w;
00202 long y;
00203 static short first = 1;
00204 #ifdef POSIX_SEMANTICS
00205 static char *savetz = NULL;
00206 static size_t savetzlen = 0;
00207 char *tz;
00208 #endif
00209 #ifndef HAVE_TM_ZONE
00210 #ifndef HAVE_TM_NAME
00211 #if ((defined(MAILHEADER_EXT) && !HAVE_VAR_TIMEZONE && HAVE_GETTIMEOFDAY) || \
00212 (!HAVE_TZNAME && HAVE_TIMEZONE))
00213 struct timeval tv;
00214 struct timezone zone;
00215 #endif
00216 #endif
00217 #endif
00218 int precision, flags;
00219 char padding;
00220 enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E};
00221 #define BIT_OF(n) (1U<<(n))
00222
00223
00224 static const char days_l[][10] = {
00225 "Sunday", "Monday", "Tuesday", "Wednesday",
00226 "Thursday", "Friday", "Saturday",
00227 };
00228 static const char months_l[][10] = {
00229 "January", "February", "March", "April",
00230 "May", "June", "July", "August", "September",
00231 "October", "November", "December",
00232 };
00233 static const char ampm[][3] = { "AM", "PM", };
00234
00235 if (s == NULL || format == NULL || vtm == NULL || maxsize == 0)
00236 return 0;
00237
00238
00239 if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) {
00240 err:
00241 errno = ERANGE;
00242 return 0;
00243 }
00244
00245 #ifndef POSIX_SEMANTICS
00246 if (first) {
00247 tzset();
00248 first = 0;
00249 }
00250 #else
00251 tz = getenv("TZ");
00252 if (first) {
00253 if (tz != NULL) {
00254 size_t tzlen = strlen(tz);
00255
00256 savetz = (char *) malloc(tzlen + 1);
00257 if (savetz != NULL) {
00258 savetzlen = tzlen + 1;
00259 memcpy(savetz, tz, savetzlen);
00260 }
00261 }
00262 tzset();
00263 first = 0;
00264 }
00265
00266 if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
00267 size_t i = strlen(tz) + 1;
00268 if (i > savetzlen) {
00269 savetz = (char *) realloc(savetz, i);
00270 if (savetz) {
00271 savetzlen = i;
00272 memcpy(savetz, tz, i);
00273 }
00274 } else
00275 memcpy(savetz, tz, i);
00276 tzset();
00277 }
00278 #endif
00279
00280 for (; *format && s < endp - 1; format++) {
00281 #define FLAG_FOUND() do { \
00282 if (precision > 0 || flags & (BIT_OF(LOCALE_E)|BIT_OF(LOCALE_O))) \
00283 goto unknown; \
00284 } while (0)
00285 #define NEEDS(n) do if (s + (n) >= endp - 1) goto err; while (0)
00286 #define FILL_PADDING(i) do { \
00287 if (!(flags & BIT_OF(LEFT)) && precision > i) { \
00288 NEEDS(precision); \
00289 memset(s, padding ? padding : ' ', precision - i); \
00290 s += precision - i; \
00291 } \
00292 else { \
00293 NEEDS(i); \
00294 } \
00295 } while (0);
00296 #define FMT(def_pad, def_prec, fmt, val) \
00297 do { \
00298 int l; \
00299 if (precision <= 0) precision = (def_prec); \
00300 if (flags & BIT_OF(LEFT)) precision = 1; \
00301 l = snprintf(s, endp - s, \
00302 ((padding == '0' || (!padding && def_pad == '0')) ? "%0*"fmt : "%*"fmt), \
00303 precision, val); \
00304 if (l < 0) goto err; \
00305 s += l; \
00306 } while (0)
00307 #define STRFTIME(fmt) \
00308 do { \
00309 i = rb_strftime_with_timespec(s, endp - s, fmt, vtm, timev, ts, gmt); \
00310 if (!i) return 0; \
00311 if (precision > i) {\
00312 memmove(s + precision - i, s, i);\
00313 memset(s, padding ? padding : ' ', precision - i); \
00314 s += precision; \
00315 }\
00316 else s += i; \
00317 } while (0)
00318 #define FMTV(def_pad, def_prec, fmt, val) \
00319 do { \
00320 VALUE tmp = (val); \
00321 if (FIXNUM_P(tmp)) { \
00322 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
00323 } \
00324 else { \
00325 VALUE args[2], result; \
00326 size_t l; \
00327 if (precision <= 0) precision = (def_prec); \
00328 if (flags & BIT_OF(LEFT)) precision = 1; \
00329 args[0] = INT2FIX(precision); \
00330 args[1] = val; \
00331 if (padding == '0' || (!padding && def_pad == '0')) \
00332 result = rb_str_format(2, args, rb_str_new2("%0*"fmt)); \
00333 else \
00334 result = rb_str_format(2, args, rb_str_new2("%*"fmt)); \
00335 l = strlcpy(s, StringValueCStr(result), endp-s); \
00336 if ((size_t)(endp-s) <= l) \
00337 goto err; \
00338 s += l; \
00339 } \
00340 } while (0)
00341
00342 if (*format != '%') {
00343 *s++ = *format;
00344 continue;
00345 }
00346 tp = tbuf;
00347 sp = format;
00348 precision = -1;
00349 flags = 0;
00350 padding = 0;
00351 again:
00352 switch (*++format) {
00353 case '\0':
00354 format--;
00355 goto unknown;
00356
00357 case '%':
00358 FILL_PADDING(1);
00359 *s++ = '%';
00360 continue;
00361
00362 case 'a':
00363 if (flags & BIT_OF(CHCASE)) {
00364 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
00365 flags |= BIT_OF(UPPER);
00366 }
00367 if (vtm->wday < 0 || vtm->wday > 6)
00368 i = 1, tp = "?";
00369 else
00370 i = 3, tp = days_l[vtm->wday];
00371 break;
00372
00373 case 'A':
00374 if (flags & BIT_OF(CHCASE)) {
00375 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
00376 flags |= BIT_OF(UPPER);
00377 }
00378 if (vtm->wday < 0 || vtm->wday > 6)
00379 i = 1, tp = "?";
00380 else
00381 i = strlen(tp = days_l[vtm->wday]);
00382 break;
00383
00384 #ifdef SYSV_EXT
00385 case 'h':
00386 #endif
00387 case 'b':
00388 if (flags & BIT_OF(CHCASE)) {
00389 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
00390 flags |= BIT_OF(UPPER);
00391 }
00392 if (vtm->mon < 1 || vtm->mon > 12)
00393 i = 1, tp = "?";
00394 else
00395 i = 3, tp = months_l[vtm->mon-1];
00396 break;
00397
00398 case 'B':
00399 if (flags & BIT_OF(CHCASE)) {
00400 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
00401 flags |= BIT_OF(UPPER);
00402 }
00403 if (vtm->mon < 1 || vtm->mon > 12)
00404 i = 1, tp = "?";
00405 else
00406 i = strlen(tp = months_l[vtm->mon-1]);
00407 break;
00408
00409 case 'c':
00410 STRFTIME("%a %b %e %H:%M:%S %Y");
00411 continue;
00412
00413 case 'd':
00414 i = range(1, vtm->mday, 31);
00415 FMT('0', 2, "d", (int)i);
00416 continue;
00417
00418 case 'H':
00419 i = range(0, vtm->hour, 23);
00420 FMT('0', 2, "d", (int)i);
00421 continue;
00422
00423 case 'I':
00424 i = range(0, vtm->hour, 23);
00425 if (i == 0)
00426 i = 12;
00427 else if (i > 12)
00428 i -= 12;
00429 FMT('0', 2, "d", (int)i);
00430 continue;
00431
00432 case 'j':
00433 FMT('0', 3, "d", vtm->yday);
00434 continue;
00435
00436 case 'm':
00437 i = range(1, vtm->mon, 12);
00438 FMT('0', 2, "d", (int)i);
00439 continue;
00440
00441 case 'M':
00442 i = range(0, vtm->min, 59);
00443 FMT('0', 2, "d", (int)i);
00444 continue;
00445
00446 case 'p':
00447 case 'P':
00448 if ((*format == 'p' && (flags & BIT_OF(CHCASE))) ||
00449 (*format == 'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
00450 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
00451 flags |= BIT_OF(LOWER);
00452 }
00453 i = range(0, vtm->hour, 23);
00454 if (i < 12)
00455 tp = ampm[0];
00456 else
00457 tp = ampm[1];
00458 i = 2;
00459 break;
00460
00461 case 's':
00462 if (ts) {
00463 time_t sec = ts->tv_sec;
00464 if (~(time_t)0 <= 0)
00465 FMT('0', 1, PRI_TIMET_PREFIX"d", sec);
00466 else
00467 FMT('0', 1, PRI_TIMET_PREFIX"u", sec);
00468 }
00469 else {
00470 VALUE sec = div(timev, INT2FIX(1));
00471 FMTV('0', 1, "d", sec);
00472 }
00473 continue;
00474
00475 case 'S':
00476 i = range(0, vtm->sec, 60);
00477 FMT('0', 2, "d", (int)i);
00478 continue;
00479
00480 case 'U':
00481 FMT('0', 2, "d", weeknumber_v(vtm, 0));
00482 continue;
00483
00484 case 'w':
00485 i = range(0, vtm->wday, 6);
00486 FMT('0', 1, "d", (int)i);
00487 continue;
00488
00489 case 'W':
00490 FMT('0', 2, "d", weeknumber_v(vtm, 1));
00491 continue;
00492
00493 case 'x':
00494 STRFTIME("%m/%d/%y");
00495 continue;
00496
00497 case 'X':
00498 STRFTIME("%H:%M:%S");
00499 continue;
00500
00501 case 'y':
00502 i = NUM2INT(mod(vtm->year, INT2FIX(100)));
00503 FMT('0', 2, "d", (int)i);
00504 continue;
00505
00506 case 'Y':
00507 if (FIXNUM_P(vtm->year)) {
00508 long y = FIX2LONG(vtm->year);
00509 FMT('0', 0 <= y ? 4 : 5, "ld", y);
00510 }
00511 else {
00512 FMTV('0', 4, "d", vtm->year);
00513 }
00514 continue;
00515
00516 #ifdef MAILHEADER_EXT
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532 case 'z':
00533 if (precision < 4) precision = 4;
00534 NEEDS(precision + 1);
00535 if (gmt) {
00536 off = 0;
00537 }
00538 else {
00539 off = NUM2LONG(rb_funcall(quo(vtm->utc_offset, INT2FIX(60)), rb_intern("round"), 0));
00540 #if 0
00541 #ifdef HAVE_TM_NAME
00542
00543
00544
00545
00546 off = -timeptr->tm_tzadj / 60;
00547 #else
00548 #ifdef HAVE_TM_ZONE
00549
00550
00551
00552
00553 off = timeptr->tm_gmtoff / 60;
00554 #else
00555 #if HAVE_VAR_TIMEZONE
00556 #if HAVE_VAR_ALTZONE
00557 off = -(daylight ? timezone : altzone) / 60;
00558 #else
00559 off = -timezone / 60;
00560 #endif
00561 #else
00562 #ifdef HAVE_GETTIMEOFDAY
00563 gettimeofday(&tv, &zone);
00564 off = -zone.tz_minuteswest;
00565 #else
00566
00567 {
00568 struct tm utc;
00569 time_t now;
00570 time(&now);
00571 utc = *gmtime(&now);
00572 off = (long)((now - mktime(&utc)) / 60);
00573 }
00574 #endif
00575 #endif
00576 #endif
00577 #endif
00578 #endif
00579 }
00580 if (off < 0) {
00581 off = -off;
00582 *s++ = '-';
00583 } else {
00584 *s++ = '+';
00585 }
00586 off = off/60*100 + off%60;
00587 i = snprintf(s, endp - s, (padding == ' ' ? "%*ld" : "%.*ld"),
00588 precision - (precision > 4), off);
00589 if (i < 0) goto err;
00590 s += i;
00591 continue;
00592 #endif
00593
00594 case 'Z':
00595 if (flags & BIT_OF(CHCASE)) {
00596 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
00597 flags |= BIT_OF(LOWER);
00598 }
00599 if (gmt) {
00600 i = 3;
00601 tp = "UTC";
00602 break;
00603 }
00604 #if 0
00605 #ifdef HAVE_TZNAME
00606 i = (daylight && timeptr->tm_isdst > 0);
00607 tp = tzname[i];
00608 #else
00609 #ifdef HAVE_TM_ZONE
00610 tp = timeptr->tm_zone;
00611 #else
00612 #ifdef HAVE_TM_NAME
00613 tp = timeptr->tm_name;
00614 #else
00615 #ifdef HAVE_TIMEZONE
00616 gettimeofday(& tv, & zone);
00617 #ifdef TIMEZONE_VOID
00618 tp = timezone();
00619 #else
00620 tp = timezone(zone.tz_minuteswest, timeptr->tm_isdst > 0);
00621 #endif
00622 #endif
00623 #endif
00624 #endif
00625 #endif
00626 #endif
00627 if (vtm->zone == NULL)
00628 tp = "";
00629 else
00630 tp = vtm->zone;
00631 i = strlen(tp);
00632 break;
00633
00634 #ifdef SYSV_EXT
00635 case 'n':
00636 FILL_PADDING(1);
00637 *s++ = '\n';
00638 continue;
00639
00640 case 't':
00641 FILL_PADDING(1);
00642 *s++ = '\t';
00643 continue;
00644
00645 case 'D':
00646 STRFTIME("%m/%d/%y");
00647 continue;
00648
00649 case 'e':
00650 FMT(' ', 2, "d", range(1, vtm->mday, 31));
00651 continue;
00652
00653 case 'r':
00654 STRFTIME("%I:%M:%S %p");
00655 continue;
00656
00657 case 'R':
00658 STRFTIME("%H:%M");
00659 continue;
00660
00661 case 'T':
00662 STRFTIME("%H:%M:%S");
00663 continue;
00664 #endif
00665
00666 #ifdef SUNOS_EXT
00667 case 'k':
00668 i = range(0, vtm->hour, 23);
00669 FMT(' ', 2, "d", (int)i);
00670 continue;
00671
00672 case 'l':
00673 i = range(0, vtm->hour, 23);
00674 if (i == 0)
00675 i = 12;
00676 else if (i > 12)
00677 i -= 12;
00678 FMT(' ', 2, "d", (int)i);
00679 continue;
00680 #endif
00681
00682
00683 #ifdef VMS_EXT
00684 case 'v':
00685 STRFTIME("%e-%^b-%4Y");
00686 continue;
00687 #endif
00688
00689
00690 #ifdef POSIX2_DATE
00691 case 'C':
00692 FMTV('0', 2, "d", div(vtm->year, INT2FIX(100)));
00693 continue;
00694
00695 case 'E':
00696
00697 flags |= BIT_OF(LOCALE_E);
00698 goto again;
00699 case 'O':
00700
00701 flags |= BIT_OF(LOCALE_O);
00702 goto again;
00703
00704 case 'V':
00705 FMT('0', 2, "d", iso8601wknum_v(vtm));
00706 continue;
00707
00708 case 'u':
00709
00710 FMT('0', 1, "d", vtm->wday == 0 ? 7 : vtm->wday);
00711 continue;
00712 #endif
00713
00714 #ifdef ISO_DATE_EXT
00715 case 'G':
00716 case 'g':
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726 {
00727 VALUE yv = vtm->year;
00728 w = iso8601wknum_v(vtm);
00729 if (vtm->mon == 12 && w == 1)
00730 yv = add(yv, INT2FIX(1));
00731 else if (vtm->mon == 1 && w >= 52)
00732 yv = sub(yv, INT2FIX(1));
00733
00734 if (*format == 'G') {
00735 FMTV('0', 1, "d", yv);
00736 }
00737 else {
00738 yv = mod(yv, INT2FIX(100));
00739 y = FIX2LONG(yv);
00740 FMT('0', 2, "ld", y);
00741 }
00742 continue;
00743 }
00744
00745 #endif
00746
00747
00748 case 'L':
00749 w = 3;
00750 goto subsec;
00751
00752 case 'N':
00753
00754
00755
00756
00757
00758
00759
00760
00761 w = 9;
00762 subsec:
00763 if (precision <= 0) {
00764 precision = w;
00765 }
00766 NEEDS(precision);
00767
00768 if (ts) {
00769 long subsec = ts->tv_nsec;
00770 if (9 < precision) {
00771 snprintf(s, endp - s, "%09ld", subsec);
00772 memset(s+9, '0', precision-9);
00773 s += precision;
00774 }
00775 else {
00776 int i;
00777 for (i = 0; i < 9-precision; i++)
00778 subsec /= 10;
00779 snprintf(s, endp - s, "%0*ld", precision, subsec);
00780 s += precision;
00781 }
00782 }
00783 else {
00784 VALUE subsec = mod(timev, INT2FIX(1));
00785 int ww;
00786 long n;
00787
00788 ww = precision;
00789 while (9 <= ww) {
00790 subsec = mul(subsec, INT2FIX(1000000000));
00791 ww -= 9;
00792 }
00793 n = 1;
00794 for (; 0 < ww; ww--)
00795 n *= 10;
00796 if (n != 1)
00797 subsec = mul(subsec, INT2FIX(n));
00798 subsec = div(subsec, INT2FIX(1));
00799
00800 if (FIXNUM_P(subsec)) {
00801 int l;
00802 l = snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec));
00803 s += precision;
00804 }
00805 else {
00806 VALUE args[2], result;
00807 size_t l;
00808 args[0] = INT2FIX(precision);
00809 args[1] = subsec;
00810 result = rb_str_format(2, args, rb_str_new2("%0*d"));
00811 l = strlcpy(s, StringValueCStr(result), endp-s);
00812 s += precision;
00813 }
00814 }
00815 continue;
00816
00817 case 'F':
00818 STRFTIME("%Y-%m-%d");
00819 continue;
00820
00821 case '-':
00822 FLAG_FOUND();
00823 flags |= BIT_OF(LEFT);
00824 padding = precision = 0;
00825 goto again;
00826
00827 case '^':
00828 FLAG_FOUND();
00829 flags |= BIT_OF(UPPER);
00830 goto again;
00831
00832 case '#':
00833 FLAG_FOUND();
00834 flags |= BIT_OF(CHCASE);
00835 goto again;
00836
00837 case '_':
00838 FLAG_FOUND();
00839 padding = ' ';
00840 goto again;
00841
00842 case '0':
00843 padding = '0';
00844 case '1': case '2': case '3': case '4':
00845 case '5': case '6': case '7': case '8': case '9':
00846 {
00847 char *e;
00848 precision = (int)strtoul(format, &e, 10);
00849 format = e - 1;
00850 goto again;
00851 }
00852
00853 default:
00854 unknown:
00855 i = format - sp + 1;
00856 tp = sp;
00857 precision = -1;
00858 flags = 0;
00859 padding = 0;
00860 break;
00861 }
00862 if (i) {
00863 FILL_PADDING(i);
00864 memcpy(s, tp, i);
00865 switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
00866 case BIT_OF(UPPER):
00867 do {
00868 if (ISLOWER(*s)) *s = TOUPPER(*s);
00869 } while (s++, --i);
00870 break;
00871 case BIT_OF(LOWER):
00872 do {
00873 if (ISUPPER(*s)) *s = TOLOWER(*s);
00874 } while (s++, --i);
00875 break;
00876 default:
00877 s += i;
00878 break;
00879 }
00880 }
00881 }
00882 if (s >= endp) {
00883 goto err;
00884 }
00885 if (*format == '\0') {
00886 *s = '\0';
00887 return (s - start);
00888 } else
00889 return 0;
00890 }
00891
00892 size_t
00893 rb_strftime(char *s, size_t maxsize, const char *format, const struct vtm *vtm, VALUE timev, int gmt)
00894 {
00895 return rb_strftime_with_timespec(s, maxsize, format, vtm, timev, NULL, gmt);
00896 }
00897
00898 size_t
00899 rb_strftime_timespec(char *s, size_t maxsize, const char *format, const struct vtm *vtm, struct timespec *ts, int gmt)
00900 {
00901 return rb_strftime_with_timespec(s, maxsize, format, vtm, Qnil, ts, gmt);
00902 }
00903
00904
00905
00906 #ifndef __STDC__
00907 static int
00908 isleap(year)
00909 long year;
00910 #else
00911 static int
00912 isleap(long year)
00913 #endif
00914 {
00915 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
00916 }
00917
00918
00919 static void
00920 vtm2tm_noyear(const struct vtm *vtm, struct tm *result)
00921 {
00922 struct tm tm;
00923
00924
00925 tm.tm_year = FIX2INT(mod(vtm->year, INT2FIX(400))) + 100;
00926
00927 tm.tm_mon = vtm->mon-1;
00928 tm.tm_mday = vtm->mday;
00929 tm.tm_hour = vtm->hour;
00930 tm.tm_min = vtm->min;
00931 tm.tm_sec = vtm->sec;
00932 tm.tm_wday = vtm->wday;
00933 tm.tm_yday = vtm->yday-1;
00934 tm.tm_isdst = vtm->isdst;
00935 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
00936 tm.tm_gmtoff = NUM2LONG(vtm->utc_offset);
00937 #endif
00938 #if defined(HAVE_TM_ZONE)
00939 tm.tm_zone = (char *)vtm->zone;
00940 #endif
00941 *result = tm;
00942 }
00943
00944 #ifdef POSIX2_DATE
00945
00946
00947 #ifndef __STDC__
00948 static int
00949 iso8601wknum(timeptr)
00950 const struct tm *timeptr;
00951 #else
00952 static int
00953 iso8601wknum(const struct tm *timeptr)
00954 #endif
00955 {
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970 int weeknum, jan1day;
00971
00972
00973 weeknum = weeknumber(timeptr, 1);
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
00989 if (jan1day < 0)
00990 jan1day += 7;
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 switch (jan1day) {
01006 case 1:
01007 break;
01008 case 2:
01009 case 3:
01010 case 4:
01011 weeknum++;
01012 break;
01013 case 5:
01014 case 6:
01015 case 0:
01016 if (weeknum == 0) {
01017 #ifdef USE_BROKEN_XPG4
01018
01019 weeknum = 53;
01020 #else
01021
01022 struct tm dec31ly;
01023 dec31ly = *timeptr;
01024 dec31ly.tm_year--;
01025 dec31ly.tm_mon = 11;
01026 dec31ly.tm_mday = 31;
01027 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
01028 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
01029 weeknum = iso8601wknum(& dec31ly);
01030 #endif
01031 }
01032 break;
01033 }
01034
01035 if (timeptr->tm_mon == 11) {
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 int wday, mday;
01048
01049 wday = timeptr->tm_wday;
01050 mday = timeptr->tm_mday;
01051 if ( (wday == 1 && (mday >= 29 && mday <= 31))
01052 || (wday == 2 && (mday == 30 || mday == 31))
01053 || (wday == 3 && mday == 31))
01054 weeknum = 1;
01055 }
01056
01057 return weeknum;
01058 }
01059
01060 static int
01061 iso8601wknum_v(const struct vtm *vtm)
01062 {
01063 struct tm tm;
01064 vtm2tm_noyear(vtm, &tm);
01065 return iso8601wknum(&tm);
01066 }
01067
01068 #endif
01069
01070
01071
01072
01073
01074 #ifndef __STDC__
01075 static int
01076 weeknumber(timeptr, firstweekday)
01077 const struct tm *timeptr;
01078 int firstweekday;
01079 #else
01080 static int
01081 weeknumber(const struct tm *timeptr, int firstweekday)
01082 #endif
01083 {
01084 int wday = timeptr->tm_wday;
01085 int ret;
01086
01087 if (firstweekday == 1) {
01088 if (wday == 0)
01089 wday = 6;
01090 else
01091 wday--;
01092 }
01093 ret = ((timeptr->tm_yday + 7 - wday) / 7);
01094 if (ret < 0)
01095 ret = 0;
01096 return ret;
01097 }
01098
01099 static int
01100 weeknumber_v(const struct vtm *vtm, int firstweekday)
01101 {
01102 struct tm tm;
01103 vtm2tm_noyear(vtm, &tm);
01104 return weeknumber(&tm, firstweekday);
01105 }
01106
01107 #if 0
01108
01109
01110 Date: Wed, 24 Apr 91 20:54:08 MDT
01111 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
01112 To: arnold@audiofax.com
01113
01114 Hi Arnold,
01115 in a process of fixing of strftime() in libraries on Atari ST I grabbed
01116 some pieces of code from your own strftime. When doing that it came
01117 to mind that your weeknumber() function compiles a little bit nicer
01118 in the following form:
01119
01120
01121
01122 {
01123 return (timeptr->tm_yday - timeptr->tm_wday +
01124 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
01125 }
01126 How nicer it depends on a compiler, of course, but always a tiny bit.
01127
01128 Cheers,
01129 Michal
01130 ntomczak@vm.ucs.ualberta.ca
01131 #endif
01132
01133 #ifdef TEST_STRFTIME
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162 #ifndef NULL
01163 #include <stdio.h>
01164 #endif
01165 #include <sys/time.h>
01166 #include <string.h>
01167
01168 #define MAXTIME 132
01169
01170
01171
01172
01173
01174 static char *array[] =
01175 {
01176 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
01177 "(%%B) full month name, var length (January..December) %B",
01178 "(%%C) Century %C",
01179 "(%%D) date (%%m/%%d/%%y) %D",
01180 "(%%E) Locale extensions (ignored) %E",
01181 "(%%H) hour (24-hour clock, 00..23) %H",
01182 "(%%I) hour (12-hour clock, 01..12) %I",
01183 "(%%M) minute (00..59) %M",
01184 "(%%O) Locale extensions (ignored) %O",
01185 "(%%R) time, 24-hour (%%H:%%M) %R",
01186 "(%%S) second (00..60) %S",
01187 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
01188 "(%%U) week of year, Sunday as first day of week (00..53) %U",
01189 "(%%V) week of year according to ISO 8601 %V",
01190 "(%%W) week of year, Monday as first day of week (00..53) %W",
01191 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
01192 "(%%Y) year with century (1970...) %Y",
01193 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
01194 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
01195 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
01196 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
01197 "(%%d) day of the month (01..31) %d",
01198 "(%%e) day of the month, blank-padded ( 1..31) %e",
01199 "(%%h) should be same as (%%b) %h",
01200 "(%%j) day of the year (001..366) %j",
01201 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
01202 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
01203 "(%%m) month (01..12) %m",
01204 "(%%p) locale's AM or PM based on 12-hour clock %p",
01205 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
01206 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
01207 "(%%v) VMS date (dd-bbb-YYYY) %v",
01208 "(%%w) day of week (0..6, Sunday == 0) %w",
01209 "(%%x) appropriate locale date representation %x",
01210 "(%%y) last two digits of year (00..99) %y",
01211 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
01212 (char *) NULL
01213 };
01214
01215
01216
01217 int
01218 main(argc, argv)
01219 int argc;
01220 char **argv;
01221 {
01222 long time();
01223
01224 char *next;
01225 char string[MAXTIME];
01226
01227 int k;
01228 int length;
01229
01230 struct tm *tm;
01231
01232 long clock;
01233
01234
01235
01236 clock = time((long *) 0);
01237 tm = localtime(&clock);
01238
01239 for (k = 0; next = array[k]; k++) {
01240 length = strftime(string, MAXTIME, next, tm);
01241 printf("%s\n", string);
01242 }
01243
01244 exit(0);
01245 }
01246 #endif
01247