Actions
Bug #17042
closedTimes with timezones return incorrect week numbers
Bug #17042: Times with timezones return incorrect week numbers
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.8.0dev (2020-07-15 master 79d06483a8)
Description
Times with timezones return incorrect week numbers from strftime. For example:
$ irb -r tzinfo irb(main):001:0> Time.utc(2020, 7, 22, 12, 0, 0).strftime('%U %V %W') => "29 30 29" irb(main):002:0> Time.new(2020, 7, 22, 12, 0, 0, TZInfo::Timezone.get('America/New_York')).strftime('%U %V %W') => "00 00 00" Follow up to #17024
Updated by S_H_ (Shun Hiraoka) over 5 years ago
Same behaviour in 2.7 & 2.6(checked 2.7.1, 2.7.0, 2.6.6, 2.6.5).
Updated by S_H_ (Shun Hiraoka) over 5 years ago
Apparently the calculation result ret = ((timeptr->tm_yday + 7 - wday) / 7); is negative(timeptr->tm_yday is negative).
static int weeknumber(const struct tm *timeptr, int firstweekday) { int wday = timeptr->tm_wday; int ret; if (firstweekday == 1) { if (wday == 0)/* sunday */ wday = 6; else wday--; } ret = ((timeptr->tm_yday + 7 - wday) / 7); if (ret < 0) ret = 0; return ret; }
Updated by S_H_ (Shun Hiraoka) over 5 years ago
This code resolve this behaviour.
static VALUE time_strftime(VALUE time, VALUE format) { struct time_object *tobj; const char *fmt; long len; rb_encoding *enc; VALUE tmp; GetTimeval(time, tobj); if (tobj->vtm.yday == 0) { VALUE zone = tobj->vtm.zone; if (!NIL_P(zone)) zone_localtime(zone, time); } MAKE_TM(time, tobj); StringValue(format); if (!rb_enc_str_asciicompat_p(format)) { rb_raise(rb_eArgError, "format should have ASCII compatible encoding"); } tmp = rb_str_tmp_frozen_acquire(format); fmt = RSTRING_PTR(tmp); len = RSTRING_LEN(tmp); enc = rb_enc_get(format); if (len == 0) { rb_warning("strftime called with empty format string"); return rb_enc_str_new(0, 0, enc); } else { VALUE str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj)); rb_str_tmp_frozen_release(format, tmp); if (!str) rb_raise(rb_eArgError, "invalid format: %"PRIsVALUE, format); return str; } } tobj->vtm.ydayis seems to be 0.
So, these code resolve.
if (tobj->vtm.yday == 0) { VALUE zone = tobj->vtm.zone; if (!NIL_P(zone)) zone_localtime(zone, time); }
Updated by S_H_ (Shun Hiraoka) over 5 years ago
Created patch.
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Status changed from Open to Closed
Applied in changeset git|8ed687a4d7b7a77f30f8e937f58aae74bfb699b6.
Test for weeknumber with timezone [Bug #17042]
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN to 2.5: DONTNEED, 2.6: REQUIRED, 2.7: REQUIRED
Actions