Bug 1562837

Summary: Setting the TZ environment variable breaks TimeZone in Java
Product: Red Hat Enterprise Linux 8 Reporter: Deepu K S <dkochuka>
Component: java-1.8.0-openjdkAssignee: Andrew John Hughes <ahughes>
Status: CLOSED MIGRATED QA Contact: OpenJDK QA <java-qa>
Severity: medium Docs Contact:
Priority: medium    
Version: 8.2CC: ahughes, cww, dbhole, jandrlik, jdoyle, jvanek, jwright, mmillson, vpakolu
Target Milestone: rcKeywords: MigratedToJIRA, Triaged
Target Release: 8.4   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2023-09-12 21:51:43 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1594286, 1894575    

Description Deepu K S 2018-04-02 16:29:15 UTC
Description of problem:
If the TZ environment variable is set to the absolute filepath of the TimeZone, java fall back to setting the timezone in GMT time. 
This caused incorrect times to be reported in a java/tomcat application. Every time when daylight savings time is active, between March and November in the US.

Version-Release number of selected component (if applicable):
Red Hat Enterprise Linux 7.4
java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64
tzdata-java-2018d-1.el7.noarch
tzdata-2018d-1.el7.noarch

Also reproduced on java-1.7.0-openjdk and java-1.6.0-openjdk .

How reproducible:
Always

Steps to Reproduce:
1. # cat GetDate.java:
import java.util.Date;

class GetDate {
  /** Print a hello message */
  public static void main(String[] args) {
    Date today = new Date();
    System.out.println(today);
  }
}

# javac GetDate.java

2.
# java GetDate
Wed Mar 21 14:48:35 CDT 2018

3.
# export TZ=:/etc/localtime
# java GetDate
Wed Mar 21 13:50:04 GMT-06:00 2018

With date command, I get the correct time, even with the environment variable
# date
Wed Mar 21 14:50:06 CDT 2018

# ls -hla /etc/localtime
lrwxrwxrwx. 1 root root 37 Mar 21 13:28 /etc/localtime -> ../usr/share/zoneinfo/America/Chicago

4.
If the TZ environment variable is unset, it provides the correct output.
$ unset TZ
$ java GetDate
Wed Mar 21 14:52:38 CDT 2018

Actual results:
When TZ is set to absolute path of the TimeZone file, it fails to output the TimeZone but prints the GMT time.

Expected results:
Expected to return the TimeZone correctly even when TZ environment variable is set.

Additional info:

Comment 2 Deepu K S 2018-04-02 16:34:09 UTC
Another test that we ran locally;

# cat test.java
class test {
  /** Print a hello message */
  public static void main(String[] args) {
    System.out.println(java.util.TimeZone.getDefault());
  }
}

# javac test.java

# export TZ="/usr/share/zoneinfo/America/New_York"

# java test
sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] <<<<<<<<<---timezone id is set to GMT -05, it should be America/New_York.

# unset TZ

# java test
sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

If we export TZ to just the timezone as below, it works.
# export TZ=":America/New_York"

# java test
sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

The issue is seen when we export TZ to the absolute filepath or /etc/localtime.

Comment 3 Deepu K S 2018-04-06 08:42:10 UTC
Below is the Bug that was opened Oracle Java BugZilla.
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8200722

Comment 6 Deepak Bhole 2018-11-22 20:24:44 UTC
Andrew, can you please provide a status update?

Comment 7 Andrew John Hughes 2018-12-12 05:31:12 UTC
Sorry, I mixed this up with a similar bug (bug 1536048) and thought I'd answered it.

Looking over the code, I see the issue is that any value of TZ is assumed to be a zonename. So, America/New_York works, but if it's a file path, the path is sent back to the calling Java code unaltered, which can't match it to a zone and instead falls back to GMT.

I believe different behaviour should be used if the TZ value starts with a '/' (or ':/', but it already just drops the ':')

1. If the path is /etc/localtime, use the existing logic to find out which zone that points to.
2. If it's a timezone path, use getZoneName to strip the beginning and return the zone name

The logic for both these options is already there for the case where TZ is unset. It's just ignored where TZ is set, preferring to just use that value as is. A patch for this should be fairly trivial.

Comment 8 Andrew John Hughes 2019-06-28 17:15:08 UTC
Moving to RHEL 7.8.

Comment 43 RHEL Program Management 2023-09-12 21:50:04 UTC
Issue migration from Bugzilla to Jira is in process at this time. This will be the last message in Jira copied from the Bugzilla bug.

Comment 44 RHEL Program Management 2023-09-12 21:51:43 UTC
This BZ has been automatically migrated to the issues.redhat.com Red Hat Issue Tracker. All future work related to this report will be managed there.

Due to differences in account names between systems, some fields were not replicated.  Be sure to add yourself to Jira issue's "Watchers" field to continue receiving updates and add others to the "Need Info From" field to continue requesting information.

To find the migrated issue, look in the "Links" section for a direct link to the new issue location. The issue key will have an icon of 2 footprints next to it, and begin with "RHEL-" followed by an integer.  You can also find this issue by visiting https://issues.redhat.com/issues/?jql= and searching the "Bugzilla Bug" field for this BZ's number, e.g. a search like:

"Bugzilla Bug" = 1234567

In the event you have trouble locating or viewing this issue, you can file an issue by sending mail to rh-issues. You can also visit https://access.redhat.com/articles/7032570 for general account information.