[newbie] Year in two digits; Time with leading zero

Discussion in 'Perl Misc' started by Gilles Ganault, May 18, 2007.

  1. Hello

    As indicated in the subject line, I don't Perl, but I must
    write a short script in it.

    I just need to get the current year in two digits, and time should be
    HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.

    Here's what I found:

    ------------------------------
    ($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];

    #BAD : 2007
    $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year+1900);
    #BAD : 107!
    $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year);
    print $currentdate . "\n";

    #BAD : 10:1 instead of 10:01
    $currenttime = sprintf("%d:%d", $hrs,$min);
    print $currenttime . "\n";
    ------------------------------

    Should I use an other library?

    Thank you.
    Gilles Ganault, May 18, 2007
    #1
    1. Advertising

  2. Gilles Ganault

    Thomas J. Guest

    Re: Year in two digits; Time with leading zero

    Youre using "leading zero"-format for $currentdate, why not for
    $currenttime ?
    for 2-digit-date use the modulo-function (%)

    $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year % 100);
    $currenttime = sprintf("%02d:%02d", $hrs,$min);

    hth, Thomas
    Thomas J., May 18, 2007
    #2
    1. Advertising

  3. Gilles Ganault wrote:

    > Hello
    >
    > As indicated in the subject line, I don't Perl, but I must
    > write a short script in it.
    >
    > I just need to get the current year in two digits, and time should be
    > HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.
    >
    > Here's what I found:
    >
    > ------------------------------
    > ($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];
    >
    > #BAD : 2007
    > $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year+1900);
    > #BAD : 107!
    > $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year);
    > print $currentdate . "\n";
    >
    > #BAD : 10:1 instead of 10:01
    > $currenttime = sprintf("%d:%d", $hrs,$min);
    > print $currenttime . "\n";
    > ------------------------------
    >
    > Should I use an other library?
    >
    > Thank you.


    You could "use POSIX" and make use of the standard strftime() function
    assuming you are on a POSIX compliant system where that works.

    But there's nothing wrong with your method either - I'd use either with no
    sense that one is better or worse than the other. Your method is definately
    portable, which I seldom care about because I work with nothing but *nix,
    but I'm an old grumbleweed and portability is good...Honest :)

    Cheers

    Tim
    Tim Southerwood, May 18, 2007
    #3
  4. Gilles Ganault

    Dr.Ruud Guest

    Gilles Ganault schreef:

    > As indicated in the subject line, I don't Perl, but I must
    > write a short script in it.
    >
    > I just need to get the current year in two digits, and time should be
    > HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.
    >
    > Here's what I found:
    >
    > ------------------------------
    > ($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];
    >
    > #BAD : 2007
    > $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year+1900);
    > #BAD : 107!
    > $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year);
    > print $currentdate . "\n";


    How about "$year % 100"?


    > #BAD : 10:1 instead of 10:01
    > $currenttime = sprintf("%d:%d", $hrs,$min);
    > print $currenttime . "\n";


    You forgot the "02" parts:

    $currenttime = sprintf("%02d:%02d", $hrs, $min);


    #!/usr/bin/perl
    use strict;
    use warnings;

    my ($min, $hrs, $day, $month, $year) = (localtime)[1..5];

    my $currentdate = sprintf( "%02d/%02d/%02d",
    $day,
    $month + 1,
    $year % 100,
    );

    print "$currentdate\n";

    my $currenttime = sprintf( "%02d:%02d",
    $hrs,
    $min,
    );
    print "$currenttime\n";
    __END__

    Consider the ISO 8601 date format:
    http://en.wikipedia.org/wiki/ISO_8601

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 18, 2007
    #4
  5. On Fri, 18 May 2007 11:18:45 +0200, "Dr.Ruud"
    <> wrote:
    >How about "$year % 100"?

    [...]
    >You forgot the "02" parts:


    That did it. Thanks!
    Gilles Ganault, May 18, 2007
    #5
  6. Gilles Ganault

    Tony Curtis Guest

    Gilles Ganault wrote:
    > Hello
    >
    > As indicated in the subject line, I don't Perl, but I must
    > write a short script in it.
    >
    > I just need to get the current year in two digits, and time should be
    > HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.


    I'd go with strftime from POSIX, e.g.

    use POSIX qw( strftime );

    my $now = strftime( '%y %H:%M', localtime() );

    print "$now\n";

    Nicely mnemonic solution.

    hth
    t
    Tony Curtis, May 18, 2007
    #6
  7. On 2007-05-18 14:13, Petr Vileta <> wrote:
    > "Gilles Ganault" <> píse v diskusním príspevku
    > news:...
    >> I just need to get the current year in two digits,

    >
    > This is simple:
    >
    > ($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];
    > $currentdate = sprintf("%02d/%02d/%02d", $day, $month+1,
    > substr($year,-2,2));

    ^^^^^^^^^^^^^^^^^^
    That's just plain ugly. What's wrong with ($year % 100)?

    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 18, 2007
    #7
  8. On Fri, 18 May 2007 10:40:32 -0400, Tony Curtis
    <> wrote:
    >I'd go with strftime from POSIX, e.g.


    Thanks for the alternative solution.
    Gilles Ganault, May 20, 2007
    #8
    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. tshad

    add leading zero

    tshad, Nov 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    13,252
    tshad
    Nov 9, 2005
  2. james
    Replies:
    2
    Views:
    872
    Bengt Richter
    Dec 2, 2003
  3. John Nagle
    Replies:
    4
    Views:
    584
    James Antill
    Aug 7, 2007
  4. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    562
    Delaney, Timothy (Tim)
    Aug 7, 2007
  5. David Mathog
    Replies:
    13
    Views:
    957
    Keith Thompson
    Jun 13, 2011
Loading...

Share This Page