localtime(time())

Discussion in 'Perl Misc' started by Martin Trautmann, May 22, 2007.

  1. Hi all,

    what's wrong about my interpretation of localtime?

    perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
    my $date =($year+1900)."-".
    sprintf("%02d", $mon) ."-".
    sprintf("%02d", $mday);
    print $date;'

    .... returns 2007-04-22 instead of 2007-05-22.

    Do I need to increment the month count since Janury is counted as zero?

    Thanks,
    Martin
     
    Martin Trautmann, May 22, 2007
    #1
    1. Advertising

  2. Martin Trautmann wrote:
    > what's wrong about my interpretation of localtime?


    Interpretation? You didn't read the docs.

    perldoc -f localtime

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 22, 2007
    #2
    1. Advertising

  3. On Tue, 22 May 2007 14:55:27 +0200, Gunnar Hjalmarsson wrote:
    > Martin Trautmann wrote:
    > > what's wrong about my interpretation of localtime?

    >
    > Interpretation? You didn't read the docs.
    >
    > perldoc -f localtime


    Thanks, you're right - I checked cpan.org, but failed to check perldoc
    first.
     
    Martin Trautmann, May 22, 2007
    #3
  4. On 22 May 2007 12:16:09 GMT,
    Martin Trautmann <> wrote:
    >
    > Hi all,
    >
    > what's wrong about my interpretation of localtime?
    >
    > perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
    > my $date =($year+1900)."-".
    > sprintf("%02d", $mon) ."-".
    > sprintf("%02d", $mday);
    > print $date;'
    >
    > ... returns 2007-04-22 instead of 2007-05-22.
    >
    > Do I need to increment the month count since Janury is counted as zero?


    The documentation for localtime states:

    $ perldoc -f localtime

    [SNIP]
    $mday is the day of the
    month, and $mon is the month itself, in the range 0..11 with 0
    indicating January and 11 indicating December.
    [SNIP]

    I believe the answer to your question is in that bit.

    Did you know about perldoc? If not, perldoc -f reads the perlfunc
    documentation, and displays that part of that documentation file that
    pertains to the function you give it as an argument. You can, of course,
    simply read perlfuncdirectly. It's a lot faster than posting to Usenet.

    It's interesting that the variable list you use is identical to that
    from the documentation.

    Martien
    --
    |
    Martien Verbruggen | If it isn't broken, it doesn't have enough
    | features yet.
    |
     
    Martien verbruggen, May 22, 2007
    #4
  5. Martin Trautmann

    Tony Curtis Guest

    Martin Trautmann wrote:
    > Hi all,
    >
    > what's wrong about my interpretation of localtime?
    >
    > perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
    > my $date =($year+1900)."-".
    > sprintf("%02d", $mon) ."-".
    > sprintf("%02d", $mday);
    > print $date;'
    >
    > ... returns 2007-04-22 instead of 2007-05-22.
    >
    > Do I need to increment the month count since Janury is counted as zero?


    Well, you've answered your own question...but simpler is:

    use POSIX qw( strftime );

    print strftime '%Y-%m-%d', localtime();

    hth
    t
     
    Tony Curtis, May 22, 2007
    #5
  6. On Tue, 22 May 2007 23:11:14 +1000, Martien verbruggen wrote:
    > Did you know about perldoc? If not, perldoc -f reads the perlfunc
    > documentation, and displays that part of that documentation file that
    > pertains to the function you give it as an argument. You can, of course,
    > simply read perlfuncdirectly. It's a lot faster than posting to Usenet.


    I ftp'ed the file to a cgi place where I found the bug later on - thus I
    did not have access to perldoc.

    > It's interesting that the variable list you use is identical to that
    > from the documentation.


    It was taken from the documentation before...
    .... where I just had expected a different meaning. $years was ok
    (counting from 1900), while I was surprised by $mon, which was $mon--
    instead - good for array processing, but not what it looked like.

    Yes, shame on me for not reading the docu...

    Fortunately, I got extra feedback, showing the even better solution,

    Thanks,
    Martin
     
    Martin Trautmann, May 22, 2007
    #6
  7. On Tue, 22 May 2007 09:31:57 -0400, Tony Curtis wrote:
    > Well, you've answered your own question...but simpler is:
    >
    > use POSIX qw( strftime );
    >
    > print strftime '%Y-%m-%d', localtime();


    Thanks!
    Martin
     
    Martin Trautmann, May 22, 2007
    #7
  8. Martin Trautmann

    Uri Guttman Guest

    >>>>> "MT" == Martin Trautmann <> writes:


    MT> I ftp'ed the file to a cgi place where I found the bug later on - thus I
    MT> did not have access to perldoc.

    ever heard of perldoc.perl.org? or installing perl on your desktop so
    you can have perldoc locally? the excuse "i didn't have access to
    perldoc" is weaker than your dog eating your computer.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 22, 2007
    #8
  9. Martin Trautmann

    Sisyphus Guest

    "Uri Guttman" <> wrote in message
    news:...
    ..
    ..
    > the excuse "i didn't have access to
    > perldoc" is weaker than your dog eating your computer.


    Hah !!

    (I always blame someone else's dog :)

    Cheers,
    Rob
     
    Sisyphus, May 22, 2007
    #9
  10. On Tue, 22 May 2007 14:29:57 GMT, Uri Guttman wrote:
    > ever heard of perldoc.perl.org?


    no, not yet.

    Thanks,
    Martin
     
    Martin Trautmann, May 22, 2007
    #10
  11. Martin Trautmann

    Dr.Ruud Guest

    Martin Trautmann schreef:

    > my $date =($year+1900)."-".
    > sprintf("%02d", $mon) ."-".
    > sprintf("%02d", $mday);
    > print $date;'


    my $date = sprintf( q[%s-%02d-%02d],
    $y + 1900,
    $m + 1,
    $d,
    );
    print $date;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, May 22, 2007
    #11
  12. Martin Trautmann

    Brad Baxter Guest

    On May 22, 4:06 pm, "Dr.Ruud" <> wrote:
    > Martin Trautmann schreef:
    >
    > > my $date =($year+1900)."-".
    > > sprintf("%02d", $mon) ."-".
    > > sprintf("%02d", $mday);
    > > print $date;'

    >
    > my $date = sprintf( q[%s-%02d-%02d],
    > $y + 1900,
    > $m + 1,
    > $d,
    > );
    > print $date;
    >
    > --
    > Affijn, Ruud
    >
    > "Gewoon is een tijger."


    I've used a variant of the following for years.

    #!/usr/local/bin/perl -l
    use warnings;
    use strict;

    sub now {
    for( $_[0] ) {
    defined and /(.*?)yyyy(.*?)mm(.*?)dd(.*)/i and
    return sprintf( "$1%04d$2%02d$3%02d$4",
    sub { ( $_[5] + 1900, $_[4] + 1, $_[3] )
    }->( localtime ) );
    }
    scalar localtime;
    }

    print now;
    print now 'yyyy-mm-dd';
    print now 'YYYY/MM/DD';
    print now 'Year: yyyy, Month: mm, Day: dd'

    __END__
    Wed May 23 10:06:26 2007
    2007-05-23
    2007/05/23
    Year: 2007, Month: 05, Day: 23

    --
    Brad
     
    Brad Baxter, May 23, 2007
    #12
  13. Martin Trautmann

    Uri Guttman Guest

    >>>>> "BB" == Brad Baxter <> writes:

    BB> I've used a variant of the following for years.

    BB> sub now {
    BB> for( $_[0] ) {
    BB> defined and /(.*?)yyyy(.*?)mm(.*?)dd(.*)/i and
    BB> return sprintf( "$1%04d$2%02d$3%02d$4",
    BB> sub { ( $_[5] + 1900, $_[4] + 1, $_[3] )
    BB> }->( localtime ) );
    BB> }
    BB> scalar localtime;
    BB> }

    bletcherific!!

    what if you want a different format? days of week? use POSIX::strftime
    and you get all you do there and much more.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 23, 2007
    #13
  14. Tony Curtis wrote:
    > Well, you've answered your own question...but simpler is:
    >
    > use POSIX qw( strftime );
    >
    > print strftime '%Y-%m-%d', localtime();


    I'm not sure that using POSIX is simpler, since at least I would need to
    look up the applicable conversion specifiers.

    I would do:

    my ($d, $m, $y) = ( localtime )[3..5];
    printf '%d-%02d-%02d', $y+1900 , $m+1, $d;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 23, 2007
    #14
  15. Martin Trautmann

    Tony Curtis Guest

    Gunnar Hjalmarsson wrote:
    > Tony Curtis wrote:
    >> Well, you've answered your own question...but simpler is:
    >>
    >> use POSIX qw( strftime );
    >>
    >> print strftime '%Y-%m-%d', localtime();

    >
    > I'm not sure that using POSIX is simpler, since at least I would need to
    > look up the applicable conversion specifiers.
    >
    > I would do:
    >
    > my ($d, $m, $y) = ( localtime )[3..5];
    > printf '%d-%02d-%02d', $y+1900 , $m+1, $d;


    "Simpler" of course depends on the metric(s) you use to measure complexity,
    but I would definitely argue that strftime() is certainly much easier to
    read,
    especially *for other people*, and thus produces "simpler" code.

    It helps you solve a problem instead of coding a problem.

    hth
    t
     
    Tony Curtis, May 23, 2007
    #15
  16. Tony Curtis wrote:
    > Gunnar Hjalmarsson wrote:
    >> Tony Curtis wrote:
    >>> Well, you've answered your own question...but simpler is:
    >>>
    >>> use POSIX qw( strftime );
    >>>
    >>> print strftime '%Y-%m-%d', localtime();

    >>
    >> I'm not sure that using POSIX is simpler, since at least I would need
    >> to look up the applicable conversion specifiers.
    >>
    >> I would do:
    >>
    >> my ($d, $m, $y) = ( localtime )[3..5];
    >> printf '%d-%02d-%02d', $y+1900 , $m+1, $d;

    >
    > "Simpler" of course depends on the metric(s) you use to measure complexity,
    > but I would definitely argue that strftime() is certainly much easier to
    > read,
    > especially *for other people*, and thus produces "simpler" code.


    Working with localtime() and (s)printf() is simple enough even for
    beginner level Perl programmers. Whoelse would you be coding for? ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 24, 2007
    #16
  17. On 2007-05-24 01:09, Gunnar Hjalmarsson <> wrote:
    > Tony Curtis wrote:
    >> Gunnar Hjalmarsson wrote:
    >>> Tony Curtis wrote:
    >>>> Well, you've answered your own question...but simpler is:
    >>>>
    >>>> use POSIX qw( strftime );
    >>>>
    >>>> print strftime '%Y-%m-%d', localtime();
    >>>
    >>> I'm not sure that using POSIX is simpler, since at least I would need
    >>> to look up the applicable conversion specifiers.


    %Y, %m and %d are mnemonic enough that you probably only have to look
    %them up once (although you might confuse %m and %M). Some others are
    very obscure though.


    >>> I would do:
    >>>
    >>> my ($d, $m, $y) = ( localtime )[3..5];
    >>> printf '%d-%02d-%02d', $y+1900 , $m+1, $d;


    So you need to remember the order of fields in the list returned by
    localtime (quick, which field is day of the week?), that you have to add
    1 to the month and 1900 to the year. Plus it's two lines instead of one.


    >> "Simpler" of course depends on the metric(s) you use to measure
    >> complexity, but I would definitely argue that strftime() is certainly
    >> much easier to read, especially *for other people*, and thus produces
    >> "simpler" code.

    >
    > Working with localtime() and (s)printf() is simple enough even for
    > beginner level Perl programmers. Whoelse would you be coding for? ;-)


    Judging from the number of programs which thought that the year after
    1999 would be 19100 localtime() was obviously not simple enough for many
    programmers.

    hp

    --
    _ | Peter J. Holzer | I know I'd be respectful of a pirate
    |_|_) | Sysadmin WSR | with an emu on his shoulder.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
     
    Peter J. Holzer, May 26, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen
    Replies:
    1
    Views:
    1,218
    Purl Gurl
    May 19, 2004
  2. Gerry George

    Time::localtime problems

    Gerry George, May 26, 2004, in forum: Perl
    Replies:
    2
    Views:
    1,170
    Joe Smith
    May 26, 2004
  3. OleMacGeezer

    time.localtime() Format Question

    OleMacGeezer, Aug 20, 2006, in forum: Python
    Replies:
    1
    Views:
    471
    Greg Krohn
    Aug 20, 2006
  4. KW
    Replies:
    1
    Views:
    1,836
    Eric Sosman
    Dec 8, 2004
  5. Rich Bogle

    time configuration for localtime

    Rich Bogle, Apr 23, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    197
    Glenn Jackman
    Apr 23, 2004
Loading...

Share This Page