[lug] Perl days/dates

Tkil tkil at scrye.com
Tue Nov 14 20:12:41 MST 2000


um.  may i ask again exactly what you're trying to do?  :)

given this description:
>>>>> "Chip" == Atkinson, Chip <CAtkinson at Circadence.com> writes:

Chip> I'm writing a perl script to figure out the day of the week
Chip> based on a given date.  The numeric value for the day of the
Chip> week seems rather odd in that the week starts with Friday.  Does
Chip> anyone know about this?  Here's the script in its entirety.

Chip> use vars qw (@months @days $i);
Chip> use vars qw ($sec $min $hours $mday $mon $year);
Chip> use vars qw ($TIME $WEEKDAY @datestuff);

why not just use "my"?  and, on a stylistic note, i prefer to declare
at point of use...

Chip> @days = ("Fri", "Sat",
Chip>          "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

as efm pointed out, this is weird.

you can either use the sane interface to timelocal and localtime, or
if you insist on abusing things, take a look at

   http://slinky.scrye.com/~tkil/perl/Weekday.pm

a version using Time::Local and localtime might look like:

   sub day_of_week
   {
     my ($year, $month, $day) = @_;

     $year  -= 1900;
     $month -= 1;

     my $time_t = timelocal(0, 0, 0, $day, $month, $year);

     my ($dow) = (localtime($time_t))[6];
     return $dow;
   }

depending on how flexible you need to be about date parsing, you might
also look at the CPAN modules Date::Parse, Date::Calc, and Date::Manip
(all of which have different strengths, quite a bit of overlap, and
might be more than you need in this case).

can you describe your actual need more clearly?

t.




More information about the LUG mailing list