473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Adjusting for Timezone

I'm (still) working on an ISO 8601 date parser. I want to convert at least
the formats described here:

http://www.w3.org/TR/NOTE-datetime

Well.. I've got most of it working (via RegEx's) good enough for me but I'm
having a brain block when it comes to TimeZone.

The datetime may come with an optional timezone offset (from GMT) as here
(the offset is +1 hour from GMT):

1997-07-16T19:20:30+01:00

So... that's the current time there... but my spec is that timezone
information, if present, should be used to convert the given time to local
time. For me, for example, this would be -5:00 from GMT.

I've already split the Timezone information: I've got indiviual access to
the sign (plus or minus), the hour offset and the minute offset.

So I'm sure there's a nice, simple formula for this... but it eludes me. Or
I'm lazy... I can't decide which.

Any pointers?

Thanks in advance,

Jim Davis
Aug 30 '05 #1
7 3181
JRS: In article <df*********@news2.newsguy.com>, dated Tue, 30 Aug 2005
11:58:36, seen in news:comp.lang.javascript, Jim Davis
<ne********@vboston.com> posted :
I'm (still) working on an ISO 8601 date parser.
Please do not start a new thread to continue an existing discussion. As
a thread develops, it is taken for granted that some progress may be
made.

The datetime may come with an optional timezone offset (from GMT) as here
(the offset is +1 hour from GMT):

1997-07-16T19:20:30+01:00

So... that's the current time there... but my spec is that timezone
information, if present, should be used to convert the given time to local
time. For me, for example, this would be -5:00 from GMT.

I've already split the Timezone information: I've got indiviual access to
the sign (plus or minus), the hour offset and the minute offset.


There are 60 minutes in each hour; I see no other possible difficulty.
In fact, you do not even need to know that. Perhaps you should explain
your problem more clearly.

Remember that you should not use new Date(Y, M, D, h, m, s, ms) as
that muddles the situation with your local zone. Use new
Date(Date.UTC(,,,,,,)) .

Read the FAQ; see below. Use extant resources before reinventing
wheels.

Consider
new Date(Date.UTC(Y, M, D, h+oh, m+om, s, c))
where oh, om are the offsets and maybe + should be - ; that's not
explicit in the cited resource but should be obvious after reading it.

Consider whether the input should be validated; perhaps not, if your
data source is reliable.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 30 '05 #2
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:+n**************@merlyn.demon.co.uk...
JRS: In article <df*********@news2.newsguy.com>, dated Tue, 30 Aug 2005
11:58:36, seen in news:comp.lang.javascript, Jim Davis
<ne********@vboston.com> posted :
I'm (still) working on an ISO 8601 date parser.
Please do not start a new thread to continue an existing discussion. As
a thread develops, it is taken for granted that some progress may be
made.


New topic... new thread. Seems simple to me.

While this topic may be related to another it has both greater specificity
and broader appeal (for adjusting timezones is only one small part of
converting iso8601 dates and applicable to much more than iso8601 dates).
Any answers may inform the other but are not part of the same discussion.

Honestly it seems like you're being rather pendantic about this (or in
general I don't you so perhaps it's that). For archiving purposes
specific,searchable subjects are generally respected on Usenet.
The datetime may come with an optional timezone offset (from GMT) as here
(the offset is +1 hour from GMT):

1997-07-16T19:20:30+01:00

So... that's the current time there... but my spec is that timezone
information, if present, should be used to convert the given time to local
time. For me, for example, this would be -5:00 from GMT.

I've already split the Timezone information: I've got indiviual access to
the sign (plus or minus), the hour offset and the minute offset.


There are 60 minutes in each hour; I see no other possible difficulty.
In fact, you do not even need to know that. Perhaps you should explain
your problem more clearly.


In other words actually obtaining the offset is not the problem (I do not
need help in extracting it). My question concerned the conversion of one
timezone to another.
Remember that you should not use new Date(Y, M, D, h, m, s, ms) as
that muddles the situation with your local zone. Use new
Date(Date.UTC(,,,,,,)) .
Well... only if I want UTC time. ;^)

My spec is clear that, if timezone information provided, it should be used
to convert the time to local time.
Read the FAQ; see below. Use extant resources before reinventing
wheels.
Perhaps I'm missing it. I see nothing in the FAQ that concerns this. A
rather extensive search didn't produce the answer I needed.

I'm neither too proud or too stupid to existing code when I can find it and
when it's appropriately licensed.
Consider
new Date(Date.UTC(Y, M, D, h+oh, m+om, s, c))
where oh, om are the offsets and maybe + should be - ; that's not
explicit in the cited resource but should be obvious after reading it.
That does not convert a date... it provides the UTC date.
Consider whether the input should be validated; perhaps not, if your
data source is reliable.


Not an issue for puposes of discussion.
I thank you for your effort, however. I've since solved the problem and
will shortly post a resolution.

It really should have have clearer, but I was having a mental block on
this... once I got a nap in it became much clearer.

Jim Davis
Aug 31 '05 #3
Well, I took a nap and then was able to figure it out.

There are two general methods for doing this. I'll be used the term
"source" in relation to the given time and "target" in relation to the local
time zone (so the problem becomes "how to convert a source timezone to a
target timezone):

1) A two stage conversion. Step one uses the source timezone offset to
generate a UTC datetime. We then use the target offset to generate the
local time.

This works just fine... but it's rather inelegant in my opinion (needing the
middle-man irks me).

2) Do the basic math (never a personal strong point). Subtracting the
target from the source gives us our answer. For example:

Source offset is +1:00, target is -4:00: (+1 - -4) = 5
Source offset is -5:00, target is -5:00: (-5 - -5) = 0
Source offset is -7:00, target is 2:00: (-7 - +2) = -9

And so forth. The resulting number is the offset between the source date
and the target date: modify the target by the result and the conversion is
complete.

I hope this helps somebody else. ;^)

Jim Davis
Aug 31 '05 #4
Jim Davis wrote:
I'm (still) working on an ISO 8601 date parser. I want to convert at least
the formats described here:

http://www.w3.org/TR/NOTE-datetime

Well.. I've got most of it working (via RegEx's) good enough for me but I'm
having a brain block when it comes to TimeZone.

The datetime may come with an optional timezone offset (from GMT) as here
(the offset is +1 hour from GMT):

1997-07-16T19:20:30+01:00

So... that's the current time there... but my spec is that timezone
information, if present, should be used to convert the given time to local
time. For me, for example, this would be -5:00 from GMT.

I've already split the Timezone information: I've got indiviual access to
the sign (plus or minus), the hour offset and the minute offset.

So I'm sure there's a nice, simple formula for this... but it eludes me. Or
I'm lazy... I can't decide which.

Any pointers?
The sample below converts a timestamp to local time. If a zone is
present, it is used to create a local time based on the offset.

Each component must be a proper 'bit' of the datetime in the format
you've specified, i.e. the date must be yyyy-m-d, the time must be
Thh:mm:ss and the zone must be +/-hh:mm. It will tolerate not having
leading zeros.

How you handle not having some parts of the date can done elsewhere,
e.g. if you get '2005' you can decide if you want to make it 2005-01-01
or 2005-currentMonth-currentDate, it's up to you. Same with the time.
If no timezone offset is specified, it will be the local one.

Decimal seconds in the input time are removed.

The input date separator can be '-' or '/' as hyphens are converted to
slashes.

<form action="">
<input type="text" name="time" value="1997-07-16T19:20:30.45+01:00"
size="50">
<input type="button" value="Convert" onclick="

var x, tz, tStr = this.form.time.value;

// Get timezone
tz = tStr.match(/[\+\-]\d{1,2}:\d{1,2}$/);

// Remove timezone
tStr = tStr.replace( tz, '');

// Split date and time
tStr = tStr.split('T');

// Format date string
x = tStr[0].replace(/-/g,'/');

// If there's a time
// Remove decimal seconds and append it to the date string
if ( tStr[1] ) x += ' ' + tStr[1].match(/^[^\.]+/);

// Add timezone offset if there is one
if ( tz ) x += ' ' + tz;

// Generate a date object using the string
var localTime = new Date( x );

// Show the world what we've done
var msg = document.getElementById('zz')
msg.innerHTML = 'Datetime string: ' + x;
msg.innerHTML += '<br>Timezone: ' + (tz || '<i>No timezone</i>');
msg.innerHTML += '<br>Local time: ' + localTime.toString();

">
<br>
<input type="reset">
</form>
<p id="zz"></p>



Thanks in advance,

Jim Davis

--
Rob
Aug 31 '05 #5

"RobG" <rg***@iinet.net.au> wrote in message
news:43***********************@per-qv1-newsreader-01.iinet.net.au...

Thanks for this!

Truly - above and beyond. ;^)

Jim Davis
Aug 31 '05 #6
JRS: In article <+n**************@merlyn.demon.co.uk>, dated Tue, 30
Aug 2005 23:59:45, seen in news:comp.lang.javascript, Dr John Stockton
<jr*@merlyn.demon.co.uk> posted :
...

Update : omit "maybe", and in the same line "not" -> "now".

function TryISO(F) { var OK, S
S = F.ISO.value
OK = /^(\d{4})?-?(\d\d)?-?(\d\d)?T?(\d\d)?:?(\d\d)?:?(\d\d)?([+-])?(\d\d)?:?(\d\d)?$/.test(S)
with (RegExp) F.DT.value = !OK ? "BAD" : new Date(Date.UTC(
$1||77, $2-1, $3, $4 - ($7+$8), $5 - ($7+$9)||0, $6
)).UTCstr() }

F is a form. Those who don't have .UTCstr() can omit it. 77 is arbitrary!

One day, I may learn what's preferred to such use of $1..$9.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 31 '05 #7
JRS: In article <df********@news3.newsguy.com>, dated Tue, 30 Aug 2005
21:47:48, seen in news:comp.lang.javascript, Jim Davis
<ne********@vboston.com> posted :
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:+n**************@merlyn.demon.co.uk...
JRS: In article <df*********@news2.newsguy.com>, dated Tue, 30 Aug 2005
11:58:36, seen in news:comp.lang.javascript, Jim Davis
<ne********@vboston.com> posted :
I'm (still) working on an ISO 8601 date parser.
Please do not start a new thread to continue an existing discussion. As Honestly it seems like you're being rather pendantic about this (or in
general I don't you so perhaps it's that).
Please read what you write before expecting others to do so. You can
then adjust it to be potentially comprehensible.

So... that's the current time there... but my spec is that timezone
information, if present, should be used to convert the given time to local
time. For me, for example, this would be -5:00 from GMT.

That seems strange. A datum such as 1111-11-11 22:22:22 is in local
time, but there remains the question of which locality. The processer's
local offset is not necessarily the offset at the location of the data.

Timezone information enables conversion to UTC. Conversion to own local
is a further, albeit trivial in javascript, step.

Remember that you should not use new Date(Y, M, D, h, m, s, ms) as
that muddles the situation with your local zone. Use new
Date(Date.UTC(,,,,,,)) .


Well... only if I want UTC time. ;^)


No; if the computer's timezone is not the data's timezone. First
determine the meaning of the data, then consider the presentation.
My spec is clear that, if timezone information provided, it should be used
to convert the time to local time.


If that's so, you go beyond ISO 8601.

Read the FAQ; see below. Use extant resources before reinventing
wheels.


Perhaps I'm missing it. I see nothing in the FAQ that concerns this. A
rather extensive search didn't produce the answer I needed.

I'm neither too proud or too stupid to existing code when I can find it and
when it's appropriately licensed.


As above.

Consider
new Date(Date.UTC(Y, M, D, h+oh, m+om, s, c))
where oh, om are the offsets and maybe + should be - ; that's not
explicit in the cited resource but should be obvious after reading it.


That does not convert a date... it provides the UTC date.


"Consider" is a recommendation that you read and think about it. It
does not mean that you can use it directly. That statement in fact
gives a Date Object which holds the instant given by the arguments
interpreted as UTC, but default conversion to string uses your local
timezone.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Aug 31 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason Kinkade | last post by:
I recently installed MySQL 4.0.18 on my slackware 9.1 machine (kernel 2.4.25) by compiling the source. It works fine, and all tests pass except one. The timezone test. Can someone tell me why...
1
by: cnliou | last post by:
Hi! If I correctly understand v7.4 manual, value, say, '2003-11-26 12:00' in TIMESTAMP WITHOUT TIMEZONE column should output '2003-11-26 19:00' for "+08:00" timezone. The following test...
1
by: Raj Chudasama | last post by:
I have the following code for a clock in my gui. Everything works fine EXCEPT the following line when the TimeZone is changed in the windows. string tz = TimeZone.CurrentTimeZone.StandardName;...
6
by: Bijoy Naick | last post by:
I have an events table which stores the time of each event - the time and assoicated timezone. Is there a way of converting this time into GMT (with support for DST).. some sort of function which...
2
by: David Garamond | last post by:
When a timestamp string input contains a timezone abbreviation (CDT, PST, etc), which timezone offset is used? The input date's or today date's? The result on my computer suggests the latter. #...
13
by: Bruno Wolff III | last post by:
Recently there has been some discussion about attaching a timezone to a timestamp and some other discussion about including a 'day' part in the interval type. These two features impact each other,...
3
by: asanford | last post by:
I want to create a web service that allows the caller to pass a DateTime to the web service (that is, create a web method such as void MyWebMethod(DateTime dt).) However, I want to be able to...
2
by: Sanjay | last post by:
Hi All, Using pytz, I am facing a problem with Asia/Calcutta, described below. Asia/Calcutta is actually IST, which is GMT + 5:30. But while using pytz, it was recognized as HMT (GMT + 5:53)....
2
by: =?Utf-8?B?Sm9ubnk=?= | last post by:
I have an ASP.NET 2.0 C# web application that is contacting an Exchange server using WEBDAV. It allows the users to look up appointments for a future date. The problem I have is determining the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.