Project

General

Profile

Bug #14241

Time.strptime() doesn't support the directive "%W".

Added by stsuboi (Sougo TSUBOI) almost 2 years ago. Updated 16 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.3.3p222 (2016-11-21 revision 56859) [x64-mingw32]
[ruby-core:84478]

Description

According to reference manual, Time.strptime() accepts the directive "%W" in a format string, but it seems not to be worked.
On the other hand, Date.strptime() accepts "%W" correctly.

C:>ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x64-mingw32]

C:>irb
irb(main):001:0> require "time"
=> true
irb(main):003:0> Time.strptime("2017 1", "%Y %W").strftime("%F")
=> "2017-01-01"
irb(main):004:0> Date.strptime("2017 1", "%Y %W").strftime("%F")
=> "2017-01-02"


Files

time-strptime-wnum.patch (1.89 KB) time-strptime-wnum.patch jeremyevans0 (Jeremy Evans), 06/21/2019 05:47 PM
time-strptime-wnum-cw-14241.patch (3.2 KB) time-strptime-wnum-cw-14241.patch jeremyevans0 (Jeremy Evans), 08/11/2019 07:31 PM

Related issues

Related to Ruby master - Bug #9836: Bad Implementation of Time.strptimeClosedActions

Associated revisions

Revision a9d4f2d0
Added by jeremyevans (Jeremy Evans) 16 days ago

Support %U/%u/%W/%w/%V/%g/%G formats in Time.strptime

Most of these formats were documented as supported, but were not
actually supported. Document that %g and %G are supported.

If %U/%W is specified without yday and mon/mday are not specified,
then Date.strptime is used to get the appropriate yday.

If cwyear is specifier without the year, or cwday and cweek are
specified without mday and mon, then use Date.strptime and convert
the resulting value to Time, since Time.make_time cannot handle
those conversions

Fixes [Bug #9836]
Fixes [Bug #14241]

History

#1

Updated by stsuboi (Sougo TSUBOI) almost 2 years ago

  • Description updated (diff)
#2

Updated by stsuboi (Sougo TSUBOI) almost 2 years ago

  • Description updated (diff)
#3

Updated by stsuboi (Sougo TSUBOI) almost 2 years ago

  • Description updated (diff)
#4

Updated by stsuboi (Sougo TSUBOI) almost 2 years ago

  • Subject changed from Time.strptime() doesn't support the directive "%W" in . to Time.strptime() doesn't support the directive "%W".

Updated by Anonymous almost 2 years ago

To add to the report, the Time.strptime doesn't support any of the following directives

%w %W %j %u

basically, it doesn't support anything outputted from the Date._strptim that is not in the following hash keys
(https://github.com/ruby/ruby/blob/ruby_2_5/lib/time.rb#L448)

:year , :mon, :mday, :hour, :min, :sec, :sec_fraction, :zone

and the previous format directives produce various different keys like

:wnum1, :cwday, :wday, :yday

examples:

Date.strptime("2017 1 3", "%Y %W %u").strftime("%F")
=> "2017-01-04"
Time.strptime("2017 1 3", "%Y %W %u").strftime("%F")
=> "2017-01-01"
Date.strptime("2017 20", "%Y %j").strftime("%F")
=> "2017-01-20"
Time.strptime("2017 20", "%Y %j").strftime("%F")
=> "2017-01-01"

Updated by jeremyevans0 (Jeremy Evans) 6 months ago

I agree this is a bug that should be fixed. Attached is a patch that implements support for %U/%W/%u/%w in Time#strptime.

#7

Updated by jeremyevans0 (Jeremy Evans) 4 months ago

  • Related to Bug #9836: Bad Implementation of Time.strptime added

Updated by jeremyevans0 (Jeremy Evans) 4 months ago

Here's an updated patch that handles %V, %g, and %G as well.

#9

Updated by jeremyevans (Jeremy Evans) 16 days ago

  • Status changed from Assigned to Closed

Applied in changeset git|a9d4f2d03c847ec1c89dc03a5076a9fa29ffa61f.


Support %U/%u/%W/%w/%V/%g/%G formats in Time.strptime

Most of these formats were documented as supported, but were not
actually supported. Document that %g and %G are supported.

If %U/%W is specified without yday and mon/mday are not specified,
then Date.strptime is used to get the appropriate yday.

If cwyear is specifier without the year, or cwday and cweek are
specified without mday and mon, then use Date.strptime and convert
the resulting value to Time, since Time.make_time cannot handle
those conversions

Fixes [Bug #9836]
Fixes [Bug #14241]

Also available in: Atom PDF