From mvyver at gmail.com Wed Feb 6 03:36:49 2008 From: mvyver at gmail.com (Mark Van De Vyver) Date: Wed, 6 Feb 2008 19:36:49 +1100 Subject: [TZInfo-users] TzInfo timezone base question Message-ID: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> Hi, Thanks for the work that has gone into making tzinfo available, and for making it freely available! I'm trying to figure out a way to obtain a Time instance that has a 'specific' timezone. The twist is I just start with a time string. For example require 'tzinfo' # Returns my time zone (GMT +11) Time.parse("09:30").iso8601 # "2008-02-06T09:30:00+11:00" What I'd like to be able to do is something like: Time.parse("09:30", other_tz).iso8601 which would return the deired timezone, say: "2008-02-06T09:30:00+5:00" Is there a way to do this using tzinfo? Appreciate any suggestions. Cheers Mark From phil.ross at gmail.com Wed Feb 6 08:43:12 2008 From: phil.ross at gmail.com (Philip Ross) Date: Wed, 6 Feb 2008 13:43:12 +0000 Subject: [TZInfo-users] TzInfo timezone base question In-Reply-To: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> References: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> Message-ID: <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> On 06/02/2008, Mark Van De Vyver wrote: > Hi, > Thanks for the work that has gone into making tzinfo available, and > for making it freely available! > > I'm trying to figure out a way to obtain a Time instance that has a > 'specific' timezone. The twist is I just start with a time string. > > Is there a way to do this using tzinfo? Hi Mark, The Ruby Time class only supports representing times in either UTC or the local timezone of the system. On Unix based architectures, the local timezone can be changed at runtime by setting the TZ environment variable. This will affect all Times constructed after changing the environment variable. For example: > ENV['TZ']='Australia/Melbourne' => "Australia/Melbourne" > Time.parse("09:30").iso8601 => "2008-02-07T09:30:00+11:00" > ENV['TZ']='Asia/Karachi' => "Asia/Karachi" > Time.parse("09:30").iso8601 => "2008-02-06T09:30:00+05:00" On Windows, changing the TZ environment variable has no effect (see http://www.ruby-forum.com/topic/68570#88607). Because of the limitations of the Time class, TZInfo always returns UTC Time instances and ignores the timezone of Times that are passed in. Regards, Phil -- Phil Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby From mvyver at gmail.com Wed Feb 6 14:46:26 2008 From: mvyver at gmail.com (Mark Van De Vyver) Date: Thu, 7 Feb 2008 06:46:26 +1100 Subject: [TZInfo-users] TzInfo timezone base question In-Reply-To: <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> References: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> Message-ID: <389c43e40802061146g554f8178oc5ebaada252c307@mail.gmail.com> Hi Philip, Thanks for the prompt response.... On Feb 7, 2008 12:43 AM, Philip Ross wrote: > On 06/02/2008, Mark Van De Vyver wrote: > > Hi, > > Thanks for the work that has gone into making tzinfo available, and > > for making it freely available! > > > > I'm trying to figure out a way to obtain a Time instance that has a > > 'specific' timezone. The twist is I just start with a time string. > > > > Is there a way to do this using tzinfo? > > Hi Mark, > > The Ruby Time class only supports representing times in either UTC or > the local timezone of the system. > > On Unix based architectures, the local timezone can be changed at > runtime by setting the TZ environment variable. This will affect all > Times constructed after changing the environment variable. For > example: > > > ENV['TZ']='Australia/Melbourne' > => "Australia/Melbourne" > > Time.parse("09:30").iso8601 > => "2008-02-07T09:30:00+11:00" > > ENV['TZ']='Asia/Karachi' > => "Asia/Karachi" > > Time.parse("09:30").iso8601 > => "2008-02-06T09:30:00+05:00" Thanks for the clarification, so there is no way to achieve a similar effect using tzinfo. > On Windows, changing the TZ environment variable has no effect (see > http://www.ruby-forum.com/topic/68570#88607). > > Because of the limitations of the Time class, TZInfo always returns > UTC Time instances and ignores the timezone of Times that are passed > in. OK this is where I got a little confused - apologies if this is obvious, so it seems to do what I want I can't use 'parse', but instead will have to extract the hrs and minutes, then use: tz = TZInfo::Timezone.get(source_tz) src_time_as_utc = tz.local_to_utc(Time.local(2005,8,29,src_hrs,src_mins,0)) Thanks for the clarification. Regards Mark > Regards, > > Phil > > -- > Phil Ross > http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby > From phil.ross at gmail.com Wed Feb 6 15:11:39 2008 From: phil.ross at gmail.com (Philip Ross) Date: Wed, 6 Feb 2008 20:11:39 +0000 Subject: [TZInfo-users] TzInfo timezone base question In-Reply-To: <389c43e40802061146g554f8178oc5ebaada252c307@mail.gmail.com> References: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> <389c43e40802061146g554f8178oc5ebaada252c307@mail.gmail.com> Message-ID: <4ac540410802061211r54600c6fjefaeec756813b511@mail.gmail.com> On 06/02/2008, Mark Van De Vyver wrote: > On Feb 7, 2008 12:43 AM, Philip Ross wrote: > > On Unix based architectures, the local timezone can be changed at > > runtime by setting the TZ environment variable. This will affect all > > Times constructed after changing the environment variable. For > > example: > > > > > ENV['TZ']='Australia/Melbourne' > > => "Australia/Melbourne" > > > Time.parse("09:30").iso8601 > > => "2008-02-07T09:30:00+11:00" > > > ENV['TZ']='Asia/Karachi' > > => "Asia/Karachi" > > > Time.parse("09:30").iso8601 > > => "2008-02-06T09:30:00+05:00" > > Thanks for the clarification, so there is no way to achieve a similar > effect using tzinfo. There is no equivalent of the parse function in TZInfo. You could use parse and do the following though: tz = TZInfo::Timezone.get(source_tz) src_time_as_utc = tz.local_to_utc(Time.parse("09:30" + " UTC", tz.now)) Adding " UTC" to the time forces it to be parsed as a UTC timestamp and causes any daylight savings rules of the system timezone to be ignored. TZInfo will still treat the time value as if it were in local time (it ignores the timezone aspect of the Time). Passing tz.now as the second argument to Time.parse is required in order to make sure you are dealing with the correct date. > OK this is where I got a little confused - apologies if this is > obvious, so it seems to do what I want I can't use 'parse', but > instead will have to extract the hrs and minutes, then use: > > tz = TZInfo::Timezone.get(source_tz) > src_time_as_utc = tz.local_to_utc(Time.local(2005,8,29,src_hrs,src_mins,0)) This would work too. TZInfo will just read the year, month, day, hour, minute and second of the Time passed to local_to_utc and ignore the timezone. It doesn't matter if Time.local returns a different timezone to source_tz. I would use Time.utc instead of Time.local though. If you specified a time that fell in a transition between standard and daylight savings time of the system timezone, then you might get an unexpected result with Time.local. Regards, Phil -- Phil Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby From mvyver at gmail.com Wed Feb 6 15:29:30 2008 From: mvyver at gmail.com (Mark Van De Vyver) Date: Thu, 7 Feb 2008 07:29:30 +1100 Subject: [TZInfo-users] TzInfo timezone base question In-Reply-To: <4ac540410802061211r54600c6fjefaeec756813b511@mail.gmail.com> References: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> <389c43e40802061146g554f8178oc5ebaada252c307@mail.gmail.com> <4ac540410802061211r54600c6fjefaeec756813b511@mail.gmail.com> Message-ID: <389c43e40802061229n5718a26el5734608b14fa24a9@mail.gmail.com> Hi Philip, On Feb 7, 2008 7:11 AM, Philip Ross wrote: > On 06/02/2008, Mark Van De Vyver wrote: > > On Feb 7, 2008 12:43 AM, Philip Ross wrote: > > > On Unix based architectures, the local timezone can be changed at > > > runtime by setting the TZ environment variable. This will affect all > > > Times constructed after changing the environment variable. For > > > example: > > > > > > > ENV['TZ']='Australia/Melbourne' > > > => "Australia/Melbourne" > > > > Time.parse("09:30").iso8601 > > > => "2008-02-07T09:30:00+11:00" > > > > ENV['TZ']='Asia/Karachi' > > > => "Asia/Karachi" > > > > Time.parse("09:30").iso8601 > > > => "2008-02-06T09:30:00+05:00" > > > > Thanks for the clarification, so there is no way to achieve a similar > > effect using tzinfo. > > There is no equivalent of the parse function in TZInfo. You could use > parse and do the following though: > > tz = TZInfo::Timezone.get(source_tz) > src_time_as_utc = tz.local_to_utc(Time.parse("09:30" + " UTC", tz.now)) Sorry I wasn't clearer, but in my case the time string 09:30 refers to the source_tz, which is _not_ my local tz nor UTC, and this source tz may change in different calls to the method I'm trying to write. I'd like to get a time object that is in the source_tz, but have abandond this idea, and will settle for an iso8601 string that contains the time in the source tz. > Adding " UTC" to the time forces it to be parsed as a UTC timestamp > and causes any daylight savings rules of the system timezone to be > ignored. TZInfo will still treat the time value as if it were in local > time (it ignores the timezone aspect of the Time). Thanks I suppose I could + "". But I may be getting confused again, I'd like TZInfo to treat the time as if it were in the source_tz rather than my local tz. > > Passing tz.now as the second argument to Time.parse is required in > order to make sure you are dealing with the correct date. > > > OK this is where I got a little confused - apologies if this is > > obvious, so it seems to do what I want I can't use 'parse', but > > instead will have to extract the hrs and minutes, then use: > > > > tz = TZInfo::Timezone.get(source_tz) > > src_time_as_utc = tz.local_to_utc(Time.local(2005,8,29,src_hrs,src_mins,0)) > > This would work too. TZInfo will just read the year, month, day, hour, > minute and second of the Time passed to local_to_utc and ignore the > timezone. It doesn't matter if Time.local returns a different timezone > to source_tz. Sorry, I'm confused, I thought that by calling tz = TZInfo::Timezone.get(source_tz), it means that in a call to 'tz.local_to_utc' that the 'local in 'local_to_utc' refers to the source_tz and not the system's tz. Is this correct? Regards Mark > > I would use Time.utc instead of Time.local though. If you specified a > time that fell in a transition between standard and daylight savings > time of the system timezone, then you might get an unexpected result > with Time.local. > > Regards, > > Phil > > -- > Phil Ross > http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby > _______________________________________________ > TZInfo-users mailing list > TZInfo-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/tzinfo-users > From phil.ross at gmail.com Wed Feb 6 15:54:46 2008 From: phil.ross at gmail.com (Philip Ross) Date: Wed, 6 Feb 2008 20:54:46 +0000 Subject: [TZInfo-users] TzInfo timezone base question In-Reply-To: <389c43e40802061229n5718a26el5734608b14fa24a9@mail.gmail.com> References: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> <389c43e40802061146g554f8178oc5ebaada252c307@mail.gmail.com> <4ac540410802061211r54600c6fjefaeec756813b511@mail.gmail.com> <389c43e40802061229n5718a26el5734608b14fa24a9@mail.gmail.com> Message-ID: <4ac540410802061254t6cb56049qc65fc0a2d003772d@mail.gmail.com> On 06/02/2008, Mark Van De Vyver wrote: > On Feb 7, 2008 7:11 AM, Philip Ross wrote: > > There is no equivalent of the parse function in TZInfo. You could use > > parse and do the following though: > > > > tz = TZInfo::Timezone.get(source_tz) > > src_time_as_utc = tz.local_to_utc(Time.parse("09:30" + " UTC", tz.now)) > > Sorry I wasn't clearer, but in my case the time string 09:30 refers to > the source_tz, which is _not_ my local tz nor UTC, and this source tz > may change in different calls to the method I'm trying to write. I'd > like to get a time object that is in the source_tz, but have abandond > this idea, and will settle for an iso8601 string that contains the > time in the source tz. My example does do what you want. "09:30" + " UTC" creates a Time that is in UTC, but TZInfo treats it as though it were in source_tz. I'm creating the Time in UTC to avoid any problems that might occur as a result of daylight savings rules in the system timezone. > > Adding " UTC" to the time forces it to be parsed as a UTC timestamp > > and causes any daylight savings rules of the system timezone to be > > ignored. TZInfo will still treat the time value as if it were in local > > time (it ignores the timezone aspect of the Time). > > Thanks I suppose I could + "". But I may be getting > confused again, I'd like TZInfo to treat the time as if it were in the > source_tz rather than my local tz. When using local_to_utc or any other method that takes a local time as a parameter, TZInfo will always treat the time as though it were in source_tz. It ignores any Timezone information in the Time and just reads the year, month, day, hour, minute and second values. > > This would work too. TZInfo will just read the year, month, day, hour, > > minute and second of the Time passed to local_to_utc and ignore the > > timezone. It doesn't matter if Time.local returns a different timezone > > to source_tz. > > Sorry, I'm confused, I thought that by calling tz = > TZInfo::Timezone.get(source_tz), it means that in a call to > 'tz.local_to_utc' that the 'local in 'local_to_utc' refers to the > source_tz and not the system's tz. Is this correct? Yes, that is correct. Regards, Phil -- Phil Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby From mvyver at gmail.com Wed Feb 6 19:18:16 2008 From: mvyver at gmail.com (Mark Van De Vyver) Date: Thu, 7 Feb 2008 11:18:16 +1100 Subject: [TZInfo-users] TzInfo timezone base question In-Reply-To: <4ac540410802061254t6cb56049qc65fc0a2d003772d@mail.gmail.com> References: <389c43e40802060036y2ec676e1g45e6016f962cd0dd@mail.gmail.com> <4ac540410802060543w688d5a8cwe5a33e5e23d2cbfb@mail.gmail.com> <389c43e40802061146g554f8178oc5ebaada252c307@mail.gmail.com> <4ac540410802061211r54600c6fjefaeec756813b511@mail.gmail.com> <389c43e40802061229n5718a26el5734608b14fa24a9@mail.gmail.com> <4ac540410802061254t6cb56049qc65fc0a2d003772d@mail.gmail.com> Message-ID: <389c43e40802061618v3911fa5bqdb29f91fd8c79fda@mail.gmail.com> Fantastic, Thanks again fro your patience and efforts. Regards Mark On Feb 7, 2008 7:54 AM, Philip Ross wrote: > On 06/02/2008, Mark Van De Vyver wrote: > > On Feb 7, 2008 7:11 AM, Philip Ross wrote: > > > There is no equivalent of the parse function in TZInfo. You could use > > > parse and do the following though: > > > > > > tz = TZInfo::Timezone.get(source_tz) > > > src_time_as_utc = tz.local_to_utc(Time.parse("09:30" + " UTC", tz.now)) > > > > Sorry I wasn't clearer, but in my case the time string 09:30 refers to > > the source_tz, which is _not_ my local tz nor UTC, and this source tz > > may change in different calls to the method I'm trying to write. I'd > > like to get a time object that is in the source_tz, but have abandond > > this idea, and will settle for an iso8601 string that contains the > > time in the source tz. > > My example does do what you want. "09:30" + " UTC" creates a Time that > is in UTC, but TZInfo treats it as though it were in source_tz. > > I'm creating the Time in UTC to avoid any problems that might occur as > a result of daylight savings rules in the system timezone. > > > > Adding " UTC" to the time forces it to be parsed as a UTC timestamp > > > and causes any daylight savings rules of the system timezone to be > > > ignored. TZInfo will still treat the time value as if it were in local > > > time (it ignores the timezone aspect of the Time). > > > > Thanks I suppose I could + "". But I may be getting > > confused again, I'd like TZInfo to treat the time as if it were in the > > source_tz rather than my local tz. > > When using local_to_utc or any other method that takes a local time as > a parameter, TZInfo will always treat the time as though it were in > source_tz. It ignores any Timezone information in the Time and just > reads the year, month, day, hour, minute and second values. > > > > This would work too. TZInfo will just read the year, month, day, hour, > > > minute and second of the Time passed to local_to_utc and ignore the > > > timezone. It doesn't matter if Time.local returns a different timezone > > > to source_tz. > > > > Sorry, I'm confused, I thought that by calling tz = > > TZInfo::Timezone.get(source_tz), it means that in a call to > > 'tz.local_to_utc' that the 'local in 'local_to_utc' refers to the > > source_tz and not the system's tz. Is this correct? > > Yes, that is correct. > > > Regards, > > Phil > > -- > Phil Ross > http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby > _______________________________________________ > TZInfo-users mailing list > TZInfo-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/tzinfo-users >