Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

finding whether the current time is weekday & working time with Date::Manip

by Anonymous Monk
on Nov 21, 2011 at 21:07 UTC ( #939320=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


I have used Date::Manip to find whether the current time is "working day and time". I did it in the following method,

# find whether the today is working day ? &Is_WorkDay(&ParseDate(&DateCalc("today", "+9 hours"))) <br><br> # find whether the current time is within current working day ? # find current hour with localtime function. print "yes" if ( $cur_hr > 9 && $cr_hr < 17 )
But I understand there is simpler way with Date::Manip With Date_Init - set workdaybeginning and end. But again how to check get the current time ( do i have to still use localtime ? ).

Am looking for something simple - a one line code which says whether current time and date is business time or not ! Can anybody did it already ?- please help me.

Comment on finding whether the current time is weekday & working time with Date::Manip
Download Code
Re: finding whether the current time is weekday & working time with Date::Manip
by Marshall (Prior) on Nov 22, 2011 at 00:31 UTC
    Here is one link of many: datetime faqs, etc for Perl.

    Update: Oh, Darn. I see that I mis-read the question. The below is still applicable although the question is hour of day instead of day of week. Look at the parameters that localtime(time) returns. Think about what happens if you are going to pay somebody for number of hours worked and all you have is 1:30 AM as the log-out time. On some days that local time will happen twice! The whole subject of dates/times is actually pretty complicated and much more so than a simplistic "first pass" would lead you to believe.

    The most important basic advice is for you to store date/time information in UTC (GMT). Do this because each second has a unique UTC date that it occurs in. Convert from UTC to local time only for user presentation. Do not calculate in local time (some days have 25 hours) and other weird things due to daylight savings time.

    UTC (GMT) marches onward and never goes backward. We do sometimes, rarely have "leap seconds" where time as measured by humans jumps forward one second. But it doesn't go "backward"...this "spring forward, fall back" multiple times per year doesn't happen with UTC(GMT).

    Each UTC time is unique in terms of your normal human sense of time flow. But say on the day that we set our clocks "back one hour" at 2AM local, that means that on that day, you will have 2 different UTC times for say 1:28 AM because 1:28 AM happened twice on that day!

    Some simple code for you..if you just want to know about "right now"...no extra modules needed...

    #!/usr/bin/perl -w use strict; # the time functions follow the US sense of the calendar # in the US, the week starts on Sunday (index zero) # in Europe and other parts of the world, the week starts on Monday. # be aware of that if you are printing calendars, etc. my @dayNames = qw(Sunday Monday Tuesday Wednessday Thursday Friday Saturday); my ($wday) = (localtime(time))[6]; print "today's index number of the week is $wday: $dayNames[$wday]\n"; if ($wday >=1 and $wday <=5) { print "Today is normally a work day ", " holidays aren't taken into account\n"; } else { print "Hey, go home and sleep unless you get paid overtime\n"; } __END__ today's index number of the week is 1: Monday Today is normally a work day holidays aren't taken into account
Re: finding whether the current time is weekday & working time with Date::Manip
by Anonymous Monk on Nov 22, 2011 at 09:49 UTC
    This is what I was expecting, now done - thanks anyway.
    &Date_Init('WorkWeekBeg=1', 'WorkWeekEnd=5', 'WorkDayBeg = 09:00', 'Wo +rkDayEnd = 17:00'); # if now is not a week day - then return 0. if (not &Date_IsWorkDay(&ParseDate("now"), 1)) { .... }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2014-12-26 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (172 votes), past polls