Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Newbie: Request help on creating CSV using timestamp data

by Tux (Monsignor)
on Nov 22, 2013 at 14:43 UTC ( #1063954=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Newbie: Request help on creating CSV using timestamp data
in thread Newbie: Request help on creating CSV using timestamp data

I'd start with making that easier to extend (and maintain). The snippet uses the default (builtin) localtime function, not the one returning a hashref or an object:

sub timestamp { my @st = localtime (shift); return sprintf "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $st[5] + 1900, ++$st[4], @st[3,2,1,0]; } sub starttimestamp { return timestamp (time); } sub endtimestamp { return timestamp (time + (15 * 60)); } sub futuretimestamp { return timestamp (time + shift); } my $fts = futuretimestamp (162 * 60 * 60); # Stamp 162 hours ahead

This way, there is only one location that defines the required format and several easy ways to use that.


Enjoy, Have FUN! H.Merijn


Comment on Re^3: Newbie: Request help on creating CSV using timestamp data
Select or Download Code
Re^4: Newbie: Request help on creating CSV using timestamp data
by Anonymous Monk on Nov 25, 2013 at 10:34 UTC
    Using the perl version --> v5.8.8 built. Getting the following error using the above snippet,
    Use of uninitialized value in addition (+)

      Is it possible that you are still using a module that overrides the builtin localtime function? I did mention that my example did use the builtin. (to be complete, I tested the same with use Time::Local; and use Time::Piece;, and both passed too).

      I ran my piece of example code through every version of perl ever released since 5.6.0, and they all work as expected. Here is the shortened list of results (I skipped all development releases and older versions of stable branches, but included 5.8.8):

      $ cat test.pl use strict; use warnings; sub timestamp { my @st = localtime (shift); return sprintf "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $st[5] + 1900, ++$st[4], @st[3,2,1,0]; } sub starttimestamp { return timestamp (time); } sub endtimestamp { return timestamp (time + (15 * 60)); } sub futuretimestamp { return timestamp (time + shift); } my $fts = futuretimestamp (162 * 60 * 60); # Stamp 162 hours ahead print $fts, "\n"; $ perl-all test.pl === base/perl5.6.2 5.006002 i686-linux-64int-perlio 2013-12-02T06:46:51.000-0700 === base/tperl5.6.2 5.006002 i686-linux-thread-multi-64int-ld-perli +o 2013-12-02T06:46:52.000-0700 === base/perl5.8.8 5.008008 i686-linux-64int 2013-12-02T06:46:56.000-0700 === base/tperl5.8.8 5.008008 i686-linux-thread-multi-64int-ld 2013-12-02T06:46:56.000-0700 === base/perl5.8.9 5.008009 i686-linux-64int 2013-12-02T06:46:56.000-0700 === base/tperl5.8.9 5.008009 i686-linux-thread-multi-64int-ld 2013-12-02T06:46:57.000-0700 === base/perl5.10.1 5.010001 i686-linux-64int 2013-12-02T06:46:58.000-0700 === base/tperl5.10.1 5.010001 i686-linux-thread-multi-64int-ld 2013-12-02T06:46:58.000-0700 === base/perl5.12.5 5.012005 i686-linux-64int 2013-12-02T06:47:05.000-0700 === base/tperl5.12.5 5.012005 i686-linux-thread-multi-64int-ld 2013-12-02T06:47:05.000-0700 === base/cperl5.14.4 5.014004 i686-linux-64int-ld 2013-12-02T06:47:15.000-0700 === base/perl5.14.4 5.014004 i686-linux-64int 2013-12-02T06:47:16.000-0700 === base/tperl5.14.4 5.014004 i686-linux-thread-multi-64int-ld 2013-12-02T06:47:16.000-0700 === base/cperl5.16.3 5.016003 i686-linux-64int-ld 2013-12-02T06:47:25.000-0700 === base/perl5.16.3 5.016003 i686-linux-64int 2013-12-02T06:47:25.000-0700 === base/tperl5.16.3 5.016003 i686-linux-thread-multi-64int-ld 2013-12-02T06:47:26.000-0700 === base/cperl5.18.2 5.018002 i686-linux-64int-ld 2013-12-02T06:47:36.000-0700 === base/perl5.18.2 5.018002 i686-linux-64int 2013-12-02T06:47:37.000-0700 === base/tperl5.18.2 5.018002 i686-linux-thread-multi-64int-ld 2013-12-02T06:47:37.000-0700

      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063954]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-09-15 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls