# Ruby 2.0 DateTime::strptime formatting problem?

**URL:** <https://rubytalk.org/t/ruby-2-0-datetime-strptime-formatting-problem/68445>\
**Category:** ruby-talk\
**Created:** [8 September 2013 16:49 UTC](https://rubytalk.org/t/ruby-2-0-datetime-strptime-formatting-problem/68445 "2013-09-08T16:49:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [8 September 2013 16:49 UTC](https://rubytalk.org/t/ruby-2-0-datetime-strptime-formatting-problem/68445/1 "2013-09-08T16:49:25Z")

</div>

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

Attachments:  
[http://www.ruby-forum.com/attachment/8717/DateTime\_strptime\_problem.rb](http://www.ruby-forum.com/attachment/8717/DateTime_strptime_problem.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Stok1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_stok1/32/2065_2.png) [@Mike\_Stok1](https://rubytalk.org/u/Mike_Stok1)\
**Post date:** [8 September 2013 19:45 UTC](https://rubytalk.org/t/ruby-2-0-datetime-strptime-formatting-problem/68445/2 "2013-09-08T19:45:34Z")

</div>

The DateTime.strptime documentation says it doesn’t support the specification of flags:

= DateTime.strptime

(from ruby core)

> **···**
>
> On 2013-09-08, at 5:49 PM, pt pt \<lists@ruby-forum.com\> wrote:
> 
> > 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
> > 
> > Attachments:  
> > [http://www.ruby-forum.com/attachment/8717/DateTime\_strptime\_problem.rb](http://www.ruby-forum.com/attachment/8717/DateTime_strptime_problem.rb)
> 
> ------------------------------------------------------------------------------  
> &nbsp;&nbsp;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.
> 
> &nbsp;&nbsp;DateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 2001-02-03T04:05:06+07:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 2001-02-03T16:05:06+00:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('2001-W05-6T04:05:06+07:00', '%G-W%V-%uT%H:%M:%S%z')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 2001-02-03T04:05:06+07:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('2001 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 2001-02-03T04:05:06+07:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('2001 05 6 04 05 06 +7', '%Y %W %u %H %M %S %z')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 2001-02-03T04:05:06+07:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('-1', '%s')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 1969-12-31T23:59:59+00:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('-1000', '%Q')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 1969-12-31T23:59:59+00:00 ...\>  
> &nbsp;&nbsp;DateTime.strptime('sat3feb014pm+7', '%a%d%b%y%H%p%z')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#=\> #\<DateTime: 2001-02-03T16:00:00+07:00 ...\>
> 
> See also strptime(3) and strftime.
> 
> And the DateTime.strftime documentation includes this description of flags:
> 
> &nbsp;&nbsp;Flags:  
> &nbsp;&nbsp;&nbsp;&nbsp;- don't pad a numerical output.  
> &nbsp;&nbsp;&nbsp;&nbsp;\_ use spaces for padding.  
> &nbsp;&nbsp;&nbsp;&nbsp;0 use zeros for padding.  
> &nbsp;&nbsp;&nbsp;&nbsp;^ upcase the result string.  
> &nbsp;&nbsp;&nbsp;&nbsp;# change case.  
> &nbsp;&nbsp;&nbsp;&nbsp;: 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 Stok \<mike@stok.ca\>  
> [http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)
> 
> The "`Stok' disclaimers" apply.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 September 2013 23:39 UTC](https://rubytalk.org/t/ruby-2-0-datetime-strptime-formatting-problem/68445/3 "2013-09-10T23:39:04Z")

</div>

Mike Stok wrote in post #1120977:

> 

{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 Stok \<mike@stok.ca\>  
> [Mike Stok](http://www.stok.ca/~mike/)
> 
> 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 \<lists@ruby-forum.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
