Small date question

Discussion in 'Perl Misc' started by Tuxedo, Sep 3, 2009.

  1. Tuxedo

    Tuxedo Guest

    I would like to return the date and use in daily file names as in
    somefile-2009-09-03.txt etc. Anyway. the date part is where I got stuck....

    Where in a Bash shell I would simply do: ...

    date +%Y-%m-%d

    .... to return 2009-09-03

    How could the same be done in a fairly short Perl code and without use of
    modules? (I guess by the localtime builtin procedures).

    Any examples would be much appreciated.

    Thanks,
    Tudedo
    Tuxedo, Sep 3, 2009
    #1
    1. Advertising

  2. Tuxedo <> wrote:
    >I would like to return the date and use in daily file names as in
    >somefile-2009-09-03.txt etc. Anyway. the date part is where I got stuck....
    >
    >Where in a Bash shell I would simply do: ...
    >
    >date +%Y-%m-%d
    >
    >... to return 2009-09-03
    >
    >How could the same be done in a fairly short Perl code


    One way:
    use POSIX qw(strftime);
    my $today = strftime "%Y-%m-%d", localtime;

    >and without use of modules?


    Why would you want to tie your hands behind your back and wear a
    blindfold?

    >(I guess by the localtime builtin procedures).


    Oh, if you already knew the answer, then why were you asking?

    Yes, indeed you could do it that way if you absolutely wanted to
    reinvent the wheel. Just capture the 4th, 5th and 6th elements of the
    return value of localtime, adjust for the different starting values as
    described in the man page of localtime, and use sprintf to enforce your
    desired 4-2-2 digit format.

    jue
    Jürgen Exner, Sep 3, 2009
    #2
    1. Advertising

  3. Tuxedo

    Tuxedo Guest

    Ben Morrow wrote:

    [...]

    > just points you to the C documentation for the same function; if you are
    > on a system that doesn't have C documentation installed, it's easy to
    > find it on the Web.


    Thanks for pointing this out!
    Tuxedo, Sep 3, 2009
    #3
  4. Tuxedo

    Tuxedo Guest

    Tad J McClellan wrote:

    [...]


    > sub today {
    > my($day, $mon, $year) = (localtime)[3,4,5];
    >
    > return sprintf "%4d-%02d-%02d", $year+1900, $mon+1, $day;
    > } # end sub today
    >
    >


    Thanks for this example, it works perfectly, and is just what I needed!
    Tuxedo, Sep 3, 2009
    #4
  5. Tuxedo

    Tuxedo Guest

    Jürgen Exner wrote:

    [...]

    > One way:
    > use POSIX qw(strftime);
    > my $today = strftime "%Y-%m-%d", localtime;
    >
    > >and without use of modules?

    >
    > Why would you want to tie your hands behind your back and wear a
    > blindfold?


    I mean modules that aren't included in perl as pre-installed standard
    modules. If a module is already included, that's good. But I don't want the
    hassle of installing a variety of modules for only small script procedures
    unless it is really needed, for easier portability.

    > >(I guess by the localtime builtin procedures).

    >
    > Oh, if you already knew the answer, then why were you asking?
    >


    Far from being well versed in perl, I wasn't sure and wanted suggestions by
    those who know, like yourself. The only way to know by experience without
    having the experience :)

    I definitely didn't know the answer. I only guessed.

    > Yes, indeed you could do it that way if you absolutely wanted to
    > reinvent the wheel. Just capture the 4th, 5th and 6th elements of the
    > return value of localtime, adjust for the different starting values as
    > described in the man page of localtime, and use sprintf to enforce your
    > desired 4-2-2 digit format.


    Thanks for this and the above POSIX example!
    Tuxedo, Sep 3, 2009
    #5
    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. Matt
    Replies:
    1
    Views:
    600
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,251
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    515
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    720
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    405
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page