Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about datetime conversions involving daylight saving time

Status
Not open for further replies.

ajdavis

Programmer
Jun 17, 2005
14
US
I'm a nubie Perl programmer. I'm working on a Perl cgi gui. Part of the data it will display are datetimes that are stored in a database in GMT. I want to give the user the option of specifying which time zone the dates are displayed in. I have put the following code together to convert the GMT time to the corresponding timezone.

my $datetime = "2005-06-22 20:04:47"; #to be passed in
my $timezone = "EST"; #to be passed in

my ( $year_month_days, $hours_mins_secs ) = split ' ', $datetime;
my ( $year, $month, $day ) = split '-', $year_month_days;
my ( $hours, $minutes, $seconds ) = split ':', $hours_mins_secs;

$month -= 1;
my $time = timegm($seconds, $minutes, $hours, $day, $month, $year);

my $template = "%Y-%m-%d %H:%M:%S";
print = time2str($template, $time, $timezone);

This doesn't take into account that the GMT date given falls within the daylight savings period for the eastern timezone so it returns a datetime that is an hour off. Is there a method that I can call that determines whether a daylight savings shift should take place based on a GMT time and a given timezone? Is anyone aware of any modules or code that may already be out there that will do what I'm looking for? I've been looking through stuff on the net, but I've not found anything that goes beyond the daylight savings flag that is returned by the localtime method. Thanks for taking the time to look at this.
 
are datetimes that are stored in a database in GMT
What kind of database ?
And how do you make your query to get the time from the database ?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
If you pass EST, you get Eastern Standard Time, if you pass EDT, you get Eastern Daylight Time.

It is incredibly difficult to tell when "Daylight Saving Time" is in effect for a given timezone/region. Your easiest bet may be to just check localtime() and use that value. This is probably what the average user would expect.

________________________________________
Andrew
 
perluserpengo said:
What kind of database ?
And how do you make your query to get the time from the database ?

The database is MySQL and I won't be making the query. I'll just format a SOAP call to our back-end application that this GUI is for, asking for certain fields. The app will make the database query and return the desired fields. All dates are stored in GMT in the format given in the above code snippet.


icrf said:
If you pass EST, you get Eastern Standard Time, if you pass EDT, you get Eastern Daylight Time.
I noticed that this was the case, but I was trying to keep from having to list both sets of time zones (Standard/Daylight) in a drop down list. It would be huge.

icrf said:
It is incredibly difficult to tell when "Daylight Saving Time" is in effect for a given timezone/region.
I know, that's why I came here first. :)

icrf said:
Your easiest bet may be to just check localtime() and use that value. This is probably what the average user would expect.
Our users are going to be operations support people who will be located anywhere in the world that may be working on an issue for a client located somewhere else in the world. The thought behind converting these datetimes to a user specified timezone is to allow the support people to view all the information in the GUI based on the time zone for that client or to have the dates all displayed in their local time, whichever they wished.

Thanks for the prompt replies from both of you. I apreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top