Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Java application that depends on date/time of the system to check for new and old email messages.

While I was checking the Java application log, I found that the server timezone has changed. This caused the application to fail in doing its job.

What could be the reason of this sudden change in the server timezone? How can I prevent such problems in the future.

I am using ubuntu server 8.04.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Does your application check the time by checking Unix time (time_t, seconds since the epoch), or does it check just human-expressed time, neglecting the timezone?

If the latter, I fear you may have made a coding error. The timezone can be set and changed on a process-by-process basis, as easily as changing the TZ environment variable; try

export TZ=BST
date
export TZ=EST
date

Note that this is an environment variable, and thus inheritable by the children of the running process. If the java process happens to be restarted by someone who has TZ set to something you didn't expect, and your startup file doesn't force TZ, then java will be running with an unexpected timezone.

TZ is by no means the only way of setting the timezone; I wanted to mention this to show you that Unix is designed to allow the timezone to be a personal convenience, much like your shell prompt or your PATH. If you need a process to have a particular view of timezone, you need to make sure that it sets that when it starts up.

share|improve this answer
    
I am starting the Java application using cron job. I am not specifying the timezone using TZ variable. So, it should get it from the system timezone. Your idea is helpful to set the timezone before starting the process. However, I would prefer to have the timezone unchanged in the first place to avoid similar problems. –  Khaled Oct 27 '10 at 8:27
    
The default constructor for Java.util.Date() returns the current time in milliseconds since the "epoch". It is designed to represent time in UTC not in the local timezone. Your program must be converting this to a representation in the local timezone - it is the Java program that is not doing what you wish. If you wish the program to pick up a configured timezone rather than the invoking user's timezone you should have the program rewritten to read a timezone from a configuration file. –  RedGrittyBrick Oct 27 '10 at 8:36
    
I understand that. The problem is, you can't stop people setting TZ for themselves, and if they do, and restart java, and java doesn't set TZ, it'll inherit the user's preference. So your java startup file really should sanitise the environment; better still would be for java not to neglect the timezone when calculating the time. –  MadHatter Oct 27 '10 at 8:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.