SR has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Date Manipulation
by virtualsue (Vicar) on Dec 17, 2006 at 13:08 UTC
    Your title is very close to the name of a well-known module for working with dates. I can also tell you that what you want is as easy (or Ez) as falling off a log.

    This site offers all sorts of help for students. If you don't find something useful in Tutorials, try Super Search.

Re: Date Manipulation
by jettero (Monsignor) on Dec 17, 2006 at 13:05 UTC
    Short answer: Date::Manip.

    Longer answer: my $date1= &UnixDate( &ParseDate("friday 3pm 2 weeks ago"), '%Y-%m-%d %H:%M:%S' );

    -Paul

Re: Date Manipulation
by brian_d_foy (Abbot) on Dec 17, 2006 at 21:00 UTC

    Of course there is a way to do this. Computers are pretty smart nowadays. :)

    You might try a Google Codesearch for routines called yesterday, or even just a normal googling of search.cpan.org for yesterday

    Since this sounds a lot like a homework problem, you probaby won't be able to get away with using Date::Simple:

    use Date::Simple qw(today); $date = today() - 1; print $date;

    or Date::Manip:

    use Date::Manip; $date = ParseDate("yesterday"); print $date;

    or Date::PeriodParser:

    use Date::PeriodParser; $date = parse_period("yesterday"); print scalar localtime( $date );

    or many other modules which can handle this sort of thing. You can always look at their source to see how they did it.

    Good luck :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review

      If you disregard timezones, my favourite method of calculating yesterday's date is the following:

      my $yesterday = localtime; sleep( 86400 ); print "Yesterdays date is $yesterday\n";

      People might complain that the runtime is a bit too long, but I'm sure one can reduce the runtime by rewriting this program in C, Haskell or Perl6.

        Yep. Exactly, 'sleep' subroutine should be rewritten for better performance...
Re: Date Manipulation
by swampyankee (Parson) on Dec 18, 2006 at 01:12 UTC

    As brian_d_foy said, "yes". There are numerous ways to do it.

    Basically, find out today's date, and subtract 1 day. If this results in a day number of 0, subtract 1 from the month, and reset the day to the last day yesterday's month. If the new month is 0 (assuming January is 1), subract 1 day from the year, and set the month to December.

    In principle, this is no different than any other sort of arithmetic involving borrowing and carrying.

    So, you need one array: the number of days in each month (remembering that February sometimes has 29, not 28, days), the logic for determining leap year, (hint: 1900 was not a leap year), and the logic for when month and year boundaries are crossed.

    sub is_leap_year { my $year = shift; if($year%100 == 0) { return 1 if $year%400 == 0; } else{ return 1 if($year%4 == 0); } return undef; }

    Or you could use something like this, in the spirit of Corion's response):

    $today = localtime; sleep(-86400); $yesterday = localtime; print "Yesterday's date was $yesterday\n";

    This will use less wall clock time than Corion's solution, but it requires quite a lot of CPU time, or access to a naked singularity. Even so, sleeping for negative time periods is supported on very few O/S; you may need to install the Acme::Time::Reversal module from CPAN.

    <

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

      The DST-safe algorithm I use for calculating the daydate of yesterday or other relative date descriptions is to add/subtract 22 hours from the current gmtime() or localtime() until the date or day of week changes - this is a much simpler (and easier to debug) loop than maintaining any carry.

        I'm lazy. In Perl, I use one of the date modules. In Fortran -- where I did implement something like this -- DST adds another layer of horror that I contemplated and decided to ignore; nobody where I work is likely to be working between 6pm and 7am, so DST wasn't an issue. My general opinion of DST is f*** it, in any case.

        emc

        At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

        —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.