Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Timestamp of last monday

by vibeweb (Initiate)
on Nov 18, 2011 at 14:10 UTC ( #938835=perlquestion: print w/ replies, xml ) Need Help??
vibeweb has asked for the wisdom of the Perl Monks concerning the following question:

Hello
I'm tumbling with some stats script someone else have made some long time ago, and i need to modify it a bit

The specific part is where the timestamps used in the later database query is set.

In my current code this is defined:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(tim +e); $start_date = sprintf("%4d%02d%02d000000",$year+1900,$mon+1,$mday);

where $start_date returns the current date at 00:00:00 like "20111118000000"

I need to get it to return the date of the last monday (so if the script is run on a monday, it returns the date from the week before).

I've tried various of localtime, tried substract from the $day, but that could result in a negative and so on.

Comment on Timestamp of last monday
Download Code
Re: Timestamp of last monday
by Anonymous Monk on Nov 18, 2011 at 14:22 UTC
    Currently your code looks like this :
    my ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst)=localtime(time);

    The time keyword returns an integer value of seconds since epoch... if you decrement that value before sending it to localtime you can return any date/time you want. I.e :

    my $week_of_seconds = 60 * 60 * 24 * 7; my $current_time = time(); my $week_ago = $current_time - $week_of_seconds; my ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst)=localtime($week_ago);

    The above isn't tested because I can't be bothered, the solution looks more or less like that, play around with it.

Re: Timestamp of last monday
by Anonymous Monk on Nov 18, 2011 at 14:32 UTC
    DateTime. Accept no substitutes.
    use DateTime qw(); my $now = DateTime->now; my $previous_monday = $now->subtract(days => ($now->wday_0 || 7)); my $formatted_date = $previous_monday->ymd(q()); # 20111114
      My preferred Swiss Army Knife for date-handling is Date::Calc. It offers Monday_of_Week, which you can use after identifying the week using Week_of_Year.
      Thank you very much! That helped me a lot, but i'm still struggeling with for formatting of the outputtet date, i have read and tried messing with the 'ymd' parameter without any luck
        Scratch that, did'nt look properly on your snippet, sorry. But thanks
Re: Timestamp of last monday
by choroba (Abbot) on Nov 18, 2011 at 14:38 UTC
Re: Timestamp of last monday
by Anonymous Monk on Nov 18, 2011 at 14:39 UTC

      Fav’d.

      Remember ... this is Perl ... and we don’t actually use the language (just) because we love the punctuation.   :-D   We do it for CPAN, which at this immediate moment has:

      73,482 Uploads; 23,767 Distributions; 101,586 Modules; 9,348 Uploaders.
      ... and by the time you read this, those numbers probably will have increased again.

      So, no matter what it is you’re doing, it has already been done.   That realization should guide your every thought and your every move.   Perl has one of the largest and the most mature libraries of ruggedly-tested code out there, anywhere.   So, if you want a new door, you don’t pick out a tree and buy a sawmill ... you pick out a door that suits you (they’re all free ...), and you install it.   Done.   If you’ve got a problem, you select a solution to that problem and install it.   Done.

Re: Timestamp of last monday
by Cristoforo (Deacon) on Nov 19, 2011 at 19:01 UTC
    #!/usr/bin/perl use strict; use warnings; use POSIX qw/ strftime /; # Script to get prior Monday (if today is Mon, then Mon a week ago). print scalar localtime, "\n", last_monday(localtime); sub last_monday { my ($wday, @dmy) = @_[6, 3..5]; # (will work across month, year boundries) $dmy[0] -= ($wday+6) % 7 || 7; # $dmy[0] == mday return strftime "%Y%m%d000000", 0,0,0,@dmy; }
Re: Timestamp of last monday
by ww (Bishop) on Nov 19, 2011 at 21:27 UTC
    And you've no doubt considered what to do in those cases in which Monday is a holiday and (iggerant) users simply run the job on their first workday of the week?

    PS: Ratazong's suggestion implicitly offers an algorithm to avoid the problem above: Identify week number of current week and of the previous Monday... and if those are the same, adjust Monday_of_Week as required.

    PPS: Christoforo's reply has a solution, too.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://938835]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-09-17 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls