From 64097bd7332f55d7b7af941388cf55076449439a Mon Sep 17 00:00:00 2001 From: tadf Date: Sun, 5 Jun 2011 08:53:56 +0000 Subject: * ext/date/date_core.c (m_real_cwyear): new. derived from m_cwyear. * ext/date/date_strftime.c: trivial changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31925 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_core.c | 32 ++++++++++---- ext/date/date_strftime.c | 108 ++++++++++++++++++++++++++--------------------- 2 files changed, 84 insertions(+), 56 deletions(-) (limited to 'ext') diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 5743e74f67..5de537a939 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -1707,18 +1707,32 @@ m_wday(union DateData *x) return c_jd_to_wday(m_local_jd(x)); } -static VALUE +static int m_cwyear(union DateData *x) { - double sg; int ry, rw, rd; - VALUE ry2; - sg = x_sg(x); /* !=m_sg() */ - c_jd_to_commercial(m_local_jd(x), sg, + c_jd_to_commercial(m_local_jd(x), x_sg(x), /* !=m_sg() */ &ry, &rw, &rd); - encode_year(m_nth(x), ry, sg, &ry2); - return ry2; + return ry; +} + +static VALUE +m_real_cwyear(union DateData *x) +{ + VALUE nth, ry; + int year; + + nth = m_nth(x); + year = m_cwyear(x); + + if (f_zero_p(nth)) + return INT2FIX(year); + + encode_year(nth, year, + m_gregorian_p(x) ? -1 : +1, + &ry); + return ry; } static int @@ -4716,7 +4730,7 @@ static VALUE d_lite_cwyear(VALUE self) { get_d1(self); - return m_cwyear(dat); + return m_real_cwyear(dat); } /* @@ -6396,7 +6410,7 @@ static struct tmx_funcs tmx_funcs = { (int (*)(void *))m_yday, (int (*)(void *))m_mon, (int (*)(void *))m_mday, - (VALUE (*)(void *))m_cwyear, + (VALUE (*)(void *))m_real_cwyear, (int (*)(void *))m_cweek, (int (*)(void *))m_cwday, (int (*)(void *))m_wnum0, diff --git a/ext/date/date_strftime.c b/ext/date/date_strftime.c index 6525209d56..5781d3b795 100644 --- a/ext/date/date_strftime.c +++ b/ext/date/date_strftime.c @@ -180,7 +180,6 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, long off; ptrdiff_t i; int w; - long y; int precision, flags, colons; char padding; enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E}; @@ -300,10 +299,13 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); flags |= BIT_OF(UPPER); } - if (tmx_wday < 0 || tmx_wday > 6) - i = 1, tp = "?"; - else - i = 3, tp = days_l[tmx_wday]; + { + int wday = tmx_wday; + if (wday < 0 || wday > 6) + i = 1, tp = "?"; + else + i = 3, tp = days_l[wday]; + } break; case 'A': /* full weekday name */ @@ -311,10 +313,13 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); flags |= BIT_OF(UPPER); } - if (tmx_wday < 0 || tmx_wday > 6) - i = 1, tp = "?"; - else - i = strlen(tp = days_l[tmx_wday]); + { + int wday = tmx_wday; + if (wday < 0 || wday > 6) + i = 1, tp = "?"; + else + i = strlen(tp = days_l[wday]); + } break; #ifdef SYSV_EXT @@ -325,10 +330,13 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); flags |= BIT_OF(UPPER); } - if (tmx_mon < 1 || tmx_mon > 12) - i = 1, tp = "?"; - else - i = 3, tp = months_l[tmx_mon-1]; + { + int mon = tmx_mon; + if (mon < 1 || mon > 12) + i = 1, tp = "?"; + else + i = 3, tp = months_l[mon-1]; + } break; case 'B': /* full month name */ @@ -336,10 +344,13 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); flags |= BIT_OF(UPPER); } - if (tmx_mon < 1 || tmx_mon > 12) - i = 1, tp = "?"; - else - i = strlen(tp = months_l[tmx_mon-1]); + { + int mon = tmx_mon; + if (mon < 1 || mon > 12) + i = 1, tp = "?"; + else + i = strlen(tp = months_l[mon-1]); + } break; case 'c': /* appropriate date and time representation */ @@ -410,8 +421,8 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, } continue; - case 'S': /* second, 00 - 60 */ - i = range(0, tmx_sec, 60); + case 'S': /* second, 00 - 59 */ + i = range(0, tmx_sec, 59); FMT('0', 2, "d", (int)i); continue; @@ -442,13 +453,16 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, continue; case 'Y': /* year with century */ - if (FIXNUM_P(tmx_year)) { - long y = FIX2LONG(tmx_year); - FMT('0', 0 <= y ? 4 : 5, "ld", y); - } - else { - FMTV('0', 4, "d", tmx_year); - } + { + VALUE year = tmx_year; + if (FIXNUM_P(year)) { + long y = FIX2LONG(year); + FMT('0', 0 <= y ? 4 : 5, "ld", y); + } + else { + FMTV('0', 4, "d", year); + } + } continue; #ifdef MAILHEADER_EXT @@ -549,11 +563,14 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE)); flags |= BIT_OF(LOWER); } - if (tmx_zone == NULL) - tp = ""; - else - tp = tmx_zone; - i = strlen(tp); + { + char *zone = tmx_zone; + if (zone == NULL) + tp = ""; + else + tp = zone; + i = strlen(tp); + } break; #ifdef SYSV_EXT @@ -639,24 +656,21 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, #endif /* POSIX2_DATE */ #ifdef ISO_DATE_EXT - case 'G': - case 'g': + case 'g': /* year of ISO week without a century */ + i = NUM2INT(mod(tmx_cwyear, INT2FIX(100))); + FMT('0', 2, "d", (int)i); + continue; + + case 'G': /* year of ISO week with century */ { - VALUE yv = tmx_cwyear; - if (*format == 'G') { - if (FIXNUM_P(yv)) { - long y = FIX2LONG(yv); - FMT('0', 0 <= y ? 4 : 5, "ld", y); - } - else { - FMTV('0', 4, "d", yv); - } + VALUE year = tmx_cwyear; + if (FIXNUM_P(year)) { + long y = FIX2LONG(year); + FMT('0', 0 <= y ? 4 : 5, "ld", y); + } + else { + FMTV('0', 4, "d", year); } - else { - yv = mod(yv, INT2FIX(100)); - y = FIX2LONG(yv); - FMT('0', 2, "ld", y); - } continue; } -- cgit v1.2.3