Time.strptime doesn't parse week formatters?

ruby

Solution

This could be a bug, since it seems to work fine if you use `DateTime` instead.

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:

If you look at the source code this is actually just dumb coding. `Time.strptime` calls `Date._strptime` which correctly parses out the year and week number. But then `Time.strptime` stupidly only looks for the usual year, month, day, hour, etc. and ignores the week number entirely.

I submitted a bug report.

Problem

Using Ruby 2.1, I am trying to find the reciprocal of `Time#strftime('%Y%U')`. For example: ``` 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 topic suggested to use `%G` so I read the docs and tried it, but all I get out of it is the current Time. eg: ``` t = Time.strptime('201418', '%G%U') # 2014-05-13 12:07:51 -0700 ``` From the docs, it looks to me that `%G` is only intended to work with `%V` as both are ISO 8601 and `%U` is not, but even even using `%G%V` I get back the current time. So what's the right way to turn a `%Y%U` string into the corresponding `Time`?

Original source