Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

found this script that gives the day for a particular date...

Status
Not open for further replies.

spewn

Programmer
Joined
May 7, 2001
Messages
1,034
i found this online:


Code:
$day=5;
$month=12;
$year=2007;

$a = (14 - $month) / 12;
$y = $year - $a;
$m = $month + 12*$a - 2;
#$d = (5 + $day + $y + $y/4 + (31*$m)/12) % 7;
$d = ($day + $y + $y/4 - $y/100 + $y/400 + (31*$m)/12) % 7;

any idea why this isn't working?

- g
 
Not sure..

This works :)


use Date::Calc qw(Day_of_Week Week_Number Day_of_Week_to_Text);

$year = 2007;
$month = 12; # (June)
$day = 5;

$wday = Day_of_Week($year, $month, $day);
print "$month/$day/$year was a ", Day_of_Week_to_Text($wday), "\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
that's good to know, but the server i'm on (godaddy) only has:

Date::Manip

any way around it?

- g
 
use Date::Manip;
$y = "2007";
$m = "12";
$d = "5";


$day = Date_DayOfWeek($m,$d,$y);
#Appears to use 1=Mon - 7=Sun
print "$day\n";


btw I had no clue about either of these modules till I read your question and looked for a solution. Both of the examples come directly from their documentation (I test them though :) ). You should play around on and check out what each module can do.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
You can use Time::Local which is a core module. I wrote this snippet for another forum but it can be easily changed to use three variables as input instead of one:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Time::Local[/green] [red]'[/red][purple]timelocal_nocheck[/purple][red]'[/red][red];[/red]

[gray][i]# An array to hold the names of each day of the week. [/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@weekday[/blue] = [red]qw([/red][purple]Sunday Monday Tuesday Wednesday Thursday Friday Saturday[/purple][red])[/red][red];[/red]

[gray][i]# '09-11-2001' can be a parameter/argument you pass to the script[/i][/gray]
[black][b]my[/b][/black] [blue]$mm_dd_yyyy[/blue] = [red]'[/red][purple]09-11-2001[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$day_of_week[/blue] = [maroon]get_day[/maroon][red]([/red][blue]$mm_dd_yyyy[/blue][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$mm_dd_yyyy[/blue] was a [blue]$day_of_week[/blue][/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]get_day[/maroon] [red]{[/red]
   [black][b]my[/b][/black] [blue]$date[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url] || [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url][red]([/red][fuchsia]0[/fuchsia][red])[/red][red];[/red] 
   [black][b]my[/b][/black] [red]([/red][blue]$mon[/blue],[blue]$mday[/blue],[blue]$year[/blue][red])[/red] = [blue]$date[/blue] =~ [red]/[/red][purple]([purple][b]\d[/b][/purple]+)-([purple][b]\d[/b][/purple]+)-([purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red];[/red]
   [black][b]my[/b][/black] [blue]$epochtime[/blue] = [maroon]timelocal_nocheck[/maroon][red]([/red][fuchsia]0[/fuchsia], [fuchsia]0[/fuchsia], [fuchsia]0[/fuchsia], [blue]$mday[/blue], [blue]$mon[/blue]-[fuchsia]1[/fuchsia], [blue]$year[/blue][red])[/red][red];[/red]
   [black][b]my[/b][/black] [blue]$day[/blue] = [red]([/red][url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red]([/red][blue]$epochtime[/blue][red])[/red][red])[/red][red][[/red][fuchsia]6[/fuchsia][red]][/red][red];[/red]
   [black][b]return[/b][/black] [blue]$weekday[/blue][red][[/red][blue]$day[/blue][red]][/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Core (perl 5.8.8) Modules used :
[ul]
[li]Time::Local - efficiently compute time from local and GMT time[/li]
[/ul]
[/tt]



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
If you have Date::Calc module (from CPAN) then you could use this...

use Date::Calc qw(day_of_week week_number day_of_year);
$wday = day_of_week ($year, $month, $day)
$wnum = week_number ($year, $month, $day)
$dnum = day_of_year ($year, $month, $day)

 
lol, I just realised that to get the actual day of the week, you use module Date::Calc and travs69 has already explained how to do this...

Sorry ignore my posts hehe
 
Personally I hate using modules where a simple formula will do (though perfectionists will observe that a module checks also the correctness of any input...).
But spewn, you should have read that link more attentively: here is the correct translation in a script of that algorithm:
Code:
$a=int((14-$month)/12);
$y=$year-$a;
$m=$month+12*$a-2;
$d=($day+$y+int($y/4)-int($y/100)+int($y/400)+int(31*$m/12))%7;
There is however a much simpler and straightforward solution: the [tt]gmtime[/tt] and [tt]localtime[/tt] functions simply return the day of the week among other data!
Of course you'll need to express your date as a leap time in seconds, but that should be the only way that date values are handled internally by any Perl script...

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top