Doing a fixed offset and then using localtime is a bad idea since it doesn't account for differences in daylight saving. Even if both source and target area do daylight saving, they likely will not have them over the same periods of time.

The proper solution is to run localtime for a different timezone. At least on unices this can normally be done by setting the TZ environment variable. You can do that before even calling the program or at the very beginning of the program (probably even inside a BEGIN) if you want everything reported in Singapore time, or locally if you only want specific calls in Singapore time:

{ local $ENV{TZ} = "Singapore"; print scalar localtime; }

I could use "Singapore" as timezone name directly here since I have an entry named like that in my timezone database. That won't work on all operating systems though. Since Singapore doesn't do daylight saving, this one is probably more portable:

{ # Strangely enough I must use GMT-8 on my linux, even # though the official timezone name is GMT+8 (or UTC+8) local $ENV{TZ} = "GMT-8"; print scalar localtime; }
For other places you'd use their official timezone name that does have daylight saving, e.g. "CET" for central Europe.

And in case you are on a system that doesn't react to TZ, you can (ab)use the fact that Singapore does no daylight saving by indeed working with an offset, but relative to gmtime:

print scalar gmtime(8*3600+time);
Though that wouldn't of course generalize to places that do have daylight saving, or if Singapore ever introduces it.

In reply to Re: User's time by thospel
in thread User's time by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.