Hey all,
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:
15/08/2010 06:46:50 (because it is BST we add the hour)
At present I am struggling a little to achieve this, in particular the
tim zone offset. At present I have the following code:
dateparts = ParseDate.parsedate("#{myDate}")
myDate =
Time.mktime("#{dateparts[0]}","#{dateparts[1]}","#{dateparts[2]}","#{dateparts[3]}","#{dateparts[4]}","#{dateparts[5]}")
myDate = myDate.strftime("%d/%m/%Y %H:%M:%S")
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 would appreciate any help.
Stuart C. wrote:
Hey all,
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).
http://tzinfo.rubyforge.org/doc/files/README.html
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)
See for full: http://gist.github.com/540828
reg
rahul
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.
Stuart C. wrote:
Hey all,
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
Just sub out the “o’clock” and you should be fine.
require ‘time’
=> true
t = Time.parse(“Sunday, 15 August 2010 05:46:50 BST”)
=> Sun Aug 15 05:46:50 +0100 2010
t.gmtime
=> Sun Aug 15 04:46:50 UTC 2010
t.localtime
=> Sun Aug 15 05:46:50 +0100 2010
t.zone
=> “BST”
Note that t stores the universal (UTC/GMT) time, but remembers the local
zone is BST; from that you can format and print either.
Internally they are both represented as in UTC as the number of seconds
from midnight on Jan 1, 1970.
t.to_i
=> 1281847610
t.gmtime.to_i
=> 1281847610
t.localtime.to_i
=> 1281847610
t.gmtoff
=> 3600
I want basically out these dates as follows, applying the necessary time
zone bias:
15/08/2010 06:46:50 (because it is BST we add the hour)
I think you have made a mistake there.
If the time is 05:46:50 BST, as the original string said, then that’s
the time in BST.
Since BST is GMT+1 (in the summer anyway), then the time is also
04:46:50 in GMT.
It is 06:46:50 local time in France (Western European Time, GMT+2 in
summer)
HTH,
Brian.
R… Kumar 1.9.1 OSX wrote:
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)
This then gives me
Sun Aug 15 06:46:50 UTC 2010
Is my code and logic correct?
Many thanks
Brian C. wrote:
Just sub out the “o’clock” and you should be fine.
require ‘time’
=> true
t = Time.parse(“Sunday, 15 August 2010 05:46:50 BST”)
=> Sun Aug 15 05:46:50 +0100 2010
t.gmtime
=> Sun Aug 15 04:46:50 UTC 2010
t.localtime
=> Sun Aug 15 05:46:50 +0100 2010
t.zone
=> “BST”
I want basically out these dates as follows, applying the necessary time
zone bias:
15/08/2010 06:46:50 (because it is BST we add the hour)
I think you have made a mistake there.
If the time is 05:46:50 BST, as the original string said, then that’s
the time in BST.
Since BST is GMT+1 (in the summer anyway), then the time is also
04:46:50 in GMT.
It is 06:46:50 local time in France (Western European Time, GMT+2 in
summer)
HTH,
Brian.
Yeah sorry, got my words tangled up regarding + and - of hours.
I will give this a try.
Many thanks