0

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.

Khaled
  • 35,688
  • 8
  • 69
  • 98

1 Answers1

1

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.

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • 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 08: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 08: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 08:36