|
| 1 | +package com.coditory.quark.i18n; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | + |
| 6 | +import java.time.ZoneId; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import static com.coditory.quark.i18n.Preconditions.expectNonNull; |
| 11 | + |
| 12 | +public class ZoneIds { |
| 13 | + // Australia/Darwin |
| 14 | + public static final ZoneId ACT = ZoneId.of("ACT", ZoneId.SHORT_IDS); |
| 15 | + // Australia/Sydney |
| 16 | + public static final ZoneId AET = ZoneId.of("AET", ZoneId.SHORT_IDS); |
| 17 | + // America/Argentina/Buenos_Aires |
| 18 | + public static final ZoneId AGT = ZoneId.of("AGT", ZoneId.SHORT_IDS); |
| 19 | + // Africa/Cairo |
| 20 | + public static final ZoneId ART = ZoneId.of("ART", ZoneId.SHORT_IDS); |
| 21 | + // America/Anchorage |
| 22 | + public static final ZoneId AST = ZoneId.of("AST", ZoneId.SHORT_IDS); |
| 23 | + // America/Sao_Paulo |
| 24 | + public static final ZoneId BET = ZoneId.of("BET", ZoneId.SHORT_IDS); |
| 25 | + // Asia/Dhaka |
| 26 | + public static final ZoneId BST = ZoneId.of("BST", ZoneId.SHORT_IDS); |
| 27 | + // Africa/Harare |
| 28 | + public static final ZoneId CAT = ZoneId.of("CAT", ZoneId.SHORT_IDS); |
| 29 | + // America/St_Johns |
| 30 | + public static final ZoneId CNT = ZoneId.of("CNT", ZoneId.SHORT_IDS); |
| 31 | + // America/Chicago |
| 32 | + public static final ZoneId CST = ZoneId.of("CST", ZoneId.SHORT_IDS); |
| 33 | + // Asia/Shanghai |
| 34 | + public static final ZoneId CTT = ZoneId.of("CTT", ZoneId.SHORT_IDS); |
| 35 | + // Africa/Addis_Ababa |
| 36 | + public static final ZoneId EAT = ZoneId.of("EAT", ZoneId.SHORT_IDS); |
| 37 | + // Europe/Paris |
| 38 | + public static final ZoneId ECT = ZoneId.of("ECT", ZoneId.SHORT_IDS); |
| 39 | + // America/Indiana/Indianapolis |
| 40 | + public static final ZoneId IET = ZoneId.of("IET", ZoneId.SHORT_IDS); |
| 41 | + // Asia/Kolkata |
| 42 | + public static final ZoneId IST = ZoneId.of("IST", ZoneId.SHORT_IDS); |
| 43 | + // Asia/Tokyo |
| 44 | + public static final ZoneId JST = ZoneId.of("JST", ZoneId.SHORT_IDS); |
| 45 | + // Pacific/Apia |
| 46 | + public static final ZoneId MIT = ZoneId.of("MIT", ZoneId.SHORT_IDS); |
| 47 | + // Asia/Yerevan |
| 48 | + public static final ZoneId NET = ZoneId.of("NET", ZoneId.SHORT_IDS); |
| 49 | + // Pacific/Auckland |
| 50 | + public static final ZoneId NST = ZoneId.of("NST", ZoneId.SHORT_IDS); |
| 51 | + // Asia/Karachi |
| 52 | + public static final ZoneId PLT = ZoneId.of("PLT", ZoneId.SHORT_IDS); |
| 53 | + // America/Phoenix |
| 54 | + public static final ZoneId PNT = ZoneId.of("PNT", ZoneId.SHORT_IDS); |
| 55 | + // America/Puerto_Rico |
| 56 | + public static final ZoneId PRT = ZoneId.of("PRT", ZoneId.SHORT_IDS); |
| 57 | + // America/Los_Angeles |
| 58 | + public static final ZoneId PST = ZoneId.of("PST", ZoneId.SHORT_IDS); |
| 59 | + // Pacific/Guadalcanal |
| 60 | + public static final ZoneId SST = ZoneId.of("SST", ZoneId.SHORT_IDS); |
| 61 | + // Asia/Ho_Chi_Minh |
| 62 | + public static final ZoneId VST = ZoneId.of("VST", ZoneId.SHORT_IDS); |
| 63 | + // -05:00 |
| 64 | + public static final ZoneId EST = ZoneId.of("EST", ZoneId.SHORT_IDS); |
| 65 | + // -07:00 |
| 66 | + public static final ZoneId MST = ZoneId.of("MST", ZoneId.SHORT_IDS); |
| 67 | + // -10:00 |
| 68 | + public static final ZoneId HST = ZoneId.of("HST", ZoneId.SHORT_IDS); |
| 69 | + // UTC |
| 70 | + public static final ZoneId UTC = ZoneId.of("UTC"); |
| 71 | + |
| 72 | + public static class Europe { |
| 73 | + public static final ZoneId Amsterdam = ZoneId.of("Europe/Amsterdam"); |
| 74 | + public static final ZoneId Berlin = ZoneId.of("Europe/Berlin"); |
| 75 | + public static final ZoneId Brussels = ZoneId.of("Europe/Brussels"); |
| 76 | + public static final ZoneId Copenhagen = ZoneId.of("Europe/Copenhagen"); |
| 77 | + public static final ZoneId Dublin = ZoneId.of("Europe/Dublin"); |
| 78 | + public static final ZoneId Warsaw = ZoneId.of("Europe/Warsaw"); |
| 79 | + public static final ZoneId Paris = ZoneId.of("Europe/Paris"); |
| 80 | + public static final ZoneId Zurich = ZoneId.of("Europe/Zurich"); |
| 81 | + } |
| 82 | + |
| 83 | + public static class America { |
| 84 | + public static final ZoneId Chicago = ZoneId.of("America/Chicago"); |
| 85 | + public static final ZoneId LosAngeles = ZoneId.of("America/Los_Angeles"); |
| 86 | + public static final ZoneId Phoenix = ZoneId.of("America/Phoenix"); |
| 87 | + public static final ZoneId PuertoRico = ZoneId.of("America/Puerto_Rico"); |
| 88 | + public static final ZoneId Vancouver = ZoneId.of("America/Vancouver"); |
| 89 | + } |
| 90 | + |
| 91 | + public static class Africa { |
| 92 | + public static final ZoneId Cairo = ZoneId.of("Africa/Cairo"); |
| 93 | + public static final ZoneId Casablanca = ZoneId.of("Africa/Casablanca"); |
| 94 | + public static final ZoneId Nairobi = ZoneId.of("Africa/Nairobi"); |
| 95 | + public static final ZoneId Tunis = ZoneId.of("Africa/Tunis"); |
| 96 | + } |
| 97 | + |
| 98 | + public static class Asia { |
| 99 | + public static final ZoneId Baghdad = ZoneId.of("Asia/Baghdad"); |
| 100 | + public static final ZoneId Baku = ZoneId.of("Asia/Baku"); |
| 101 | + public static final ZoneId Bangkok = ZoneId.of("Asia/Bangkok"); |
| 102 | + public static final ZoneId Beirut = ZoneId.of("Asia/Beirut"); |
| 103 | + public static final ZoneId Damascus = ZoneId.of("Asia/Damascus"); |
| 104 | + public static final ZoneId Dubai = ZoneId.of("Asia/Dubai"); |
| 105 | + public static final ZoneId Saigon = ZoneId.of("Asia/Saigon"); |
| 106 | + public static final ZoneId TelAviv = ZoneId.of("Asia/Tel_Aviv"); |
| 107 | + public static final ZoneId Tokyo = ZoneId.of("Asia/Tokyo"); |
| 108 | + } |
| 109 | + |
| 110 | + private static final Set<String> AVAILABLE_ZONES = ZoneId.getAvailableZoneIds(); |
| 111 | + |
| 112 | + private ZoneIds() { |
| 113 | + throw new UnsupportedOperationException("Do not instantiate utility class"); |
| 114 | + } |
| 115 | + |
| 116 | + @NotNull |
| 117 | + public static ZoneId parseZoneId(@NotNull String value) { |
| 118 | + expectNonNull(value, "value"); |
| 119 | + ZoneId zoneId; |
| 120 | + try { |
| 121 | + zoneId = ZoneId.of(value); |
| 122 | + } catch (Exception e) { |
| 123 | + throw new IllegalArgumentException("Could not parse ZoneId: '" + value + "'"); |
| 124 | + } |
| 125 | + boolean isAvailable = AVAILABLE_ZONES |
| 126 | + .contains(zoneId.getId()); |
| 127 | + if (!isAvailable) { |
| 128 | + throw new IllegalArgumentException("ZoneId not available: '" + value + "'"); |
| 129 | + } |
| 130 | + return zoneId; |
| 131 | + } |
| 132 | + |
| 133 | + @Nullable |
| 134 | + public static ZoneId parseZoneIdOrNull(@NotNull String value) { |
| 135 | + if (!AVAILABLE_ZONES.contains(value)) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + try { |
| 139 | + return ZoneId.of(value); |
| 140 | + } catch (Exception e) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @NotNull |
| 146 | + public static ZoneId parseZoneIdOrDefault(@NotNull String value, @NotNull ZoneId defaultValue) { |
| 147 | + ZoneId result = parseZoneIdOrNull(value); |
| 148 | + return result == null ? defaultValue : result; |
| 149 | + } |
| 150 | + |
| 151 | + @NotNull |
| 152 | + public static Optional<ZoneId> parseZoneIdOrEmpty(@NotNull String value) { |
| 153 | + return Optional.ofNullable(parseZoneIdOrNull(value)); |
| 154 | + } |
| 155 | +} |
0 commit comments