[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Perl: datetime stamps (2006-09-28T18:38:07.97-05:0 0)



On Sep 29, 2006, at 8:53 AM, Thompson, Kenn wrote:
Sean Baker wrote:
Class::Date and Gmtime perhaps? Hmmm.....

Seems I remember a wrapper being written for Class::Date that did nothing but format the output of Class::Date into various local application formats. Why wouldn't that approach apply here, unless you were trying to improve on
performance?

Ya, I thought so too so I tried that back on Sep 29.

But %z and tzoffset doesn't seem to work. At least not on my Mac...

----
$ cat j.pl
#!/usr/bin/perl

use Class::Date;
my $stamp = Class::Date::date(time);
local $Class::Date::DATE_FORMAT="%Y-%m-%dT%H:%M:%S %z %Z";
print "$stamp\n";
print $stamp->tzoffset;
print "\n";

$ perl j.pl
2006-10-06T07:20:57 +0000 CDT
-18000
----


Over on the OTA forum Stephen Adkins posted really tight Date::Format examples (below). They work great on my Mac and are tighter than my Date::Calc version so I think Stephen wins the cookie.

(One of these days I'm going to win me one of those cookies. -grin-)

j



------ Forwarded Message
From: Stephen Adkins <spadkins@gmail.com>
Date: Fri, 29 Sep 2006 16:58:31 -0400
To: <OTA-Impl-Forum@googlegroups.com>
Subject: [OTA-Impl-Forum] Re: Perl: datetime stamps (2006-09-28T18:38:07.97-05:00)


Jay,

I use Date::Format and Date::Parse for most of my time/date handling
needs in perl. The following two scripts are alternate (and easy) ways to
print out dates in ISO (and hence OTA) format (depending on whether you
need fractions of seconds or not).

Stephen

::::::::::::::
go
::::::::::::::
#!/usr/bin/perl -w
use strict;
use Date::Format;
my $time = time();
print time2str("%Y-%m-%dT%H:%M:%S%z", $time), "\n";

::::::::::::::
go2
::::::::::::::
#!/usr/bin/perl -w
use strict;
use Date::Format;
use Time::HiRes;
my ($time, $usec) = Time::HiRes::gettimeofday();
my $frac = sprintf("%02d", int($usec/10000));   # only top two digits,
zero padded
print time2str("%Y-%m-%dT%H:%M:%S.$frac%z", $time), "\n";

spadkins@pompeii:/home/spadkins> go; go2
2006-09-29T16:51:34-0400
2006-09-29T16:51:34.79-0400