Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    is this the best way to get 'today'

    Hi,

    As you likely know, I am rubbish with posix etc.

    Code:
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
    
    $mday = sprintf "%02d", $mday;
    $mon = ($mon + 1);
    $mon = sprintf "%02d", $mon;
    $year = ($year + 1900);
    
    my $today = (join ( '-' , ( $year, $mon, $mday )));
    
    print qq( today = $today);
    Is there a better way than this?

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Code:
    use POSIX qw/strftime/;
    
    my $today = strftime("%Y-%m-%d", localtime);
    print qq( today = $today);

  • #3
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Quote Originally Posted by bazz View Post
    Hi,

    As you likely know, I am rubbish with posix etc.

    Code:
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
    
    $mday = sprintf "%02d", $mday;
    $mon = ($mon + 1);
    $mon = sprintf "%02d", $mon;
    $year = ($year + 1900);
    
    my $today = (join ( '-' , ( $year, $mon, $mday )));
    
    print qq( today = $today);
    Is there a better way than this?

    bazz
    You might be bad with POSIX, but even your code can be considerably reduced with no loss of clarity:

    Code:
    my ($mday, $mon, $year) = (localtime(time))[3,4,5];
    my $today = sprintf( "%s-%02d-%02d", ( $year+1900, $mon+1, $mday ));
    print qq( today = $today);
    I am not sure but I think POSIX is tied into whatever the locale is so formats like "%Y-%m-%d" might vary from computer to computer. Maybe Fish knows for sure if it is or isn't or reading the POSIX documentation might tell.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    if you want GMT you can use gmtime

    Code:
    use POSIX qw(strftime);
    
    $now_string = strftime "%Y-%m-%d", gmtime;
    as Fismonger, I prefere strfime for formating,

    best regards

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thank you all.

    I have got it now and shall read up again on POSIX.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •