Skip to content

Commit

Permalink
backport: reverse conversion of IANA-zones to winzones
Browse files Browse the repository at this point in the history
see issue #756
  • Loading branch information
MenoData committed Mar 17, 2018
1 parent c151052 commit eafd36a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/src/test/java/net/time4j/SystemClockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void currentTime() {
public void realTimeInMicros() {
SystemClock clock = SystemClock.MONOTONIC;
assertThat(
Math.abs(clock.realTimeInMicros() - SystemClock.INSTANCE.realTimeInMicros()) < 1000,
Math.abs(clock.realTimeInMicros() - SystemClock.INSTANCE.realTimeInMicros()) < 5000,
is(true));
long utc = clock.realTimeInMicros() / 1000000;
long unix = clock.currentTimeInMicros() / 1000000;
Expand Down
83 changes: 82 additions & 1 deletion misc/src/main/java/net/time4j/tz/other/WindowsZone.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2015 Meno Hochschild, <http://www.menodata.de/>
* Copyright © 2013-2018 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (WindowsZone.java) is part of project Time4J.
*
Expand All @@ -21,7 +21,10 @@

package net.time4j.tz.other;

import net.time4j.tz.NameStyle;
import net.time4j.tz.TZID;
import net.time4j.tz.Timezone;
import net.time4j.tz.ZoneNameProvider;
import net.time4j.tz.spi.WinZoneProviderSPI;

import java.io.IOException;
Expand All @@ -45,6 +48,18 @@
* // output: WINDOWS~America/New_York
* </pre>
*
* <p>The <i>reverse</i> way: </p>
*
* <pre>
* String winzone = WindowsZone.toString(&quot;America/New_York&quot;, Locale.US);
* System.out.println(winzone);
* // output: Eastern Standard Time
* </pre>
*
* <p><strong>Note:</strong> A perfect roundtrip is often not possible because there are many more IANA-zone
* identifiers than windows zones. For best results, it is recommended to use the tzdata-module and not the
* standard timezone data of the underlying platform. </p>
*
* @author Meno Hochschild
* @since 2.2
*/
Expand All @@ -61,6 +76,18 @@
* // output: WINDOWS~America/New_York
* </pre>
*
* <p>Der <i>umgekehrte</i> Weg: </p>
*
* <pre>
* String winzone = WindowsZone.toString(&quot;America/New_York&quot;, Locale.US);
* System.out.println(winzone);
* // output: Eastern Standard Time
* </pre>
*
* <p><strong>Hinweis:</strong> Ein perfekter Kreisweg ist oft nicht m&ouml;glich, weil es viel mehr
* IANA-Zeitzonenkennungen als Windows-Zeitzonen gibt. Um beste Ergebnisse zu erzielen, wird empfohlen,
* das tzdata-Modul und nicht die Standardzeitzonendaten der jeweiligen Plattform zu nutzen. </p>
*
* @author Meno Hochschild
* @since 2.2
*/
Expand Down Expand Up @@ -115,6 +142,7 @@ public static Set<String> getAvailableNames() {
* @throws IllegalArgumentException if given name is not supported
* @since 2.2
* @see #getAvailableNames()
* @see #toString()
*/
/*[deutsch]
* <p>Erzeugt einen Namensbezug zu einer Windows-Zeitzone. </p>
Expand All @@ -124,6 +152,7 @@ public static Set<String> getAvailableNames() {
* @throws IllegalArgumentException if given name is not supported
* @since 2.2
* @see #getAvailableNames()
* @see #toString()
*/
public static WindowsZone of(String name) {

Expand Down Expand Up @@ -170,6 +199,58 @@ public String toString() {

}

/**
* <p>Deduces the windows name of given timezone reference and region. </p>
*
* @param tzid timezone identifier (usually a IANA/Olson-ID like &quot;America/New_York&quot;)
* @param locale regional setting referring to a country
* @return name of windows zone, maybe empty if resolving fails
* @since 3.41/4.36
*/
/*[deutsch]
* <p>Leitet den Windows-Namen aus der angegebenen Zeitzonenreferenz in der jeweiligen Region ab. </p>
*
* @param tzid timezone identifier (usually a IANA/Olson-ID like &quot;America/New_York&quot;)
* @param locale regional setting referring to a country
* @return name of windows zone, maybe empty if resolving fails
* @since 3.41/4.36
*/
public static String toString(
TZID tzid,
Locale locale
) {

return toString(tzid.canonical(), locale);

}

/**
* <p>Deduces the windows name of given timezone reference and region. </p>
*
* @param tzid timezone identifier (usually a IANA/Olson-ID like &quot;America/New_York&quot;)
* @param locale regional setting referring to a country
* @return name of windows zone, maybe empty if resolving fails
* @since 3.41/4.36
*/
/*[deutsch]
* <p>Leitet den Windows-Namen aus der angegebenen Zeitzonenreferenz in der jeweiligen Region ab. </p>
*
* @param tzid timezone identifier (usually a IANA/Olson-ID like &quot;America/New_York&quot;)
* @param locale regional setting referring to a country
* @return name of windows zone, maybe empty if resolving fails
* @since 3.41/4.36
*/
public static String toString(
String tzid,
Locale locale
) {

String zoneID = Timezone.normalize(tzid).canonical();
ZoneNameProvider znp = new WinZoneProviderSPI();
return znp.getDisplayName(zoneID, NameStyle.LONG_STANDARD_TIME, locale);

}

/**
* <p>The natural order is based on the lexicographical order of the
* underlying names of windows zones. </p>
Expand Down
4 changes: 2 additions & 2 deletions misc/src/main/java/net/time4j/tz/spi/WinZoneProviderSPI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2016 Meno Hochschild, <http://www.menodata.de/>
* Copyright © 2013-2018 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (WinZoneProviderSPI.java) is part of project Time4J.
*
Expand Down Expand Up @@ -153,7 +153,7 @@ public Set<String> getPreferredIDs(
@Override
public String getDisplayName(
String tzid,
NameStyle style,
NameStyle style, // unused
Locale locale
) {

Expand Down
Binary file modified misc/src/main/resources/data/winzone.ser
Binary file not shown.
11 changes: 11 additions & 0 deletions misc/src/test/java/net/time4j/tz/other/WindowsZoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ public void normalize() {
is("America/New_York"));
}

@Test
public void toStringStatic() {
assertThat(
WindowsZone.toString(Timezone.of("America/New_York").getID(), Locale.US),
is("Eastern Standard Time"));
assertThat(
WindowsZone.toString("Asia/Kolkata", new Locale("en", "IN")),
is("India Standard Time"));
// CLDR uses "Asia/Calcutta", but Time4J adjusts it during compiling of winzone-data
}

private static Object roundtrip(Object obj)
throws IOException, ClassNotFoundException {

Expand Down

0 comments on commit eafd36a

Please sign in to comment.