Noda for Handling DST

127 views
Skip to first unread message

lakshma...@gmail.com

unread,
Apr 25, 2013, 7:54:58 AM4/25/13
to noda...@googlegroups.com
Hi Team,
 
I have few queries. Request you to clarify on the following .
 
  • Can Noda be used to handle Timezone Issues with DST in a Web Application, where the Server is in one location and the application is accessed world wide.
  • How to identify client Time Zone using Noda.
  • Can we convert the client time to UTC in Noda? If yes, how? Do we have to pass any information from the Browser/client side?
  • Where can I find the working example where the DST and other time zone related issues are handled?
 
Regards,
Lakshman

Jon Skeet

unread,
Apr 25, 2013, 8:06:21 AM4/25/13
to Noda Time
On 25 April 2013 12:54, <lakshma...@gmail.com> wrote:
I have few queries. Request you to clarify on the following .
 
  • Can Noda be used to handle Timezone Issues with DST in a Web Application, where the Server is in one location and the application is accessed world wide.

Absolutely. But you'll need to give us more information about what you're trying to do for us to help you in a concrete way. (Asking a detailed question on Stack Overflow would probably be the best idea.) Noda Time doesn't use the system-local time zone anywhere by default.
  • How to identify client Time Zone using Noda.
This is currently unsupported, assuming that by "client" you mean "the machine running a web browser which talks to a server, where only the server is running any Noda Time code". You need to look into Javascript solutions.
  • Can we convert the client time to UTC in Noda? If yes, how? Do we have to pass any information from the Browser/client side?
From Javascript, it's easier to get the UTC time than the local time, to be honest. But you would need to do it in Javascript... which should make it easy to find the current "milliseconds since the unix epoch". (Then use Instant.FromMillisecondsSinceUnixEpoch to construct an appropriate Instant.)
  • Where can I find the working example where the DST and other time zone related issues are handled?
You'd need to give a more precise definition of what you mean by "handling DST and other time zone related issues" for us to help you. But look in the API documentation for DateTimeZone, and also in the user guide - both give details of converting from LocalDateTime to ZonedDateTime.

Basically you need to work out exactly what you're trying to accomplish first - once you've done that, it should be fairly easy to provide code samples.

Jon

Matt Johnson

unread,
Apr 25, 2013, 6:15:48 PM4/25/13
to noda...@googlegroups.com
As a suggestion, you might want to take the approach I commonly use:

1) Since their is no 100% reliable way to determine an end-user's time zone through the browser, you should allow the user to pick their time zone somewhere in the application.  Because the list of TZDB zones is so large, I like to use a map-based timezone picker, such as this one:  https://github.com/dosx/timezone-picker

2) To feed a "best-guess" default starting value into the picker, I use the jsTimeZoneDetect library: https://bitbucket.org/pellepim/jstimezonedetect.  You cannot just use this value without letting your users somehow change it if necessary, because as good as this library is, they fully understand and admit int the documentation that it is just a guess.  There's no way to be 100% certain through just detection algorithms.

3) I then use the detected or selected time zone id when calculating dates in my NodaTime logic.  It is the key used to look up the correct DateTimeZone from the TZDB implementation in NodaTime.

There are other methods, but this is what I do.  YMMV.

One other method would be if you have a mobile device and can accurately get GPS coordinates, you could resolve the location to a time zone using one of the methods described here: http://stackoverflow.com/q/16086962/634824

-Matt

Lakshman Raju

unread,
Apr 26, 2013, 1:39:31 AM4/26/13
to noda...@googlegroups.com
Matt, Thanks for your time and also for the approach. Unfortunately we will not be able to use the approach as we cannot ask the user to manually select his time zone. It would be great if there is a way to identify the the user's timezone automatically. I have gone through the jsTimeZoneDetect library, they basically match the offset obtained with the set of predefined timezones which they have. There are chances of the time zone not being detected properly when the Daylight Saving comes into play in some zones. Anyway, if there aren't any direct ways of automatically detecting the time zone, the below approach will be my only saviour.
 
@Jon, I am working on handling the Timezone related challenges in a web application. I have to capture values from some Daytime fields from the user and then save the Daytime values in the UTC format. While displaying the values to the user, I must make sure that the values are displayed with respect to his current time zone. While handling this, I must also make sure that the Daylight Savings will not be having any impact while Converting the values to and from the UTC format.
 
Once again, Thank you people for your time, I really appreciate it :)
 
-Lakshman

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "Noda Time" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/noda-time/YWoySzx4s8A/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to noda-time+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matt Johnson

unread,
Apr 26, 2013, 10:28:57 AM4/26/13
to noda...@googlegroups.com, lakshma...@gmail.com
If you want to actually know what time zone the user is in, there is no direct way to get it.  Despite all of the advances of modern JavaScript, we do not yet have functions to query the browser for the ID of the time zone set on the computer.  Detection is often acceptable, but if you want to be precise then you need to provide your users a timezone picker of some sort.

However, if all you are interested in is displaying the correct time, there is a much easier solution.  JavaScript will properly convert to the local date and time.  That's actually its default functionality.  You simply have to provide it an ISO string with an offset or a Z so it knows how to adjust the input.  If you are targeting IE9+ then the built-in Date object will properly parse ISO strings and display them in the local time zone.  The only thing it can't do is talk about times in other time zones.

To support older browsers, and to get around many other quirks with the JS Date object, I highly recommend using the moment.js library.  www.momentjs.com.  It also provides excellent formatting, parsing, and localization.

So, in summary, if all you care about is displaying a UTC datetime in the correct local timezone of the browser, you really don't need NodaTime at all.  You can just send the browser a UTC datetime as an ISO string such as 2013-04-26T14:23:00Z.  Load that with moment.js, and then format and display it with moment.js also.  It will convert to local time automatically.  If you are collecting data from the user, you can also use momentjs to parse that input in javascript, and then ask for the UTC value back to send to the server as an ISO string again.

The scenarios that require knowing the time zone are usually where one user might be working with another user's time.  NodaTime is essential in completing that loop.  But it doesn't sound like you have that concern.

aul...@gmail.com

unread,
Apr 2, 2014, 6:44:25 AM4/2/14
to noda...@googlegroups.com, lakshma...@gmail.com
This is a great article for general guidance for dealing with client side date time.  Thanks very much.
Reply all
Reply to author
Forward
0 new messages