Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Between Two Times??? Help!

by ACJavascript (Acolyte)
on Mar 22, 2003 at 16:04 UTC ( [id://245158]=perlquestion: print w/replies, xml ) Need Help??

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

Hello again perl monks!
My question is who can I tell if the user has logged in between two dates?
Say the first date is 2/21/6 : that is month,day of month, day and the second is 2/22/7 : that is month, day of monty, day I am trying to make a script that will let the user in if its within the last 24 hours of his first sign in. I can do that but I am having problems with the 24 hour clock part.

Here is my test script that I am trying to work this out, Please any help I would most appreciate!!

##Time function @days = ("0","1","2","3","4","5","6"); @Months = ("0","1","2","3","4","5","6","7","8","9","10","11"); ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); ($sec2,$min2,$hr2,$mday2,$mon2,$year2,$wday2,$yday2,$isdst) = localtim +e(time-86400); $year = $year + 1900; $timerF="@Months[$mon]/$mday/@days[$wday]/$hr:$min:$sec"; $timerF2="@Months[$mon2]/$mday2/@days[$wday2]/$hr2:$min2:$sec2"; print "$timerF\n\n\n$timerF2";



Thanks in advance!

Replies are listed 'Best First'.
Re: Between Two Times??? Help!
by dakkar (Hermit) on Mar 22, 2003 at 16:48 UTC

    Where does that strange date format come from? It's the first time I see a date with "day of the week" but without the year...

    Anyway.

    About your code: the two arrays are useless, since $days[$n]==$n for each $n, and the same holds for @Months. And keep in mind that @array[@indices] is an array slice, while $array[$index] is a single element. Search for "array slice" for more info.

    Now, what do you really need to do? Do something if the current time is no later than 24 hour after a given date?

    In this case, I'd do it this way:

    use POSIX; # this module defines the function mktime $last='2/21/5/19:22:14'; # this is the timestamp of the user's last lo +gin $now=time; # the present ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now-8 +6400); # a day ago ($mon2,$mday2,$wday2,$hr2,$min2,$sec2)=($last=~m|^(\d+)/(\d+)/(\d+)/(\ +d+):(\d+):(\d+)|) or die "Wrong timestamp: $last\n"; # extract values + from the timestamp, with a bit of checking $last_time=POSIX::mktime($sec2,$min2,$hr2,$mday2,$mon2,$year); # trans +form the timestamp into a unix-time (seconds since the epoch) unless (defined $last_time) {die "Wrong timestamp: $last\n"} # mktime +will return undef if the date is not valid print "In range" if ($now-$last_time)<86400; # if the difference is le +ss than a day...
    -- 
            dakkar - Mobilis in mobile
    
      HEY Dakkar,,, that posix module seems to work just great!!! I think this is just what i was looking for.. Thank you so very much... I can now continue... If something goes wrong I know were to come back LOL :D

      Thannks alot guys/gals!
Re: Between Two Times??? Help!
by Limbic~Region (Chancellor) on Mar 22, 2003 at 16:34 UTC
    ACJavascript,
    What you asked for help on, and what you said you are trying to solve are two different things.
  • Determine if something happened less than 24 hours ago
  • Determine if one time stamp is between two other time stamps

    For the first one, you could simply do this:

    #!/usr/bin/perl -w use strict; my $Cut_Off = time - ( 24 * 60 * 60); my $First_Sign_In = 1; #some function to get the timestamp - see modul +e suggestions later if ($First_Sign_In > $Cut_Off) { print "Welcome\n"; } else { print "Sorry, you need to contact me\n"; }
    You could have a look at Date::Manip or Time::Local for help in converting dates to timestamps.

    The second part of your node - did something happen between two dates, can be solved with the following pseudo code:

  • Convert low end boundary to time stamp
  • Convert high end boundary to time stamp
  • Convert event to time stamp
  • If event greater than low and less than high, move on

    I hope this helps - cheers L~R.

      Very interesint,,,,

      Is there any way of doing this without using modules?
        ACJavascript,
        It is trivial to do the less than 24 hours thing. Just store the value in timestamp format when the person logs in and retrieve it later ($first_logon = time;). That way you can retrieve it when you are determining the cut off. As far as converting a date into a timestamp - yes it is possible without modules or the module itself wouldn't be possible. Date::Manip is very fast because it uses a C backend. Time::Local basically just uses convergence by guessing at the time and then lowering/highering values until it locks in on its target. It also uses a cache to make repeated checks of the same month faster, so I wouldn't suggest re-inventing these wheels.

        Cheers - L~R

Re: Between Two Times??? Help!
by Tomte (Priest) on Mar 22, 2003 at 16:29 UTC

    Update: Beside the module suggestion: please disregard this post, as I completly misread the OPs problem, especially the strange date-format.


    I'd suggest using Time::ParseDate: Or/And any other Time::XXX module that suites your needs.

    use Time::ParseDate; my ($date1, $date2) = qw!2002/22/6 2002/22/7!; my ($sec1, $sec2); $sec1 = parsedate($date1); $sec2 = parsedate($date2); my $diff = $sec2-$sec1; if ($diff > 0 && $diff <= 86400) { openSesame(); } else { youReOut(); } sub openSesame { print "IN\n"; } sub youReOut { print "OUT\n"; }

    regards,
    tomte


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-07-13 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.