Perl Calendar Question

Discussion in 'Perl Misc' started by amerar@iwc.net, Jun 1, 2005.

  1. Guest

    Hi All,

    I need to write some Perl code where I take the current day, subtract 3
    from it, and get the proper day of the month.

    So, if today is June 15th, then 15-3 would give me June 12th.
    But if today is July 1st, then 1-3 'should' give me June 28th.
    And then there are leap years......

    Is there any code to do this? I'm not sure how to write something like
    this......

    Thanks,

    Arthur
     
    , Jun 1, 2005
    #1
    1. Advertising

  2. mothra Guest

    wrote:
    > Hi All,
    >
    > I need to write some Perl code where I take the current day, subtract
    > 3 from it, and get the proper day of the month.
    >

    use strict;
    use warnings;
    use DateTime;
    use DateTime::Duration;

    my $d = DateTime::Duration->new (
    days => 3
    );

    my $dt = DateTime->today();
    my $new_dt = $dt -$d;

    print $new_dt;

    Hope this helps

    Mothra
     
    mothra, Jun 1, 2005
    #2
    1. Advertising

  3. wrote:

    > I need to write some Perl code where I take the current day, subtract 3
    > from it, and get the proper day of the month.
    >
    > So, if today is June 15th, then 15-3 would give me June 12th.
    > But if today is July 1st, then 1-3 'should' give me June 28th.
    > And then there are leap years......
    >
    > Is there any code to do this?


    Take a look on CPAN - I'm sure at several of the Date::* modules will do
    this. I'd probably use Date::Manip but it's quite possibly overkill.
     
    Brian McCauley, Jun 1, 2005
    #3
  4. Guest

    wrote:
    > Hi All,
    >
    > I need to write some Perl code where I take the current day, subtract 3
    > from it, and get the proper day of the month.
    >
    > So, if today is June 15th, then 15-3 would give me June 12th.
    > But if today is July 1st, then 1-3 'should' give me June 28th.
    > And then there are leap years......
    >
    > Is there any code to do this? I'm not sure how to write something like
    > this......
    >
    > Thanks,
    >
    > Arthur


    #!/usr/bin/perl

    use strict;
    use warnings;

    my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
    my $mday = $lt[3];
    print $mday."\n";

    got my info from perldoc -f localtime

    wana
     
    , Jun 1, 2005
    #4
  5. wrote:

    > my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
    > my $mday = $lt[3];


    You could have used a list slice.

    my $mday = (localtime(time - 60*60*24*3))$lt[3];

    But this is one of the classic mistakes. Not all days are 24h. In most
    places in the world there is one 23h day and one 25h day each year.
     
    Brian McCauley, Jun 1, 2005
    #5
  6. Brian McCauley wrote:
    > wrote:
    >> my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
    >> my $mday = $lt[3];

    >
    > You could have used a list slice.
    >
    > my $mday = (localtime(time - 60*60*24*3))$lt[3];
    >
    > But this is one of the classic mistakes. Not all days are 24h. In most
    > places in the world there is one 23h day and one 25h day each year.


    True. One way to take DST into consideration only using a standard module:

    use Time::Local;
    my ($d, $m, $y) = ( localtime timelocal( 0, 0, 12,
    (localtime $^T)[3..5] ) - 3 * 86400 )[3..5];
    printf "%d-%02d-%02d\n", $y+1900, $m+1, $d;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 1, 2005
    #6
  7. Brian McCauley wrote:
    > wrote:
    >
    >> my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
    >> my $mday = $lt[3];

    >
    > You could have used a list slice.
    >
    > my $mday = (localtime(time - 60*60*24*3))$lt[3];

    ^^^
    Don't you mean:

    my $mday = (localtime(time - 60*60*24*3))[3];


    :)

    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 1, 2005
    #7
  8. Gunnar Hjalmarsson wrote:

    > Brian McCauley wrote:
    >
    >> wrote:
    >>
    >>> my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
    >>> my $mday = $lt[3];

    >
    >> But this is one of the classic mistakes. Not all days are 24h. In
    >> most places in the world there is one 23h day and one 25h day each year.

    >
    > True. One way to take DST into consideration only using a standard module:
    >
    > use Time::Local;


    Simpler is to rely on the fact thar DST transition takes place at night,
    not the middle of the day:

    my $now = time;
    my $hour = (localtime $now)[2];
    my $middayish = $now + ( 12 - $hour ) * 60 * 60;
    my $three_days_ago = $middayish - 3 * 24 * 60 * 60;
    my $mday = (localtime $three_days_ago)[3];

    Of course, in practice, you'd not have so many intermediate variables.
     
    Brian McCauley, Jun 2, 2005
    #8
  9. Brian McCauley wrote:
    > Gunnar Hjalmarsson wrote:
    >> One way to take DST into consideration only using a standard
    >> module:
    >>
    >> use Time::Local;

    >
    > Simpler is to rely on the fact thar DST transition takes place at night,
    > not the middle of the day:


    Well, that is just what I did as well.

    my ($d, $m, $y) = ( localtime timelocal( 0, 0, 12,
    (localtime $^T)[3..5] ) - 3 * 86400 )[3..5];
    printf "%d-%02d-%02d\n", $y+1900, $m+1, $d;

    > my $now = time;
    > my $hour = (localtime $now)[2];
    > my $middayish = $now + ( 12 - $hour ) * 60 * 60;
    > my $three_days_ago = $middayish - 3 * 24 * 60 * 60;
    > my $mday = (localtime $three_days_ago)[3];


    Only that your way does not make use of a module.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 2, 2005
    #9
  10. Ala Qumsieh Guest

    Brian McCauley wrote:
    > wrote:
    >
    >> I need to write some Perl code where I take the current day, subtract 3
    >> from it, and get the proper day of the month.
    >>
    >> So, if today is June 15th, then 15-3 would give me June 12th.
    >> But if today is July 1st, then 1-3 'should' give me June 28th.
    >> And then there are leap years......
    >>
    >> Is there any code to do this?

    >
    >
    > Take a look on CPAN - I'm sure at several of the Date::* modules will do
    > this. I'd probably use Date::Manip but it's quite possibly overkill.


    I have used Date::Calc before for the exact same reason. Works great.

    --Ala
     
    Ala Qumsieh, Jun 2, 2005
    #10
  11. Gunnar Hjalmarsson wrote:

    > Brian McCauley wrote:
    >>
    >> Simpler is to rely on the fact thar DST transition takes place at
    >> night, not the middle of the day:

    >
    > Well, that is just what I did as well.


    Er, yes, so you did. (blush).
     
    Brian McCauley, Jun 3, 2005
    #11
    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. dpackwood
    Replies:
    3
    Views:
    1,865
  2. Shevek
    Replies:
    3
    Views:
    6,123
    Shevek
    Jun 23, 2004
  3. Shevek
    Replies:
    0
    Views:
    426
    Shevek
    Jun 23, 2004
  4. Parthiv Joshi
    Replies:
    1
    Views:
    816
    Samuel L Matzen
    Jul 6, 2004
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    334
    PerlFAQ Server
    Jan 23, 2011
Loading...

Share This Page