Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Date calculations in Perl for Win32

by cei (Monk)
on Aug 30, 2000 at 04:26 UTC ( [id://30244]=perlquestion: print w/replies, xml ) Need Help??

cei has asked for the wisdom of the Perl Monks concerning the following question:

I have a poll whose results I want to reset automatically every week. The way I would normally do this would be to get the week number (1 to 52) using formatting of the unix date command, save that along with my poll data, and when someone adds new poll data, compare the current week with the stored week -- if they're different, reinitialize the poll results.

That's what I'd LIKE to do.

But I'm writing for a client who has a stock build of ActiveState on an NT server, so I can't do a system call (unless there's a nice DOS equivalent of date that can give me week number and not just "August 29, 2000"). Likewise, it doesn't look like Date::Calc is part of the standard install for ActiveState perl, and even if it were, it would want a system_clock() call of some sort which may or may not be interpreted correctly by NT. (I haven't gotten that far, since Date::Calc wasn't installed anyway...)

So how would YOU approach my particular problem?

Help much appreciated.

Replies are listed 'Best First'.
Re: Date calculations in Perl for Win32
by wardk (Deacon) on Aug 30, 2000 at 04:58 UTC

    you can use the builtin localtime, just as on unix:

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdt) = localtime(time) +;

    just tested it on my wife's DOS98 box. I know it works on NT.

      This question deserves real code, specially since I just had to deal with almost the same thing.

      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdt) = localtime(ti +me); my $weekofyear=0; #first week is first sunday, 0 == lastyear; if ($yday>=$wday) { #into the first week $weekofyear=int(($yday-$wday)/7)+1; #normalize to sunday }

      That should do the trick nicely. Also, look at the Time::Local module. You'd be amazed what you can do once you have it in seconds...

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: Date calculations in Perl for Win32
by chromatic (Archbishop) on Aug 30, 2000 at 04:58 UTC
    Date::Calc and Date::Manip are both available from the ActiveState PPM Repository. I'd seriously suggest that your client install these.

    If that's absolutely positively impossible, then you're stuck with something like the following:

    my $day = (localtime())[7]; my $week_num = int($day/7) + 1;
    Come to think of it, that's not so bad after all.

      If you read the docs for Date::Manip, you'll see that even the author admits that it's a very large and slow module. The only time that I'd consider using it would be to parse date strings. For most other date calculations the standard Perl functions are much more efficient (like chromatic's Week number example above.

      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000, ICA, London
      <http://www.yapc.org/Europe/>

      Good point. It's strange that the Date::Calc and Date::Manip modules are included in the ActiveState Perl documentation (generated by pod2html, obviously) and the PPM Repository, but not in the standard distribution.

      Anybody know why ActiveState didn't include these modules with the standard distribution? They wouldn't have added much to the size.

      -ppm

        Because those modules aren't part of the standard distribution on any platform. As far as I know, the only non-standard modules that ActiveState have added to their distribution are the ones which are necessary in order to use ppm - i.e. the LWP bundle.

        --
        <http://www.dave.org.uk>

        European Perl Conference - Sept 22/24 2000, ICA, London
        <http://www.yapc.org/Europe/>
time and localtime should do it...
by ArthurDent (Acolyte) on Aug 30, 2000 at 21:30 UTC
    Cheers..

    It appears to me that the perl functions time and localtime are intrinsics, not in any particular module, so you could use those. I would use them to figure out what day of the week it is and use that information to see if you need to reset the results.

    Something like:
    my @time_struct; my $time_int; $time_int = time(); @time_struct = localtime($time_int); if (($poll_end_day + 1) == $time_struct[7]) { #need to reset $poll_end_day in here clear_results(); }
    Check out the documentation on time and localtime.

    Ben

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found