Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by Marshall (Prior)
on Nov 22, 2011 at 00:31 UTC ( #939347=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re: finding whether the current time is weekday & working time with Date::Manip
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-12-18 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (67 votes), past polls