Re: Date to Day
by jmcnamara (Monsignor) on Aug 25, 2004 at 21:42 UTC
|
##################################################################
+#########
#
# day_of_week($year, $month, $day)
#
# Valid from 14 September 1752 onwards. $month and $day are 1 inde
+xed.
#
# Returns: 0 .. 6 where 0 is Sunday.
#
# Algorithm by Tomohiko Sakamoto from the C FAQ, section 20.31.
#
sub day_of_week {
my ($year, $month, $day) = @_;
my @offset = (0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
$year -= $month < 3;
return ($year + int($year/4) - int($year/100) + int($year/400)
+ $offset[$month-1] + $day) % 7;
}
--
John.
| [reply] [d/l] |
|
|
Nice code! Great solution to this problem. Thank you for providing it.
Joe
| [reply] |
Re: Date to Day
by TrekNoid (Pilgrim) on Aug 25, 2004 at 21:43 UTC
|
Is there a simpler way to find the weekday from a date string without a module?
Dr Math's Calendar Fun
There's two algorithms on this page for deducing the day of the week for any Gregorian calendar date.
Trek | [reply] |
|
|
Another very nice algorithm page. I'll be sure to look through this page carefully. Thanks!
| [reply] |
Re: Date to Day
by ysth (Canon) on Aug 25, 2004 at 21:46 UTC
|
Unlike Date::Calc, Time::Local comes with perl. Untested:
use Time::Local "timelocal";
my ($month, $mday, $year) = split /-/, "01-23-2004";
my $wday = (localtime(timelocal(0, 0, 0, $mday, $month-1, $year-1900))
+)[6];
But if you are doing much date work, it's worthwhile to learn how to use Date::Calc or DateTime.
Update: adjusted year and month to be localtimey. Thanks isotope. | [reply] [d/l] |
|
|
Watch those values there, ysth. The range for $month and the math for $year are the same as localtime().
| [reply] |
Re: Date to Day
by CountZero (Bishop) on Aug 25, 2004 at 21:40 UTC
|
Here you find the algorithm to calculate the Julian Day number which is very easy to calculate the day of the week from.
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] |
|
|
Very interesting! I'll be bookmarking this page. Thanks for the help.
Joe
| [reply] |
Re: Date to Day
by Your Mother (Archbishop) on Aug 25, 2004 at 21:33 UTC
|
It's a little involved but it should get you going. There are *many* more excellent and speedy routines in Date::Calc.
use Date::Calc qw( Today Add_Delta_Days
Day_of_Week_to_Text Day_of_Week );
my @today = Today();
my $days_in_the_future = shift || 0;
my @then = Add_Delta_Days( @today, $days_in_the_future );
print Day_of_Week_to_Text( Day_of_Week( @then ) ), "\n";
| [reply] [d/l] |
|
|
Well, if the OP already has the number of days in the future, it can be a lot simpler:
my $today_wday = (localtime)[6];
my $then_wday = ($today_wday + $days_in_the_future) % 7;
Update:
But it sounds like he's just given a date string. If it's a simple MM-DD-YYYY, I'd suggest Time::Local+localtime, since it's core.
use Time::Local;
my $string = "01-01-2015";
my ($m, $d, $y) = split /-/, $string;
my $epoch = timelocal 0, 0, 0, $d, $m-1, $y-1900;
my $wday = (localtime $epoch)[6];
To do any of the other algorithms suggested, you'll need to parse out the day, month, year values anyway.. If this is a problem, you will need a beefier date module like Date::Calc.
| [reply] [d/l] [select] |
|
|
Wow! I didn't know that you could use the localtime function in that way. This is very useful. Thanks!
Joe
| [reply] |
|
|
I'll have a look into that. Though I generally try to shy away from modules, this one seems worth the extra install. Thanks for pointing me to it. I appreciate your help.
Joe
| [reply] |
|
|
| [reply] |
|
|
| [reply] |