473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3194
JRS: In article <df*********@ne ws2.newsguy.com >, dated Tue, 30 Aug 2005
11:58:36, seen in news:comp.lang. javascript, Jim Davis
<ne********@vbo ston.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 30 '05 #2
"Dr John Stockton" <jr*@merlyn.dem on.co.uk> wrote in message
news:+n******** ******@merlyn.d emon.co.uk...
JRS: In article <df*********@ne ws2.newsguy.com >, dated Tue, 30 Aug 2005
11:58:36, seen in news:comp.lang. javascript, Jim Davis
<ne********@vbo ston.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,search able 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.getEle mentById('zz')
msg.innerHTML = 'Datetime string: ' + x;
msg.innerHTML += '<br>Timezone: ' + (tz || '<i>No timezone</i>');
msg.innerHTML += '<br>Local time: ' + localTime.toStr ing();

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



Thanks in advance,

Jim Davis

--
Rob
Aug 31 '05 #5

"RobG" <rg***@iinet.ne t.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.dem on.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 31 '05 #7
JRS: In article <df********@new s3.newsguy.com> , dated Tue, 30 Aug 2005
21:47:48, seen in news:comp.lang. javascript, Jim Davis
<ne********@vbo ston.com> posted :
"Dr John Stockton" <jr*@merlyn.dem on.co.uk> wrote in message
news:+n******* *******@merlyn. demon.co.uk...
JRS: In article <df*********@ne ws2.newsguy.com >, dated Tue, 30 Aug 2005
11:58:36, seen in news:comp.lang. javascript, Jim Davis
<ne********@vbo ston.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
informatio n, 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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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
1342
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 I'm getting this error? Is there something with the TZ variable I have to set for the shell? I think its EDT right now. timezone ...
1
4013
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 results seem to be somewhat unexpected. Restting OS timezone (/etc/timezone and /etc/localtime in Linux) does not make the results more comfortable.
1
2545
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; It will not pick the changed timezone but it does pick up time change due to timezone change.
6
2459
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 accepts a time and timezone as parameters and returns the time in GMT?
2
4748
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. # create table ts (ts timestamptz); # insert into ts values ('2004-10-17 00:00:00 CDT'); -- UTC-5 # insert into ts values ('2004-11-17 00:00:00 CDT');...
13
2832
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, since if you add a 'day' to a timestamp the result can depend on what timezone the timestamp is supposed to be in. It probably makes more sense to...
3
8313
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 capture the TimeZone of the caller as well - but I don't want the interface to specify an xs:string as the method argument - I want it to use an...
2
11690
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). While I digged into the oslan database, I see the following: # Zone NAME GMTOFF RULES FORMAT Zone Asia/Calcutta 5:53:28 - LMT 1880 # Kolkata
2
3676
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 correct client time zone. Note that Exchange stores everything in UTC time. I am able currently able to use javascript to ask the client what time...
0
7527
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.