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

Hello, I capture the time that a viewers access my webpage like this: $xronos = localtime; i just want to know how can i add 2 hours to that time so i can see it in greek time and not at the time of the server. Thanks and a happy new year to everybody!

Replies are listed 'Best First'.
Re: How can i add 2 hours to localtime?
by borisz (Canon) on Jan 08, 2004 at 00:34 UTC
    perl -e 'print scalar(localtime(time + 7200))'
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can i add 2 hours to localtime?
by Zaxo (Archbishop) on Jan 08, 2004 at 00:34 UTC

    There are things like Date::Calc, but I think the easiest way is: my $now_plus_two_h = localtime( time + 2*60*60); time gives unix epoch time in seconds, so we add two hours worth of seconds and apply localtime. If the date format doesn't suit you, call POSIX::strftime() on the localtime result. See man strftime to browse what format strings you can use.

    After Compline,
    Zaxo

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can i add 2 hours to localtime?
by duff (Parson) on Jan 08, 2004 at 03:31 UTC

    Most of the answers given so far say "Just add 7200 seconds" to the local time as expressed in seconds since the epoch. What all of these solutions fail to mention is that it breaks when daylight saving time (DST) is nigh. Modules such as Date::Calc and DateTime however have facilities to handle the discrepancy. Alternatively you can do all of your math in GMT time (which doesn't have DST) and then convert to localtime at the end.

Re: How can i add 2 hours to localtime?
by ysth (Canon) on Jan 08, 2004 at 00:35 UTC
    Try:
    $ENV{TZ} = "Europe/Athens"; $xronos = localtime;
    or:
    $ENV{TZ} = "EEST+2EEDT"; $xronos = localtime;
    You may also need a use POSIX 'tzset' and a tzset() call between setting TZ and calling localtime().
Re: How can i add 2 hours to localtime?
by neuroball (Pilgrim) on Jan 08, 2004 at 00:34 UTC
    Localtime is based on epoch seconds. Meaning: localtime holds a big number of seconds that did past since a specific date (date depends on OS). Just add two hours (in seconds) to your localtime before you work with it.
    #!/usr/local/bin/perl use warnings; use strict; my $secs_in_minute = 60; my $mins_in_hour = 60; my $two_hours = $secs_in_minute * $mins_in_hour * 2; my $xronos = localtime (time + $two_hours); print $xronos."\n";

    /oliver/

    Updated code snippet, so that it would compile