Hi all,
I''ve monkey patched parser.rb to handle timezones, so everything
comes out as GMT. I''m using the TZinfo library to handle this, and
have some icky hacks to deal with Timezones that are specified oddly
in Apple''s iCal...
Does anyone have a better approach to this?
Eric
# Dates, Date-Times & Times
# NOTE: invalid dates & times will be returned as strings...
def parse_datetime(name, params, value)
#puts ""
#puts name
#puts params
#puts value
timezone =""
begin
dt = DateTime.parse(value)
# if we have timezone information, use it to convert to UTC
if !value.index("TZID=").nil?
startIndex = value.index("TZID=")
endIndex = value.index(":")
timezone = value[startIndex+5..endIndex-1]
if timezone =="(GMT-05.00) Eastern Time (US & Canada)"
timezone = "US/Eastern"
end
timezone.gsub!("-","/")
if timezone == "Pacific"
timezone="US/Pacific"
end
#puts "Timezone: #{timezone}"
tz = TZInfo::Timezone.get(timezone)
#puts "Before fixing UTC: #{dt}"
dt = tz.local_to_utc(DateTime.new
(dt.year,dt.month,dt.day,dt.hour,dt.min,dt.sec))
#puts "After fixing UTC: #{dt} (Local is: #{tz.utc_to_local
(dt)})"
end
dt
rescue Exception => ex
puts "Exception parsing datetime: #{ex.to_s} Value: #
{value} Timezone: #{timezone}"
value
end
end # Dates, Date-Times & Times
# NOTE: invalid dates & times will be returned as strings...
def parse_datetime(name, params, value)
#puts ""
#puts name
#puts params
#puts value
timezone =""
begin
dt = DateTime.parse(value)
# if we have timezone information, use it to convert to UTC
if !value.index("TZID=").nil?
startIndex = value.index("TZID=")
endIndex = value.index(":")
timezone = value[startIndex+5..endIndex-1]
if timezone =="(GMT-05.00) Eastern Time (US & Canada)"
timezone = "US/Eastern"
end
timezone.gsub!("-","/")
if timezone == "Pacific"
timezone="US/Pacific"
end
#puts "Timezone: #{timezone}"
tz = TZInfo::Timezone.get(timezone)
#puts "Before fixing UTC: #{dt}"
dt = tz.local_to_utc(DateTime.new
(dt.year,dt.month,dt.day,dt.hour,dt.min,dt.sec))
#puts "After fixing UTC: #{dt} (Local is: #{tz.utc_to_local
(dt)})"
end
dt
rescue Exception => ex
puts "Exception parsing datetime: #{ex.to_s} Value: #
{value} Timezone: #{timezone}"
value
end
end
-------------------------------------------------------
Principal
OpenSource Connections
Site: http://www.opensourceconnections.com
Blog: http://blog.opensourceconnections.com
Cell: 1-434-466-1467