Dealing with dates & timezones

S

Stuart Clarke

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.
 
R

R.. Kumar 1.9.1 OSX

Stuart said:
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
 
R

R.. Kumar 1.9.1 OSX

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.
 
B

Brian Candler

Stuart said:
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.
=> "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.
=> 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.
 
S

Stuart Clarke

R.. Kumar 1.9.1 OSX said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top