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

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

by Anonymous Monk
on Nov 22, 2013 at 13:12 UTC ( [id://1063933]=note: print w/replies, xml ) Need Help??


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

I have got the start and end time using the following code,
sub starttimestamp() { my $st = localtime; return sprintf( "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $st->year + 1900, $st->mon + 1, $st->mday, $st->hour, $st->min, $st->sec ); } sub endtimestamp() { my $et = localtime; return sprintf( "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $et->year + 1900, $et->mon + 1, $et->mday, $et->hour, $et->min + 15, $et->sec ); }
Now, i need a logic where i can keep incrementing the start and end timestamps by 15 or 30 or 60 mins. Thus, having unique timestamps. But the criteria is that i need to have only 50 unique values for a week. Later, continue the same 50 timestamps for the next week and so on..

Replies are listed 'Best First'.
Re^3: Newbie: Request help on creating CSV using timestamp data
by Tux (Canon) on Nov 22, 2013 at 14:43 UTC

    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
      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
Re^3: Newbie: Request help on creating CSV using timestamp data
by hippo (Bishop) on Nov 22, 2013 at 13:44 UTC

    Which version of Perl are you running? localtime returns a string when called in scalar context in the versions I use (<= 5.14). Perhaps you are using Time::Piece but have neglected to mention this?

    Regardless, if you are going to do date/time arithmetic I would heartily recommend using any one of the available modules to do that for you rather than re-invent the wheel (otherwise you will have to start worrying about DST and leap years, etc.). Try one of Date::Manip, DateTime, Time::Piece, etc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-24 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found