Does Time.strptime not parse weekly forms?

Using Ruby 2.1, I am trying to find a backlink Time#strftime('%Y%U'). For instance:

s = Time.parse("2014-05-07 16:41:48 -0700").strftime('%Y%U')
    # 201418

t = Time.strptime(s, '%Y%U')
    # Expected: 2014-05-04 00:00:00 -0700
    # Actual:   2014-01-01 00:00:00 -0800

This section was suggested for use %G, so I read the documents and tried, but all that I chose is the current time. eg:

t = Time.strptime('201418', '%G%U')
    # 2014-05-13 12:07:51 -0700

From the documents, he looks at me, which %Gis only intended to work with %V, since both ISO 8601 and %Uno, but even using %G%VI will return the current time.

So what is the right way to turn a string %Y%Uinto an appropriate one Time?

+4
source share
1 answer

, , DateTime.

s = Time.parse("2014-05-07 16:41:48 -0700").strftime('%Y%U')
DateTime.strptime(s, "%Y%U")
#<DateTime: 2014-05-04T00:00:00+00:00 ((2456782j,0s,0n),+0s,2299161j)>

Edit:

, . Time.strptime Date._strptime, . Time.strptime , , , .. .

.

+4

Source: https://habr.com/ru/post/1540426/


All Articles