timezone interview questions
Top timezone frequently asked interview questions
I have parsed a java.util.Date
from a String
but it is setting the local time zone as the time zone of the date
object.
The time zone is not specified in the String
from which Date
is parsed. I want to set a specific time zone of the date
object.
How can I do that?
Source: (StackOverflow)
I am looking for a function to convert date in one timezone to another.
It need two parameters,
- date (in format "2012/04/10 10:10:30 +0000")
- timezone string ("Asia/Jakarta")
The timezone string is described in http://en.wikipedia.org/wiki/Zone.tab
Is there an easy way to do this?
Source: (StackOverflow)
I am confused while using php to handle date/time.
What I am trying to do is this: When a user visits my page I am asking his timezone and then displaying the 'day of week' in his timezone.
I don't want to use the browser's day.
I want to do this calculation in php.
This is how I am trying to achieve it:
- The timezone entered by user
- Unix time stamp calculated by php time() function.
But I dont know how to proceed...
How would i get the 'day of week' in this timezone.
Source: (StackOverflow)
I want the server to always serve dates in UTC in the html, and have javascript on the client site convert it to the user's local timezone.
Bonus if I can output in the user's locale date format.
Source: (StackOverflow)
If I run the following program, which parses two date strings referencing times one second apart and compares them:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str3 = "1927-12-31 23:54:07";
String str4 = "1927-12-31 23:54:08";
Date sDt3 = sf.parse(str3);
Date sDt4 = sf.parse(str4);
long ld3 = sDt3.getTime() /1000;
long ld4 = sDt4.getTime() /1000;
System.out.println(ld4-ld3);
}
The output is:
353
Why is ld4-ld3
not 1
(as I would expect from the one-second difference in the times), but 353
?
If I change the dates to times one second later:
String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";
Then ld4-ld3
will be 1
.
Java version:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)
Timezone(TimeZone.getDefault()
):
sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
offset=28800000,dstSavings=0,
useDaylight=false,
transitions=19,
lastRule=null]
Locale(Locale.getDefault()): zh_CN
Source: (StackOverflow)
I am hoping to make this question and the answers to it the definitive guide to dealing with daylight saving time, in particular for dealing with the actual change overs.
If you have anything to add, please do
Many systems are dependent on keeping accurate time, the problem is with changes to time due to daylight savings - moving the clock forward or backwards.
For instance, one has business rules in an order taking system that depend on the time of the order - if the clock changes, the rules might not be as clear. How should the time of the order be persisted? There are of course an endless number of scenarios - this one is simply an illustrative one.
- How have you dealt with the daylight saving issue?
- What assumptions are part of your solution? (looking for context here)
As important, if not more so:
- What did you try that did not work?
- Why did it not work?
I would be interested in programming, OS, data persistence and other pertinent aspects of the issue.
General answers are great, but I would also like to see details especially if they are only available on one platform.
Source: (StackOverflow)
Is there a standard way for a Web Server to determine what time zone offset a user is in?
Perhaps from a HTTP
header? Or part of the user-agent string?
Source: (StackOverflow)
Currently we have a standard way of dealing with .net DateTimes in a TimeZone aware way: Whenever we produce a DateTime we do it in UTC (e.g. using DateTime.UtcNow), and whenever we display one, we convert back from UTC to the user's local time.
That works fine, but I've been reading about DateTimeOffset and how it captures the local and UTC time in the object itself. So the question is, what would be the advantages of using DateTimeOffset vs what we have already been doing?
Source: (StackOverflow)
When I create a new Date
object, it is initialized to the current time but in the local timezone. How can I get the current date and time in GMT?
Source: (StackOverflow)
From the server I get a datetime variable in this format: 6/29/2011 4:52:48 PM
and it is in UTC time. I want to convert it to the current user's browser time using JavaScript.
How this can be done using JavaScript or jQuery?
Source: (StackOverflow)
I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date constructor that takes numbers then I get a Date object for my current timezone:
new Date(xiYear, xiMonth, xiDate)
Give the correct date but it thinks that date is GMT+01:00 due to daylight savings time.
The problem here is that I then give this Date to an Ajax method and when the date is deserialised on the server it has been converted to GMT and so lost an hour which moves the day back by one.
Now I could just pass the day, month, and year individually into the Ajax method but it seems that there ought to be a better way.
The accepted answer pointed me in the right direction, however just using setUTCHours
by itself changed:
Apr 5th 00:00 GMT+01:00
to
Apr 4th 23:00 GMT+01:00
I then also had to set the UTC date, month and year to end up with
Apr 5th 01:00 GMT+01:00
which is what I wanted
Source: (StackOverflow)
I am trying to subtract one date value from the value of datetime.today()
to calculate how long ago something was. But it complains:
TypeError: can't subtract offset-naive and offset-aware datetimes
The value datetime.today()
doesn't seem to be "timezone aware", while my other date value is. How do I get a value of datetime.today()
that is timezone aware? Right now it's giving me the time in local time, which happens to be PST, i.e. UTC-8hrs. Worst case, is there a way I can manually enter a timezone value into the datetime
object returned by datetime.today()
and set it to UTC-8? Of course, the ideal solution would be for it to automatically know the timezone.
Source: (StackOverflow)
I got this error when I requested to update the PHP version from 5.2.17 to PHP 5.3.21 on the server.
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 86</p>
</div>
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 86
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 99
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 99</p>
</div>
Source: (StackOverflow)