Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to create CGI.pm like time strings?

by Arunbear (Prior)
on Nov 24, 2003 at 22:01 UTC ( [id://309714]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

are there any CPAN modules which allow you to create a time from a string using the formats that CGI.pm recognizes:

+30s 30 seconds from now +10m ten minutes from now +1h one hour from now -1d yesterday (i.e. "ASAP!") now immediately +3M in three months +10y in ten years time

I've created a session class (which wraps Apache::Session) for a web app and want to be able to do something like $session->store(login => 1, expires => '+10m') (I know that CGI::Session can do this - it was my 1st choice - but I can't use that module as it does not work on Win98 )
thanks for any pointers.

Replies are listed 'Best First'.
Re: How to create CGI.pm like time strings?
by shockme (Chaplain) on Nov 24, 2003 at 22:15 UTC
    Date::Calc should be able to do what you're needing. Of course, you'll still need to fit it into whatever session data structure you're using.

    If things get any worse, I'll have to ask you to stop helping me.

Re: How to create CGI.pm like time strings?
by b10m (Vicar) on Nov 24, 2003 at 22:15 UTC
    There are several modules that could manipulate dates/times, albeit a little different than what you proposed. Take a look for example at Date::Manip, Date::Calc, or Date::Handler::Delta. Like I said before, there are several modules and I only posted three. You might want to search CPAN yourself to find the one that will suit you best.

    UPDATE: taken out the nagging ...
    --
    B10m
Re: How to create CGI.pm like time strings?
by iburrell (Chaplain) on Nov 24, 2003 at 22:30 UTC
    CGI.pm uses the helpder CGI::Util::expires function. You could either use those directly. Or copy the code. The functions are pretty short.

      I wouldn't recommend using the functions directly, as they're not intended for public use or documented and thus could change out from underneath you in a future revision.

      Copying them works; as you say, they're short.

      Or, using Date::Calc:

      use Date::Calc qw(Today_and_Now Add_Delta_YMDHMS Day_of_Week_to_Text M +onth_to_Text Day_of_Week); while (<DATA>) { chomp; print "$_\n"; print cgitime($_), "\n"; } sub cgitime { my $arg = shift; my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now(1); # retu +rn in GMT for ($arg) { last if /^now$/; my @units = qw(y M d h m s); # order sensitive my $allowed = join 'w', @units; my ($sign, $num, $unit) = (/^([+-])(\d+)([$allowed])$/); return unless defined $sign and defined $num and $num and defi +ned $unit; $unit = 'd' and $num *= 7 if $unit eq 'w'; my @args = map /$unit/ ? "$sign$num" : 0, @units; ($year,$month,$day, $hour,$min,$sec) = Add_Delta_YMDHMS($year, +$month,$day, $hour,$min,$sec,@args); } return sprintf "%s, %02d-%s-%4d %02d:%02d:%02d GMT", Day_of_Week_t +o_Text(Day_of_Week($year,$month,$day)), $day, Month_to_\ Text($month), $year, $hour, $min, $sec; } __END__ now +10s +2w -1w -10m +1M +10y -1h +3d

      produces:

      now Monday, 24-November-2003 23:26:11 GMT +10s Monday, 24-November-2003 23:26:21 GMT +2w Monday, 08-December-2003 23:26:11 GMT -1w Monday, 17-November-2003 23:26:11 GMT -10m Monday, 24-November-2003 23:16:11 GMT +1M Wednesday, 24-December-2003 23:26:11 GMT +10y Sunday, 24-November-2013 23:26:11 GMT -1h Monday, 24-November-2003 22:26:11 GMT +3d Thursday, 27-November-2003 23:26:11 GMT

      Updated: Oops; 'w' for week isn't valid, and after I went to all the bother of making a special case for it.

Re: How to create CGI.pm like time strings?
by jdtoronto (Prior) on Nov 25, 2003 at 01:19 UTC
    (I know that CGI::Session can do this - it was my 1st choice - but I can't use that module as it does not work on Win98 )
    Apart form the possible problem using the file drivers (you may need to add some other modules to handle issues such as locking) CGI::Session DOES work on Win98, WinME, Win2k, WinXP. I have a single app written using Perl/Tk which runs on all of those as well as Linux, HP-UX, Solaris and OS-X. However as it is a database centred application I do use the MySQL method of storing the session data.

    jdtoronto

Re: How to create CGI.pm like time strings?
by Anonymous Monk on Nov 24, 2003 at 22:58 UTC
    I know that CGI::Session can do this - it was my 1st choice - but I can't use that module as it does not work on Win98
    That's not true. If CGI::Session doesn't work on win98, Apache::Session is not gonna work either. Perhaps you need File::FlockDir if you want to use the file drivers, but the modules do work on win32.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found