DateTime questions [newbie]

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
        from /usr/lib/ruby/1.8/date.rb:485:in `new_with_hash'
        from /usr/lib/ruby/1.8/date.rb:495:in `strptime'
        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.

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
       from /usr/lib/ruby/1.8/date.rb:485:in `new_with_hash'
       from /usr/lib/ruby/1.8/date.rb:495:in `strptime'
       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)?

              matz.

···

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

I forwarded your questions to the author.

Oh, wow, thanks matz... and thanks for Ruby too! :slight_smile:

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:

       %s The number of seconds since the epoch, i.e., since 1970-01-01
              00:00:00 UTC. Leap seconds are not counted unless leap second
              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:

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... :slight_smile:

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?