Ruby 2.0 DateTime::strptime formatting problem?

Trying to use DateTime::strptime to convert date/time strings. The
“month” format string returns “Invalid Date” for almost every
combination of input string and format string, including some
combinations that absolutely should work. I started with full date/time
strings but because of the errors I’ve tried to isolate the problem down
to just the “month” component. I have not tried “day” or “hour”.

How do I verify this as a defect?
If it is, what is next step? Thx in advance.

Here is Ruby version info and results of the attached script:

C:\Users\pt\Documents\Source\rb>ruby -v
ruby 2.0.0p195 (2013-05-14) [i386-mingw32]

C:\Users\pt\Documents\Source\rb>ruby DateTime_strptime_problem.rb

DateTime.strptime( ‘01’ , ‘%m’ ) :: month fmt string ZERO-padded
{SHOULD PASS} --> #<DateTime: 2013-01-01T00:00:00+00:00
((2456294j,0s,0n),+0s,2299161j)>
DateTime.strptime( ‘01’ , ‘%_m’ ) :: month fmt string blank-padded -->
invalid date
DateTime.strptime( ‘01’ , ‘%-m’ ) :: month fmt string unpadded -->
invalid date
DateTime.strptime( ’ 1’ , ‘%m’ ) :: month fmt string ZERO-padded -->
invalid date
DateTime.strptime( ’ 1’ , ‘%_m’ ) :: month fmt string blank-padded
{SHOULD PASS} --> invalid date
DateTime.strptime( ’ 1’ , ‘%-m’ ) :: month fmt string unpadded -->
invalid date
DateTime.strptime( ‘1’ , ‘%m’ ) :: month fmt string ZERO-padded -->
#<DateTime: 2013-01-01T00:00:00+00:00 ((2456294j,0s,0n),+0s,2299161j)>
DateTime.strptime( ‘1’ , ‘%_m’ ) :: month fmt string blank-padded -->
invalid date
DateTime.strptime( ‘1’ , ‘%-m’ ) :: month fmt string unpadded {SHOULD
PASS} --> invalid date

DateTime.strptime( ‘011’ , ‘%m’ ) :: month fmt string ZERO-padded
{SHOULD PASS} --> #<DateTime: 2013-01-01T00:00:00+00:00
((2456294j,0s,0n),+0s,2299161j)>
DateTime.strptime( ‘011’ , ‘%_m’ ) :: month fmt string blank-padded -->
invalid date
DateTime.strptime( ‘011’ , ‘%-m’ ) :: month fmt string unpadded -->
invalid date
DateTime.strptime( ’ 11’ , ‘%m’ ) :: month fmt string ZERO-padded -->
invalid date
DateTime.strptime( ’ 11’ , ‘%_m’ ) :: month fmt string blank-padded
{SHOULD PASS} --> invalid date
DateTime.strptime( ’ 11’ , ‘%-m’ ) :: month fmt string unpadded -->
invalid date
DateTime.strptime( ‘11’ , ‘%m’ ) :: month fmt string ZERO-padded -->
#<DateTime: 2013-11-01T00:00:00+00:00 ((2456598j,0s,0n),+0s,2299161j)>
DateTime.strptime( ‘11’ , ‘%_m’ ) :: month fmt string blank-padded -->
invalid date
DateTime.strptime( ‘11’ , ‘%-m’ ) :: month fmt string unpadded {SHOULD
PASS} --> invalid date

Mike S. wrote in post #1120977:

On 2013-09-08, at 5:49 PM, pt pt [email protected] wrote:

{snipsnip}

The DateTime.strptime documentation says it doesnt support the
specification of flags:

{snipsnip}

So I wouldn’t expect the flags to work in strptime.

There might be an argument that the ArgumentError could be more helpful
in that it is an error in the date format string.

Hope this helps,

Mike

Mike S. [email protected]
Mike Stok

The “`Stok’ disclaimers” apply.

AUGH! I read the bit about flag & width chars, but mistook the flag
characters as modifiers (self.respond_to? :ruby::gnubie). Thx for the
pointer.

I would definitely vote that using the unsupported chars constitutes at
least a warning condition.

On 2013-09-08, at 5:49 PM, pt pt [email protected] wrote:

((2456294j,0s,0n),+0s,2299161j)>
DateTime.strptime( ‘1’ , ‘%m’ ) :: month fmt string ZERO-padded →
invalid date
DateTime.strptime( ‘11’ , ‘%_m’ ) :: month fmt string blank-padded →
invalid date
DateTime.strptime( ‘11’ , ‘%-m’ ) :: month fmt string unpadded {SHOULD
PASS} → invalid date

Attachments:
http://www.ruby-forum.com/attachment/8717/DateTime_strptime_problem.rb

The DateTime.strptime documentation says it doesnt support the
specification of flags:

= DateTime.strptime

(from ruby core)

DateTime.strptime([string=‘-4712-01-01T00:00:00+00:00’[,
format=‘%FT%T%z’[ ,start=ITALY]]]) → datetime


Parses the given representation of date and time with the given
template, and
creates a date object. strptime does not support specification of flags
and
width unlike strftime.

DateTime.strptime(‘2001-02-03T04:05:06+07:00’, ‘%Y-%m-%dT%H:%M:%S%z’)
#=> #<DateTime: 2001-02-03T04:05:06+07:00
…>
DateTime.strptime(‘03-02-2001 04:05:06 PM’, ‘%d-%m-%Y %I:%M:%S %p’)
#=> #<DateTime: 2001-02-03T16:05:06+00:00
…>
DateTime.strptime(‘2001-W05-6T04:05:06+07:00’, ‘%G-W%V-%uT%H:%M:%S%z’)
#=> #<DateTime: 2001-02-03T04:05:06+07:00
…>
DateTime.strptime(‘2001 04 6 04 05 06 +7’, ‘%Y %U %w %H %M %S %z’)
#=> #<DateTime: 2001-02-03T04:05:06+07:00
…>
DateTime.strptime(‘2001 05 6 04 05 06 +7’, ‘%Y %W %u %H %M %S %z’)
#=> #<DateTime: 2001-02-03T04:05:06+07:00
…>
DateTime.strptime(‘-1’, ‘%s’)
#=> #<DateTime: 1969-12-31T23:59:59+00:00
…>
DateTime.strptime(‘-1000’, ‘%Q’)
#=> #<DateTime: 1969-12-31T23:59:59+00:00
…>
DateTime.strptime(‘sat3feb014pm+7’, ‘%a%d%b%y%H%p%z’)
#=> #<DateTime: 2001-02-03T16:00:00+07:00
…>

See also strptime(3) and strftime.

And the DateTime.strftime documentation includes this description of
flags:

Flags:
- don’t pad a numerical output.
_ use spaces for padding.
0 use zeros for padding.
^ upcase the result string.
# change case.
: use colons for %z.

So I wouldn’t expect the flags to work in strptime.

There might be an argument that the ArgumentError could be more helpful
in that it is an error in the date format string.

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.