Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Run if weekend

by Anonymous Monk
on Jun 14, 2016 at 15:46 UTC ( #1165607=perlquestion: print w/replies, xml ) Need Help??

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

Hi Everyone, how would you go about writing a script that will check if its the weekend if it is carry on if its a weekday cancel out? Thanks in advance still learning alot!

Replies are listed 'Best First'.
Re: Run if weekend
by davido (Cardinal) on Jun 14, 2016 at 16:06 UTC

    Keeping it simple, localtime returns the day of the week in list context as the field at index 6:

    print [localtime]->[6], "\n"; # Outputs 2 for me, today, because it's Tuesday and weeks start at 0 f +or Sunday.

    So values of 0 or 6 would represent Sunday and Saturday.

    Time::Piece, which comes with Perl, presents an object oriented interface into date/time manipulation:

    use Time::Piece; my $t = localtime; print join(', ', $t->day_of_week, $t->day, $t->fullday), "\n"; # Outputs 2, Tue, Tuesday

    However, you may also decide that it's a better separation of concerns, leading to more generally useful code if you let the code not care about the day of the week, and set up a crontab entry to do what cron does best.


    Dave

Re: Run if weekend
by Your Mother (Archbishop) on Jun 14, 2016 at 16:45 UTC

    It might be relevant how your script is run. A script that is aware of temporal concerns seems like code smell to me. A crontab or other scheduling daemon makes it easy to slot for only weekends, for example, leaving the script to only care about what it is supposed to do, not when it is supposed to do it.

      I had a perl script which I ran every day for several years. It had to form a URL which included the current date. The format was slightly different on Sunday. It made perfect sense to use localtime to get the date and day-of-week and to use strftime (FUNCTIONS) to format it.
      Bill

        That's cromulent but, at least from the limited original description, quite different from the OP's problem; a script specifically to write temporal output versus a script only intended (for the time being at least) to run within a temporal window. The second is the sort of thing that is harder to debug and circumvent on demand as well. Taking date args in the invocation would be better than hardcoding the date in all invocations. So I say. :P

      Indeed, a script that refuses to do anything during weekends sounds like a lot of fun to maintain in your spare time :-D

      -- FloydATC

      Time flies when you don't know what you're doing

Re: Run if weekend
by QuillMeantTen (Friar) on Jun 14, 2016 at 15:53 UTC

    1. Update, I was wrong, 0 is sunday, source: perldoc
    2. Else if you run it on an unix system you can always use a cronjob it might be easier from an administration point of view
    3. Added the usual disclaimer at end of post


    Funny you should ask that, for a script at work I just started using Date::Calc for manipulating weekdays, if you just want that datum and nothing else you can get it using this:
    my $wday = (localtime)[6]; if($wday == 0 || $wday == 6){ #do something }
    Be aware that its only my kind of fishing here, keep looking for answers and explanations.

Re: Run if weekend
by Anonymous Monk on Jun 15, 2016 at 08:46 UTC
    Hi Everyone! Thanks for your replies! Sorry i should have clarified further, I have my server monitoring checking for an update to a file using a deamon. There is a perl script that will check the file and then pass on the amount of updates that has occured and email us. The main issue is that it is only required for the weekend as we are in the office to manually see the update come through during the week. So i'm trying to find a method that will check the day if its a weekday exit out of the script, if it's the weekend continue with the rest of the code to check the updates to the file. Thanks! hopefully it makes a bit more sense now!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2022-07-02 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (103 votes). Check out past polls.

    Notices?