Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Date and Time

by arrow (Friar)
on Dec 03, 2002 at 00:02 UTC ( [id://217085]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Perl Monks! I have a problem with the date I'm getting from my server. The problem is not with my Perl, it's with my server, because my server is in England. Therefore, the day cannot be relied upon to be accurate. Most of the time it is a day ahead, but sometimes it's on the same day. At first I tried subracting 1 from the day, but then I realized that sometimes the original day was correct. The date on my script is not essential, but I would like to have the correct day without to much trouble. Is there any other way to do this (without changing servers)?

Thanks, arrow

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: Date and Time
by DamnDirtyApe (Curate) on Dec 03, 2002 at 00:13 UTC

    The problem probably isn't that the server in England is wrong, but that your in a different time zone. Something like this might help you get your local time:

    my $date_england = time ; my $tz_offset = -8 ; my $date_pacific = time + ( $tz_offset * 3600 ) ; print "England: ", scalar localtime( $date_england ), $/ ; print "Pacific: ", scalar localtime( $date_pacific ), $/ ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Date and Time
by SuperCruncher (Pilgrim) on Dec 03, 2002 at 00:19 UTC
    I hasten to point out that just because your server is not in your timezone, it does not mean that it "cannot be relied upon to be accurate". But I know what you mean :-)

    All you need to do is find out how many hours you are behind GMT, and substract that from localtime. Try this (untested):

    my $time = localtime; my $hours_behind_gmt = 4; # localtime is in seconds, hence we need to get hours --> seconds my $time_in_my_timezone = localtime - ($hours_behind_gmt * 60 * 60); print scalar localtime($time_in_my_timezone);
    Beware of the main caveat of dealing with different timezones: Daylight Savings Time. If your server is based in England, during the summer it will not use GMT--it will use BST (British Summer Time) instead. So beware.

    For doing fancy operations with dates, you might like to use the Date::Calc module on CPAN. Finally, in future, please do a bit of research yourself. This is quite a basic question and the answer could probably be found with a bit of hacking and Google-ing. If you have done research, give us the code you've already got (even if you think it's bad). People are more inclined to help if you seem to be making a real effort yourself.

Re: Date and Time
by robot_tourist (Hermit) on Dec 03, 2002 at 00:18 UTC

    Is this along the right lines? England should be a fixed number of hours away from you, so if you do $time = time(); then do $time += or -= n*60*60 you should have your local time (give or take a few seconds). Then get your date using localtime.

    <whisper>is this homework?</whisper> You may even be able to change locale, but I don't know anything about that.

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist. Robot Tourist, by Ten Benson

Re: Date and Time
by mt2k (Hermit) on Dec 03, 2002 at 00:13 UTC
    I don't understand why the problem lies with the server being in England. Is England on some odd time warp? :)

    But to answer your question, how about getting Greenwich Mean Time (GMT) and figuring out how many hours ahead/behing you are? gmtime() might be able to help you. It's like localtime() except it returns the date values in GMT time.`

Re: Date and Time
by arrow (Friar) on Dec 03, 2002 at 00:30 UTC
    Yeah, SuperCruncher and others are right, I should have investigated further. Sorry for seeming lazy (maybe I was :-)), but here's my code:
    (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday, +my $isdst)=localtime(time); my $month1 = $mon+1; my $year1 = $year+1900; if ($mday == 1 && $month1 == 2) { $mday = 28; } elsif ($mday == 1 && $month1 == 4) { $mday = 30; } elsif ($mday == 1 && $month1 == 6) { $mday = 30; } elsif ($mday == 1 && $month1 == 9) { $mday = 30; } elsif ($mday == 1 && $month1 == 11) { $mday = 30; } else { $mday--; } my $date1 = "$month1\/$mday\/$year1";


    Just Another Perl Wannabe

      Your adjustments are not necessary here. Assuming the only issue here is the time zone difference, try consolidating your code into something like the following:

      my $server_tz = 0 ; # GMT my $client_tz = -8 ; # PST my $offset = $client_tz - $server_tz ; my @t = localtime( time + ( $offset * 3600 ) ) ; my $date = sprintf "%d/%d/%d", $t[4] + 1, $t[3], $t[5] + 1900 ;

      _______________
      DamnDirtyApe
      Those who know that they are profound strive for clarity. Those who
      would like to seem profound to the crowd strive for obscurity.
      
                  --Friedrich Nietzsche

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found