Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Server time vs computer logging in

by htmanning (Pilgrim)
on Jan 06, 2017 at 00:43 UTC ( #1179048=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I'm calculating the time when someone logs into a site so I know if they're online or not. It works fine, but one of the guys is in Asia and the time gets inserted ahead of the current time. For example, he logged in on 1/5/2017 at 24:40. There is no such thing as 24:40. He is one day ahead of the US but why would it enter 24:40 no matter when he logged in. It should be the server time. I am stuffing variables of $dateadded2 and $hourmin into the database based on this routine:

sub Calc_Date_Vars { use DateTime qw( ); $dt = DateTime->now( time_zone => 'Pacific/Honolulu' ); $dateadded_time = $dt->strftime("%l:%M %P"); $dateadded_date = $dt->strftime("%b %e, %Y"); $ymd = $dt->ymd; $dateadded2 = $ymd; $hour = $dt->hour_1(); $min = $dt->min(); $hourmin = "$hour:$min"; }
I don't get how a time of 24:40 could be entered at all. Shouldn't 12:40 am be 00:40?

The problem is I calculate the time in minutes (hour x 60 plus minutes) and subtract the last login from now. Then I do this:

if (($lastvisit eq "$dateadded2" && $nowhourmin < 10) { print "Online"; }

Well, right now the time is 19:42 on 1/5 but his entry says 1/5 24:40. 1/5 was yesterday for him so it's showing him as logged in even if he isn't. Shouldn't the time entered be the server time no matter where you login from? I'm having a brain fart here.

Replies are listed 'Best First'.
Re: Server time vs computer logging in
by huck (Prior) on Jan 06, 2017 at 00:59 UTC
      Thanks. Yep, I'm definitely jumping through hoops. Just a hack.

      I ended up converting the 24 to 00 and it works now.

      When you say use the raw time value, how do you do that?

Re: Server time vs computer logging in
by 1nickt (Canon) on Jan 06, 2017 at 02:07 UTC

    Hello htmanning,

    To be blunt, you are going about this all wrong. As you've been shown in your other thread, you don't need any of the date string manipulation you are doing. Think about it without code for a minute:

    • User logs in or refreshes a page. You get the time that happened on your webserver with time, which returns an integer.
    • You store the integer in the DB.
    • When testing whether the user is "logged on", you get the current time with time and compare the two integers to see if they are close enough together.
    • As simple as that would be with code, you can probably do it in the database itself - what DB are you running?
    • As others have said, this removes any concerns about dates, times, formats, timezones, etc.

    Remember - the three cardinal virtues of a Perlmonk are laziness, impatience, and hubris. You've demonstrated commendable hubris in your approach. Now show some laziness and impatience, by thunder, and do it the easy way!

    Hope this helps!

    The way forward always starts with a minimal test.
Re: Server time vs computer logging in
by Anonymous Monk on Jan 06, 2017 at 01:32 UTC
    There is no need for time zones in this calculation at all. Store the last seen time either in the server's time zone or better in UTC (like UNIX timestamps), and make sure that the current time is in that same time zone. Then just subtract the last seen time from the current time, as you've already been shown in previous threads.

    There is also no need to muck around with strings as much as you are, but if you insist, then at least use the appropriate functions (strftime and strptime) instead of building them yourself, and make sure to always include the time zone information in the strings.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-05-20 01:17 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (72 votes). Check out past polls.