# DateTime.strptime()

**URL:** <https://www.ruby-forum.com/t/datetime-strptime/79301>\
**Category:** Ruby\
**Created:** [December 14, 2006, 1:04am UTC](https://www.ruby-forum.com/t/datetime-strptime/79301 "2006-12-14T01:04:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [December 14, 2006, 1:04am UTC](https://www.ruby-forum.com/t/datetime-strptime/79301/1 "2006-12-14T01:04:19Z")

</div>

Sorry for posting a beginner question here, but it seems like  
date/format.rb  
is missing documentation on [ruby-doc.org](http://ruby-doc.org).

So, the question is, why does this code not work? (I’m using  
ruby-1.8.4)

irb(main):001:0\> require ‘date’  
=\> true  
irb(main):002:0\> myd = DateTime.strptime(“1166049481”, ‘%s’)  
ArgumentError: 3 elements of civil date are necessary  
from lib/ruby/1.8/date.rb:1214:in `new_with_hash' from lib/ruby/1.8/date.rb:1238:in `strptime’  
from (irb):2

I was able to make this work: Date::strptime(“1/1/1970”, ‘%d/%m/%Y’) so  
I’m  
pretty sure I don’t have a syntax error.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [December 14, 2006, 6:49am UTC](https://www.ruby-forum.com/t/datetime-strptime/79301/2 "2006-12-14T06:49:01Z")

</div>

Thanks Morton. I did read date/format.rb and in 1.8.4 (what I’m  
running) it  
supports the %s format which I believe is supposed to represent the  
number  
of seconds since 1/1/1970. The number I’m passing in represents the  
number  
of seconds since 1/1/1970.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [December 14, 2006, 5:44am UTC](https://www.ruby-forum.com/t/datetime-strptime/79301/3 "2006-12-14T05:44:52Z")

</div>

On Dec 13, 2006, at 7:03 PM, Matt G. wrote:

> ArgumentError: 3 elements of civil date are necessary  
> from lib/ruby/1.8/date.rb:1214:in `new_with_hash' from lib/ruby/1.8/date.rb:1238:in`strptime’  
> from (irb):2
> 
> I was able to make this work: Date::strptime(“1/1/1970”, ‘%d/%m/%  
> Y’) so I’m  
> pretty sure I don’t have a syntax error.

DateTime.strptime simply can’t handle “1166049481” as an argument. It  
wants something that looks like a date/time stamp that a human might  
be able to read. Unfortunately, as you point out, exactly what it  
wants isn’t documented. You’re expected to read date/format.rb, which  
IMO isn’t easy decipher.

BTW, even if your first argument were acceptable, I think your second  
would be ignored – at least in 1.8.2, which is what I’m running, %s  
has no effect.

> Thanks for any help.

Not much help, I’m afraid, but you did ask for “any help” 🙂

Regards, Morton

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [December 15, 2006, 3:47am UTC](https://www.ruby-forum.com/t/datetime-strptime/79301/4 "2006-12-15T03:47:55Z")

</div>

On 12/14/06, Matt G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Morton. I did read date/format.rb and in 1.8.4 (what I’m running) it  
> supports the %s format which I believe is supposed to represent the number  
> of seconds since 1/1/1970. The number I’m passing in represents the number  
> of seconds since 1/1/1970.

It looks like a bug in date/format.rb to me. It also works as you  
expect in 1.9. Try this patch:

— format.rb.orig 2006-12-14 17:48:54.000000000 +1100  
+++ format.rb 2006-12-15 13:35:23.000000000 +1100  
@@ -1,6 +1,7 @@

# format.rb: Written by Tadayoshi F. 1999-2005

# $Id: format.rb,v 2.15 2005-02-06 13:28:48+09 tadf Exp $

+require ‘time’  
require ‘rational’

class Date  
@@ -138,8 +139,14 @@  
elem[:sec] = val  
when ‘%s’  
return unless str.sub!(/\A(\d+)/o, ‘’)

- 

```
  val = $1.to_i

```

- 

```
  elem[:seconds] = val

```

- 

```
   time = Time.at($1.to_i)

```

- 

```
   elem[:year] = time.year

```

- 

```
   elem[:mon] = time.mon

```

- 

```
   elem[:mday] = time.mday

```

- 

```
   elem[:hour] = time.hour

```

- 

```
   elem[:min] = time.min

```

- 

```
  elem[:sec] = time.sec

```

- 

```
   elem[:offset] = time.utc_offset
 when '%T'
  return unless __strptime(str, '%H:%M:%S', elem)
 when '%t'

```
