Nick Brutyn
2005-Oct-20 13:48 UTC
TZinfo problem: how to get diffirence in time from certain timezone
hey, this is imy situation. the site has timesheets, u can only view timesheets for the current day (the day of the user), and the server is ex in japan(total diff time and/or day), all the dates in the db is in utc ex on 20/10/2005 he want to view timesheets, so all the timesheets from 20/10/2005 00:00:00 to 20/10/2005 23:59:59 he wants to see (this is his local time), code : @timenow_start = ''#Time.local(year,month,day, 0,0,0).strftime(''%Y-%m-%d %H:%M:%S'') this need to be converted to the utc time for searching in th db. but this is the problem Time.local creates a local time (server time) and not a time for the user, so somehow i need the difference between the server time and utc, or the user time and utc, in sorts of (with daylights savings) => "europe/london" => 1 hour => "europe/brussels" => 2 hour thanks in advance
Philip Ross
2005-Oct-27 20:02 UTC
Re: TZinfo problem: how to get diffirence in time from certain timezone
Nick Brutyn wrote:> the site has timesheets, u can only view timesheets for the current day > (the day of the user), and the server is ex in japan(total diff time > and/or day), all the dates in the db is in utc > > ex on 20/10/2005 he want to view timesheets, so all the timesheets from > 20/10/2005 00:00:00 to 20/10/2005 23:59:59 he wants to see (this is his > local time), > code : @timenow_start = ''#Time.local(year,month,day, > 0,0,0).strftime(''%Y-%m-%d %H:%M:%S'') > > this need to be converted to the utc time for searching in th db. > > but this is the problem Time.local creates a local time (server time) > and not a time for the user, > so somehow i need the difference between the server time and utc, or the > user time and utc, > in sorts of (with daylights savings) => "europe/london" => 1 hour > => "europe/brussels" => 2 > hourYou can get the server time in UTC with Time.now.utc. Using TZInfo (your subject line and earlier post seems to suggest you might be using TZInfo), you can convert to and from a user''s timezone: tz = TZInfo::Timezone.get(''America/New_York'') local = tz.utc_to_local(utc_time) utc = tz.local_to_utc(local_time) You can also get the current local time in a timezone with: tz.now -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby