Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Getting Weekly Dates

by oakley (Scribe)
on Mar 09, 2001 at 01:01 UTC ( [id://63099]=note: print w/replies, xml ) Need Help??


in reply to Getting Weekly Dates

Like Coyote said, use Date::Manip - it'a a darn good module and it will do alot more than what you would probably expect.

Now though - to get a list of the first Monday in every month starting Jan 2001 - Dec 2001, I used the following snippet of code and got every single one:
#!/usr/bin/perl -w use strict; use Date::Manip; my $base = "2000123123:59:59"; my $start = "2001010100:00:00"; my $stop = "2001123123:59:59"; my $syntax = "0:1*1:1:0:0:0"; my @dates = &ParseRecur($syntax,$base,$start,$stop); foreach my $day (@dates) { print "\$day is [$day]\n"; }
This prints out:
$day is [2001010100:00:00] $day is [2001020500:00:00] $day is [2001030500:00:00] $day is [2001040200:00:00] $day is [2001050700:00:00] $day is [2001060400:00:00] $day is [2001070200:00:00] $day is [2001080600:00:00] $day is [2001090300:00:00] $day is [2001100100:00:00] $day is [2001110500:00:00] $day is [2001120300:00:00]
And yes, I did verify the dates :)

Update:You may want to use substr($day,0,8); to get the first 8 characters which is the date - anything after that is the time
- oakley
Embracing insanity - one twitch at a time >:)

Replies are listed 'Best First'.
Re: Re: Getting Weekly Dates
by merlyn (Sage) on Mar 09, 2001 at 01:26 UTC
    I think you're working at that too hard:
    use Date::Manip; print map UnixDate($_, "%g\n"), ParseRecur("first monday of every mont +h in 2001");
    which yields
    Mon, 01 Jan 2001 00:00:00 -0800 Mon, 05 Feb 2001 00:00:00 -0800 Mon, 05 Mar 2001 00:00:00 -0800 Mon, 02 Apr 2001 00:00:00 -0800 Mon, 07 May 2001 00:00:00 -0800 Mon, 04 Jun 2001 00:00:00 -0800 Mon, 02 Jul 2001 00:00:00 -0800 Mon, 06 Aug 2001 00:00:00 -0800 Mon, 03 Sep 2001 00:00:00 -0800 Mon, 01 Oct 2001 00:00:00 -0800 Mon, 05 Nov 2001 00:00:00 -0800 Mon, 03 Dec 2001 00:00:00 -0800
    Date::Manip does everything. From the docs:
    I'm trying to build a library which can do _EVERY_ con- ceivable date/time manipulation that you'll run into in everyday life.

    -- Randal L. Schwartz, Perl hacker

      ...If only Date::Manip wouldn't whine about not being able to figure out my timezone. :( (on NT4WS)
        use Date::Manip; Date_Init("TZ=PST");

        -- Randal L. Schwartz, Perl hacker


        update: later on, in the same manpage (huge) it says...
        Unable to determine TimeZone Perhaps the most common problem occurs when you get the error: Error: Date::Manip unable to determine TimeZone. Date::Manip tries hard to determine the local time- zone, but on some machines, it cannot do this (espe- cially non-unix systems). To fix this, just set the TZ variable, either at the top of the Manip.pm file, or in the DateManip.cnf file. I suggest using the form "EST5EDT" so you don't have to change it every 6 months when going to or from daylight savings time. Windows NT does not seem to set the TimeZone by default. From the Perl-Win32-Users mailing list: > How do I get the TimeZone on my NT? > > $time_zone = $ENV{'TZ'}; > You have to set the variable before, WinNT doesn't set i +t by default. Open the properties of "My Computer" and set a + SYSTEM variable TZ to your timezone. Jenda@Krynicky.cz This might help out some NT users.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found