using perl to print yesterday's date, but with formatting options ?

Discussion in 'Perl Misc' started by Tom Van Overbeke, Oct 31, 2005.

  1. Hi,

    I need a way to use yesterday's date in a variable. The tricks with 'date'
    don't work on my system, but perl does.

    so i've got:
    perl -le 'print scalar localtime time - 86400' which outputs:
    Sun Oct 30 17:56:52 2005

    But the format I need is: 30.10.05

    Any ideas on how to add this to the above piece of code ?

    thanks,

    tom.
     
    Tom Van Overbeke, Oct 31, 2005
    #1
    1. Advertising

  2. Re: using perl to print yesterday's date, but with formatting options?

    Tom Van Overbeke wrote:
    > I need a way to use yesterday's date in a variable. The tricks with 'date'
    > don't work on my system, but perl does.
    >
    > so i've got:
    > perl -le 'print scalar localtime time - 86400' which outputs:
    > Sun Oct 30 17:56:52 2005
    >
    > But the format I need is: 30.10.05
    >
    > Any ideas on how to add this to the above piece of code ?


    Capture the date string in a variable and use the tr/// operator.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 31, 2005
    #2
    1. Advertising

  3. Re: using perl to print yesterday's date, but with formatting options?

    Gunnar Hjalmarsson wrote:
    > Tom Van Overbeke wrote:
    >
    >> I need a way to use yesterday's date in a variable. The tricks with
    >> 'date' don't work on my system, but perl does.
    >>
    >> so i've got:
    >> perl -le 'print scalar localtime time - 86400' which outputs:
    >> Sun Oct 30 17:56:52 2005
    >>
    >> But the format I need is: 30.10.05
    >>
    >> Any ideas on how to add this to the above piece of code ?

    >
    > Capture the date string in a variable and use the tr/// operator.


    Sorry, didn't read the question carefully enough.

    Either you can get the date components by evaluating localtime() in list
    context

    perldoc -f localtime

    and create the date string you want 'by hand', or you can use one of the
    many date modules.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 31, 2005
    #3
  4. Tom Van Overbeke

    John Bokma Guest

    Purl Gurl <> wrote:

    > Purl Gurl wrote:
    >> Tom Van Overbeke wrote:

    >
    > (snipped)
    >
    >>> But the format I need is: 30.10.05

    >
    >> print $Array[3], ".", $Array[4] + 1, ".", $Array[5] + 1900;

    >
    >
    > #!perl
    >
    > @Array = localtime(time - 86400);
    >
    > print $Array[3], ".", $Array[4] + 1, ".0", $Array[5] - 100;


    Which gives a cool bug in a few years.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Oct 31, 2005
    #4
  5. At 2005-10-31 11:57AM, Tom Van Overbeke <> wrote:
    > Hi,
    >
    > I need a way to use yesterday's date in a variable. The tricks with 'date'
    > don't work on my system, but perl does.
    >
    > so i've got:
    > perl -le 'print scalar localtime time - 86400' which outputs:


    subtracting a fixed number of seconds is guaranteed to break twice a
    year, for places that have daylight savings.

    > But the format I need is: 30.10.05


    use Date::Calc qw(Today Add_Delta_Days);
    my ($year, $month, $day) = Add_Delta_Days(Today(), -1);
    printf "%02d.%02d.%02d", $day, $month, substr($year, 2, 2);

    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Oct 31, 2005
    #5
  6. Tom Van Overbeke

    Joe Smith Guest

    Re: using perl to print yesterday's date, but with formatting options?

    Purl Gurl wrote:
    > ($day, $month, $year) = (localtime(time - 86400)) [3,4,5];
    > print $day, ".", $month + 1, ".0", $year - 100;


    If I put your solution into a program, will it work properly
    four and a half years from now? The year after "09" is not "010".
    -Joe
     
    Joe Smith, Oct 31, 2005
    #6
  7. Tom Van Overbeke

    Mothra Guest

    Glenn Jackman wrote:
    > subtracting a fixed number of seconds is guaranteed to break twice a
    > year, for places that have daylight savings.


    Agreed, That's why there is DateTime :)

    >
    >> But the format I need is: 30.10.05

    >
    > use Date::Calc qw(Today Add_Delta_Days);
    > my ($year, $month, $day) = Add_Delta_Days(Today(), -1);
    > printf "%02d.%02d.%02d", $day, $month, substr($year, 2, 2);


    Or the DateTime version

    use strict;
    use warnings;
    use DateTime;
    use DateTime::Format::Strptime;

    my $Strp = new DateTime::Format::Strptime(
    pattern => '%d.%m.%y',
    );

    my $dt = DateTime->now()->subtract( days => 1 );


    print $Strp->format_datetime($dt);
     
    Mothra, Oct 31, 2005
    #7
  8. Tom Van Overbeke

    Paul Lalli Guest

    Purl Gurl wrote:
    > #!perl
    >
    > ($day, $month, $year) = (localtime(time - 86400)) [3,4,5];
    >
    > print $day, ".", $month + 1, ".0", $year - 100;


    Wow, PurlGurl has discovered the Year 2010 bug.

    printf ("%02d.%02d.%02d", $day, $month+1, $year % 100);

    Paul Lalli
     
    Paul Lalli, Oct 31, 2005
    #8
  9. Purl Gurl <> wrote in news:43667F2C.5010708
    @purlgurl.net:

    > Purl Gurl wrote:
    >> Purl Gurl wrote:
    >>> Tom Van Overbeke wrote:

    >
    > (snipped)
    >
    >>>> But the format I need is: 30.10.05

    >
    >>> print $Array[3], ".", $Array[4] + 1, ".", $Array[5] + 1900;

    >
    >> print $Array[3], ".", $Array[4] + 1, ".0", $Array[5] - 100;

    >
    > Somewhat obfuscated but named variables help with clarity
    > in using different formats, as you request.
    >
    > #!perl
    >
    > ($day, $month, $year) = (localtime(time - 86400)) [3,4,5];
    >
    > print $day, ".", $month + 1, ".0", $year - 100;
    >
    > print "\n\n";
    >
    > print $month + 1, "-", $day, "-", $year + 1900;


    This is your umpteenth wrong "solution" you have posted on this thread.

    Assuming the OP really does want two digit years, there are quite a few
    ways of doing this correctly.

    The problem has two parts: (1) Correctly figure out yesterday's date,
    (2) Print it correctly in a way that does not become invalid in 5 years.
    I hate two digit dates with a passion as well as ambiguous date formats,
    but since the OP asked for two digit dates, here is one way of doing it:

    On (1), let's assume that subtracting 86400 seconds from current time is
    the correct procedure for getting yesterday's date (I am sure there are
    issues that messes this up in some cases).

    As for (2), this is the perfect reason to use printf:

    use strict;
    use warnings;

    my ($day, $month, $year) = (localtime(time - 86400)) [3,4,5];
    printf "%2.2d.%2.2d.%2.2d\n", $day, $month + 1, $year % 100;

    __END__

    On the other hand, the OP should be made aware of POSIX::strftime as
    well.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 31, 2005
    #9
  10. Tom Van Overbeke

    Mothra Guest

    A. Sinan Unur wrote:
    (snipped)
    > On (1), let's assume that subtracting 86400 seconds from current time
    > is the correct procedure for getting yesterday's date (I am sure
    > there are issues that messes this up in some cases).


    You are correct, DST will mess this up leap seconds will as
    well. In cases like this it is best to use one of the Date/Time
    modules on CPAN. like DateTime :)


    Mothra
     
    Mothra, Oct 31, 2005
    #10
  11. "Mothra" <> wrote in news:43669114$1
    @usenet.ugs.com:

    > A. Sinan Unur wrote:
    > (snipped)
    >> On (1), let's assume that subtracting 86400 seconds from current time
    >> is the correct procedure for getting yesterday's date (I am sure
    >> there are issues that messes this up in some cases).

    >
    > You are correct, DST will mess this up leap seconds will as
    > well. In cases like this it is best to use one of the Date/Time
    > modules on CPAN. like DateTime :)


    Thanks, especially since I had forgotten about DateTime. Yes, I have used
    that module for this particular reason, but forgot to mention it.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 31, 2005
    #11
  12. Glenn Jackman <> wrote:

    > At 2005-10-31 11:57AM, Tom Van Overbeke <> wrote:
    >> Hi,
    >>
    >> I need a way to use yesterday's date in a variable. The tricks
    >> with 'date' don't work on my system, but perl does.
    >>
    >> so i've got:
    >> perl -le 'print scalar localtime time - 86400' which outputs:

    >
    > subtracting a fixed number of seconds is guaranteed to break twice
    > a year, for places that have daylight savings.
    >
    >> But the format I need is: 30.10.05

    >
    > use Date::Calc qw(Today Add_Delta_Days);
    > my ($year, $month, $day) = Add_Delta_Days(Today(), -1);
    > printf "%02d.%02d.%02d", $day, $month, substr($year, 2, 2);


    printf "%02d.%02d.%02d", $day, $month, $year % 100;

    Y10K bug. Your code might be in use longer than you think. :)
     
    David K. Wall, Oct 31, 2005
    #12
  13. Tom Van Overbeke

    Guest

    A. Sinan Unur <> wrote:
    > On (1), let's assume that subtracting 86400 seconds from current time is
    > the correct procedure for getting yesterday's date (I am sure there are
    > issues that messes this up in some cases).


    One is the daylight savings switch - for those time zones that have
    such a thing: the test will fail during the last hour of the 25 hour
    day. Another is the inclusion of occasional leap seconds: this test will
    fail during the last second of 2005.

    Chris
     
    , Oct 31, 2005
    #13
  14. Tom Van Overbeke

    Guest

    Tom Van Overbeke wrote:
    > I need a way to use yesterday's date in a variable.
    > ...
    > But the format I need is: 30.10.05


    use Date::Manip;
    my $yesterday = UnixDate('yesterday', "%d.$m.$y");

    --
    http://DavidFilmer.com
     
    , Oct 31, 2005
    #14
  15. Tom Van Overbeke

    Guest

    wrote:
    > my $yesterday = UnixDate('yesterday', "%d.$m.$y");


    sorry, make that

    use Date::Manip;
    my $yesterday = UnixDate('yesterday', "%d.%m.%y");

    > --
    > http://DavidFilmer.com
     
    , Oct 31, 2005
    #15
  16. Tom Van Overbeke

    Mothra Guest

    Purl Gurl wrote:
    > C:\APACHE\USERS\TEST>perl -MPOSIX -wle "print strftime '%d.%m.%y' =>
    > localtime time - 86400"
    > m.05


    Odd, it works on my Win32 system
    Z:\>perl -MPOSIX -wle "print strftime '%d.%m.%y' => localtime time - 86400"
    30.10.05

    Z:\>perl -v

    This is perl, v5.8.6 built for MSWin32-x86-multi-thread
    (with 3 registered patches, see perl -V for more detail)

    It may be you are running an older verison of Perl?

    >
    >
    > No surprise you are not being trolled for not compensating for
    > Daylight Savings Time.


    In my example I used DateTime suite of modules. It supports
    both DST conversion and leap seconds. You may want to take a
    look at the project
    http://datetime.perl.org

    Hope this helps

    Mothra
     
    Mothra, Nov 1, 2005
    #16
  17. Purl Gurl <> wrote in
    news::

    > Abigail wrote:
    >> Tom Van Overbeke wrote:

    >
    > (snipped)
    >
    >> $ perl -MPOSIX -wle 'print strftime "%d.%m.%y" => localtime time
    >> - 86400' 30.10.05

    >
    > For trivia, your method results for Win32. Readers are cautioned POSIX
    > is buggy; results will vary greatly depending on operating system.
    >
    > C:\APACHE\USERS\TEST>perl -MPOSIX -wle "print strftime '%d.%m.%y' =>
    > localtime time - 86400"
    >
    > m.05


    D:\Home\asu1\UseNet\clpmisc> perl -MPOSIX -wle "print strftime q{%d.%m.%
    y} => localtime time - 86400"
    30.10.05

    Hmmmm ...

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 1, 2005
    #17
  18. Tom Van Overbeke

    Guest

    wrote:
    > A. Sinan Unur <> wrote:
    >> On (1), let's assume that subtracting 86400 seconds from current time is
    >> the correct procedure for getting yesterday's date (I am sure there are
    >> issues that messes this up in some cases).


    > One is the daylight savings switch - for those time zones that have
    > such a thing: the test will fail during the last hour of the 25 hour
    > day.


    And for the first hour of the day following the 23 hour day.

    Axel
     
    , Nov 1, 2005
    #18
  19. Tom Van Overbeke

    Guest

    Purl Gurl wrote:

    > All-in-all, you have yet to receive any intelligent responses
    > regarding Daylight Savings Time.
    >
    > On Daylight Savings Time, you will never write code which
    > will work correctly.


    I beg to differ on both points. I posted this reply:
    http://tinyurl.com/8uwwb. It works for daylight savings time, and leap
    years, and Y10K, and years ending in "00" and it won't necessarily
    break in Autumn of 2037, and everything else.

    Simple:

    use Date::Manip;
    my $yesterday = UnixDate('yesterday', "%d.%m.%y");

    If this solution does not meet the OP's requirements while avoiding the
    pitfalls of cronowackiness, I'd like to know in what scenario it would
    fail.

    --
    http://DavidFilmer.com
     
    , Nov 1, 2005
    #19
  20. Tom Van Overbeke

    Mothra Guest

    wrote:
    > Purl Gurl wrote:
    >
    >> All-in-all, you have yet to receive any intelligent responses
    >> regarding Daylight Savings Time.
    >>
    >> On Daylight Savings Time, you will never write code which
    >> will work correctly.

    >
    > I beg to differ on both points. I posted this reply:
    > http://tinyurl.com/8uwwb. It works for daylight savings time, and leap

    ^^^^^^^^^^^^^^^
    I don't know about that. from the docs:

    KNOWN BUGS

    Daylight Savings Times

    Date::Manip does not handle daylight savings time,
    though it does handle timezones to a certain extent.
    Converting from EST to PST works fine.
    Going from EST to PDT is unreliable.

    Another reason to use DateTime. DateTime will handle
    DST changes, leap seconds, leap years, other calender systems
    and much more :)

    Hope this helps

    Mothra
     
    Mothra, Nov 1, 2005
    #20
    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. -vr_stupids

    get "yesterday's" date in iso format

    -vr_stupids, Jan 23, 2004, in forum: Python
    Replies:
    4
    Views:
    4,177
    -vr_stupids
    Jan 24, 2004
  2. Thierry Lam

    Finding yesterday's date with datetime

    Thierry Lam, May 15, 2006, in forum: Python
    Replies:
    2
    Views:
    503
  3. Hussein B
    Replies:
    1
    Views:
    263
    Simon Brunning
    Jan 20, 2009
  4. Tim Golden
    Replies:
    0
    Views:
    545
    Tim Golden
    Oct 22, 2009
  5. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    434
    D'Arcy J.M. Cain
    Oct 22, 2009
Loading...

Share This Page