I am unsure, how to get from this code above to apply the relevant time
zone bias to the time as shown in the example below. My code at present
will output:
15/08/2010 05:46:50
This is not what I want because it does not understand this is a BST
time and it needs to be added by 1 hour.
I was wondering if I could ask for some advice regarding dates. I have a
file that contains lots of date values formatted like so:
Sunday, 15 August 2010 05:46:50 o'clock BST
I want basically out these dates as follows, applying the necessary time
zone bias:
I have once done some time zone conversion in ruby. My apologies, I do
not have the time to read your post carefully. I will just attach the
small program here. It uses the tzinfo. (I cannot remember if that is
inbuilt or is a gem).
The full program is a bit long (attached) since i parse various command
line parameters but here's the meat of it. It does various conversions
in a loop.
tz = TZInfo::Timezone.get(Choice.choices[:from])
tz1 = TZInfo::Timezone.get('Asia/Calcutta') #local = tz.utc_to_local(Time.utc(2008,9,4,14,00,0))
unparsed.each {|timestr|
puts timestr
local = tz.local_to_utc(Time.parse(timestr))
puts tz1.utc_to_local(local)
}
(you would not need to loop. unparsed refers to ARGV on command line)
tz = TZInfo::Timezone.get(Choice.choices[:from])
tz1 = TZInfo::Timezone.get('Asia/Calcutta') #local = tz.utc_to_local(Time.utc(2008,9,4,14,00,0))
unparsed.each {|timestr|
puts timestr [1]
local = tz.local_to_utc(Time.parse(timestr))
puts tz1.utc_to_local(local) [2]
}
(you would not need to loop. unparsed refers to ARGV on command line)
Please note in the above program, i am always converting incoming time
to time in Asia/Calcutta. However, the incoming time (time "from" can be
passed on the command line).
tz is time_zone from
tz1 is time_zone to (calcutta)
[1] is what the user entered such as "12:00 AM"
[2] is converted time in Asia/Cal.
tz = TZInfo::Timezone.get(Choice.choices[:from])
tz1 = TZInfo::Timezone.get('Asia/Calcutta') #local = tz.utc_to_local(Time.utc(2008,9,4,14,00,0))
unparsed.each {|timestr|
puts timestr [1]
local = tz.local_to_utc(Time.parse(timestr))
puts tz1.utc_to_local(local) [2]
}
(you would not need to loop. unparsed refers to ARGV on command line)
Please note in the above program, i am always converting incoming time
to time in Asia/Calcutta. However, the incoming time (time "from" can be
passed on the command line).
tz is time_zone from
tz1 is time_zone to (calcutta)
[1] is what the user entered such as "12:00 AM"
[2] is converted time in Asia/Cal.
I hope this helps.
Thanks a lot for getting back to me. The first problem I have noticed
but have sorted out is the format of my date using the following code I
first convert - "Sunday, 15 August 2010 05:46:50 o'clock BST" into this
- "Sun, 15 Aug 2010 05:46:50 +0100"
d = "Sunday, 15 August 2010 05:46:50 o'clock BST"
dateparts = ParseDate.parsedate("#{d}")
d =
Time.mktime("#{dateparts[0]}","#{dateparts[1]}","#{dateparts[2]}","#{dateparts[3]}","#{dateparts[4]}","#{dateparts[5]}")
done = d.strftime("%a, %d %b %Y %H:%M:%S %Z")
I then go into the code you suggested:
me =TZInfo::Timezone.get('Europe/London')
utctime = Time.parse(done).utc
puts me.utc_to_local(utctime)