# DateTime questions \[newbie\]

**URL:** <https://rubytalk.org/t/datetime-questions-newbie/15961>\
**Category:** ruby-talk\
**Created:** [23 January 2005 01:03 UTC](https://rubytalk.org/t/datetime-questions-newbie/15961 "2005-01-23T01:03:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [23 January 2005 01:03 UTC](https://rubytalk.org/t/datetime-questions-newbie/15961/1 "2005-01-23T01:03:51Z")

</div>

Hi!

I have some newbie questions.

> DateTime.strptime('2000-09-28 05:23:01 -0400', '%F %T %z').strftime('%s')

=\> "970132981"

> DateTime.strptime('970132981', '%s').strftime('%s')

ArgumentError: invalid date  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/date.rb:485:in `new\_with\_hash'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/date.rb:495:in `strptime'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):31

Why does the first command work but not the second one?

Out of curiosity is there a standard/builtin way of printing ISO8601  
date format instead of having to use to\_yaml and chopping off the "---  
" garbage string at the beginning of the input or having to manually  
specify '%F %T %z'?

Also, is there a way of printing fractional seconds? I see that  
DateTime has sec\_fraction() but '%N' doesn't seem to work in the  
string definition.

Thanks a lot!

Cheers,  
Navin.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 January 2005 15:25 UTC](https://rubytalk.org/t/datetime-questions-newbie/15961/2 "2005-01-25T15:25:13Z")

</div>

Hi,

I forwarded your questions to the author.

> I have some newbie questions.
> 
> > DateTime.strptime('2000-09-28 05:23:01 -0400', '%F %T %z').strftime('%s')
> 
> =\> "970132981"
> 
> > DateTime.strptime('970132981', '%s').strftime('%s')
> 
> ArgumentError: invalid date  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/date.rb:485:in `new\_with\_hash'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/date.rb:495:in `strptime'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):31
> 
> Why does the first command work but not the second one?

strptime does not support "%s". It supports year, month, date, hour,  
minute, second, and timezone.

> Out of curiosity is there a standard/builtin way of printing ISO8601  
> date format instead of having to use to\_yaml and chopping off the "---  
> " garbage string at the beginning of the input or having to manually  
> specify '%F %T %z'?

How about DateTime#to\_s?

> Also, is there a way of printing fractional seconds? I see that  
> DateTime has sec\_fraction() but '%N' doesn't seem to work in the  
> string definition.

The author did not know about the "%N". Is it defined in any standard  
(or on any platform)?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

> **···**
>
> In message "Re: DateTime questions [newbie]" on Sun, 23 Jan 2005 10:03:51 +0900, Navindra Umanee \<navindra@cs.mcgill.ca\> writes:

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [25 January 2005 16:03 UTC](https://rubytalk.org/t/datetime-questions-newbie/15961/3 "2005-01-25T16:03:23Z")

</div>

> I forwarded your questions to the author.

Oh, wow, thanks matz... and thanks for Ruby too! 🙂

> strptime does not support "%s". It supports year, month, date, hour,  
> minute, second, and timezone.

I was looking at the code and \_\_strptime in date/format.rb does have a  
case statement for %s... but I guess it is not fully implemented  
since new\_with\_hash does not know what to do with it.

I now understand that Ruby's strptime is really based on the Unix  
strptime and that %s is a GNU extension:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s The number of seconds since the epoch, i.e., since 1970-01-01  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;00:00:00 UTC. Leap seconds are not counted unless leap second  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;support is available.

(from the strptime(3) manual)

> \>Out of curiosity is there a standard/builtin way of printing ISO8601  
> \>date format instead of having to use to\_yaml and chopping off the "---  
> \>" garbage string at the beginning of the input or having to manually  
> \>specify '%F %T %z'?
> 
> How about DateTime#to\_s?

That does work... it's apparently what to\_yaml uses as well. But I  
also found Time which is more interesting to me since it can work with  
Unix time and has other interesting methods (unfortunately yaml.rb  
uses DateTime, so I might have to find a way to convert back and forth  
between Time and DateTime).

> The author did not know about the "%N". Is it defined in any standard  
> (or on any platform)?

I am only looking at the GNU date(1) manual which says "%N" is  
nanoseconds... I now understand that Ruby's strftime/strptime are  
probably based on the Unix system calls of the same name which don't  
support "%N" either.

I thought I might need it because the yaml.rb documentation uses  
fractional time in the date examples, but it doesn't seem necessary  
afterall.

Thanks again!

Cheers,  
Navin.

> **···**
>
> Yukihiro Matsumoto \<matz@ruby-lang.org\> wrote:

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [28 January 2005 04:09 UTC](https://rubytalk.org/t/datetime-questions-newbie/15961/4 "2005-01-28T04:09:07Z")

</div>

After driving myself mad, I finally realised that in fact this does  
not work and DateTime#to\_yaml seems to be broken as well.

irb(main):029:0\> YAML::load(DateTime.now.to\_yaml).class  
=\> String # Should not be String! Either DateTime or Time.

irb(main):030:0\> DateTime.now.to\_yaml  
=\> "--- 2005-01-27T22:40:56-0500"

The problem is that the above is not iso8601 because the timezone is  
-0500 instead of -05:00. Hard to catch when you're not looking too  
close like me... 🙂

The good news is that I finally realised I could eliminate DateTime  
altogether and do everything with Time. A big simplification! At  
least until 2038.

irb(main):031:0\> YAML::load(Time.now.to\_yaml).class  
=\> Time

Woohoo!

Cheers,  
Navin.

> **···**
>
> Yukihiro Matsumoto \<matz@ruby-lang.org\> wrote:
> 
> > \>Out of curiosity is there a standard/builtin way of printing ISO8601  
> > \>date format instead of having to use to\_yaml and chopping off the "---  
> > \>" garbage string at the beginning of the input or having to manually  
> > \>specify '%F %T %z'?
> > 
> > How about DateTime#to\_s?
