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

I'm not sure if im going anywhere with the coding I have so far with this time stamp issue. I'm tryint to create a whos online script that shows all the members online and when they logout it removes them from the list or if they end up being idle for 30 mins it removes them from the list.

If theres a much better way to do this please tell me, and if the following code is good and would work please help me with the problem I have.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time ++$addsecs); @months = ("1","2","3","4","5","6","7","8","9","10","11","12"); @days = ("Sun","Mon","Tue","Wed","Thur","Fri","Sat"); if ($sec < 10) {$sec = "0$sec";} if ($min < 10) {$min = "0$min";} if ($hour < 10) {$hour = "$hour";} if ($hour > 11) {$ap = "PM";} if ($hour < 12) {$ap = "AM";} if ($mday < 10) {$mday = "0$mday";} if ($hour eq 00) { $hour = "12"; } if ($hour > 12) { $hour = ($hour - 12); $ap = "p.m."; } $millyear = $year + 1900; $date = "@days[$wday]:$mday:@months[$mon]:$millyear"; $time = "$hour:$min:$sec:$ap:$date"; use DB_File; # optional; overrides default tie %who, "DB_File", "$path/whosonline" or die "Can't open FILENAME: $ +!\n"; # open database tie hash to db $who{$Account::account} = "$time"; #account-value time-key @person = keys(%who); foreach $timer (values %who) { ($hr, $mins, $secs, $appm, $day, $daynumber, $month, $year) = split(/: +/, $timer); print <<EOF; $hr, $mins, $secs, $appm, $day, $daynumber, $month, $year EOF if ($hour eq $hr) { if (@days[$wday] eq $day) { $member = $who; print <<EOF; $member EOF
The code is not fully finished because im stuck.

Now I want to check the DBM file for all the time that members logged in. I use a foreach statement to check all the times in the DBM file and if the time is idle then it removes that key/value, and if it is not idle then it adds the new key/value to it, meaning it refreshes the members time. The thing is when it does come to a point where the time is idle, I don't know how to delete that certain time and its key because I don't know the key.
Thank you,
Anthony

Replies are listed 'Best First'.
Re: help with time stamp
by extremely (Priest) on Feb 21, 2001 at 12:18 UTC
    A few notes:

    One, for timestamps, usually just time(); is enough. Why pretty format a date you are going to stick in a dB? Work with the time in seconds and save your sanity. Or else look at $time = localtime(); and see if the date string that puts out is good enough. localtime puts out a nice pretty string when assigned to a scalar variable rather than an array.

    $month += 1; might be easier than creating the array lookup table @month. And a nice shortcut when you do need that is my @month = 1..12;

    Your AM/PM logic is really weird. Try:

    my $ap= $hour>11 ? "PM" : "AM"; $hour = $hour % 12; $hour = $hour || 12;

    sprintf is your friend. You can lose all the "zero padding" code and do this:

    $date = sprintf '%3s:%02d:%02d:%4d', @days[$wday], $mday, $mon, $milly +ear; $time = sprintf '%02d:%02d:%02d:%2s:%11s', $hour, $min, $sec, $ap, $da +te;

    Sure, it is all nitpicky stuff but once you look at it, ain't Perl cool?

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

Re: help with time stamp
by dws (Chancellor) on Feb 21, 2001 at 11:49 UTC
    This would be a lot easier if you were using MySQL and could delegate the date logic to it. merlyn has an article in the March, 2001 Web Techniques that does exactly (well, approximately) that. Unfortunately, the article isn't up on his personal site yet.

    Update: The article is up on the Web Techniques site.

    Update 2: Hey, you already asked part of this question, and got a few answers that still apply. Go re-read A Time Stamp problem, and look at my earlier answer suggesting that you use a flatfile.

Re: help with time stamp
by a (Friar) on Feb 21, 2001 at 11:35 UTC
    A guess would be to look at the Date::Calc modules, or to leave localtime alone (its an integer (I believe) in seconds from 1/1/1970) and just do the math from the number, w/o worrying about making it sec/min/hour/day/year.

    a