ttomcat-1778514358873.zip-extract/_dependencies/maven/com.h2database_h2-2.2.220/org/h2/util/TimeZoneProvider.java

Path
ttomcat-1778514358873.zip-extract/_dependencies/maven/com.h2database_h2-2.2.220/org/h2/util/TimeZoneProvider.java
Status
scanned
Type
file
Name
TimeZoneProvider.java
Extension
.java
Programming language
Java
Mime type
text/x-java
File type
Java source, ASCII text
Tag

      
    
Rootfs path

      
    
Size
14830 (14.5 KB)
MD5
d1b3566a96614ef14ece6650c20456f8
SHA1
77dfb7b9acd5c3c53fe3f9d0712f053ec45e7694
SHA256
de24f47ec2c1babf6d9a887725d5e595c9717587950f2a54ca8f27f55c8ccbc9
SHA512

      
    
SHA1_git
536a8d75d0fd6e712ae094b1933670c8ffdb29c3
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TimeZoneProvider.java | 14.5 KB |

/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.util; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.zone.ZoneRules; import java.util.Locale; /** * Provides access to time zone API. */ public abstract class TimeZoneProvider { /** * The UTC time zone provider. */ public static final TimeZoneProvider UTC = new Simple((short) 0); /** * A small cache for timezone providers. */ public static TimeZoneProvider[] CACHE; /** * The number of cache elements (needs to be a power of 2). */ private static final int CACHE_SIZE = 32; /** * Returns the time zone provider with the specified offset. * * @param offset * UTC offset in seconds * @return the time zone provider with the specified offset */ public static TimeZoneProvider ofOffset(int offset) { if (offset == 0) { return UTC; } if (offset < (-18 * 60 * 60) || offset > (18 * 60 * 60)) { throw new IllegalArgumentException("Time zone offset " + offset + " seconds is out of range"); } return new Simple(offset); } /** * Returns the time zone provider with the specified name. * * @param id * the ID of the time zone * @return the time zone provider with the specified name * @throws RuntimeException * if time zone with specified ID isn't known */ public static TimeZoneProvider ofId(String id) throws RuntimeException { int length = id.length(); if (length == 1 && id.charAt(0) == 'Z') { return UTC; } int index = 0; if (id.startsWith("GMT") || id.startsWith("UTC")) { if (length == 3) { return UTC; } index = 3; } if (length > index) { boolean negative = false; char c = id.charAt(index); if (length > index + 1) { if (c == '+') { c = id.charAt(++index); } else if (c == '-') { negative = true; c = id.charAt(++index); } } if (index != 3 && c >= '0' && c <= '9') { int hour = c - '0'; if (++index < length) { c = id.charAt(index); if (c >= '0' && c <= '9') { hour = hour * 10 + c - '0'; index++; } } if (index == length) { int offset = hour * 3_600; return ofOffset(negative ? -offset : offset); } if (id.charAt(index) == ':') { if (++index < length) { c = id.charAt(index); if (c >= '0' && c <= '9') { int minute = c - '0'; if (++index < length) { c = id.charAt(index); if (c >= '0' && c <= '9') { minute = minute * 10 + c - '0'; index++; } } if (index == length) { int offset = (hour * 60 + minute) * 60; return ofOffset(negative ? -offset : offset); } if (id.charAt(index) == ':') { if (++index < length) { c = id.charAt(index); if (c >= '0' && c <= '9') { int second = c - '0'; if (++index < length) { c = id.charAt(index); if (c >= '0' && c <= '9') { second = second * 10 + c - '0'; index++; } } if (index == length) { int offset = (hour * 60 + minute) * 60 + second; return ofOffset(negative ? -offset : offset); } } } } } } } } if (index > 0) { throw new IllegalArgumentException(id); } } int hash = id.hashCode() & (CACHE_SIZE - 1); TimeZoneProvider[] cache = CACHE; if (cache != null) { TimeZoneProvider provider = cache[hash]; if (provider != null && provider.getId().equals(id)) { return provider; } } TimeZoneProvider provider = new WithTimeZone(ZoneId.of(id, ZoneId.SHORT_IDS)); if (cache == null) { CACHE = cache = new TimeZoneProvider[CACHE_SIZE]; } cache[hash] = provider; return provider; } /** * Returns the time zone provider for the system default time zone. * * @return the time zone provider for the system default time zone */ public static TimeZoneProvider getDefault() { ZoneId zoneId = ZoneId.systemDefault(); ZoneOffset offset; if (zoneId instanceof ZoneOffset) { offset = (ZoneOffset) zoneId; } else { ZoneRules rules = zoneId.getRules(); if (!rules.isFixedOffset()) { return new WithTimeZone(zoneId); } offset = rules.getOffset(Instant.EPOCH); } return ofOffset(offset.getTotalSeconds()); } /** * Calculates the time zone offset in seconds for the specified EPOCH * seconds. * * @param epochSeconds * seconds since EPOCH * @return time zone offset in minutes */ public abstract int getTimeZoneOffsetUTC(long epochSeconds); /** * Calculates the time zone offset in seconds for the specified date value * and nanoseconds since midnight in local time. * * @param dateValue * date value * @param timeNanos * nanoseconds since midnight * @return time zone offset in minutes */ public abstract int getTimeZoneOffsetLocal(long dateValue, long timeNanos); /** * Calculates the epoch seconds from local date and time. * * @param dateValue * date value * @param timeNanos * nanoseconds since midnight * @return the epoch seconds value */ public abstract long getEpochSecondsFromLocal(long dateValue, long timeNanos); /** * Returns the ID of the time zone. * * @return the ID of the time zone */ public abstract String getId(); /** * Get the standard time name or daylight saving time name of the time zone. * * @param epochSeconds * seconds since EPOCH * @return the standard time name or daylight saving time name of the time * zone */ public abstract String getShortId(long epochSeconds); /** * Returns whether this is a simple time zone provider with a fixed offset * from UTC. * * @return whether this is a simple time zone provider with a fixed offset * from UTC */ public boolean hasFixedOffset() { return false; } /** * Time zone provider with offset. */ private static final class Simple extends TimeZoneProvider { private final int offset; private volatile String id; Simple(int offset) { this.offset = offset; } @Override public int hashCode() { return offset + 129607; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || obj.getClass() != Simple.class) { return false; } return offset == ((Simple) obj).offset; } @Override public int getTimeZoneOffsetUTC(long epochSeconds) { return offset; } @Override public int getTimeZoneOffsetLocal(long dateValue, long timeNanos) { return offset; } @Override public long getEpochSecondsFromLocal(long dateValue, long timeNanos) { return DateTimeUtils.getEpochSeconds(dateValue, timeNanos, offset); } @Override public String getId() { String id = this.id; if (id == null) { this.id = id = DateTimeUtils.timeZoneNameFromOffsetSeconds(offset); } return id; } @Override public String getShortId(long epochSeconds) { return getId(); } @Override public boolean hasFixedOffset() { return true; } @Override public String toString() { return "TimeZoneProvider " + getId(); } } /** * Time zone provider with time zone. */ static final class WithTimeZone extends TimeZoneProvider { /** * Number of seconds in 400 years. */ static final long SECONDS_PER_PERIOD = 146_097L * 60 * 60 * 24; /** * Number of seconds per year. */ static final long SECONDS_PER_YEAR = SECONDS_PER_PERIOD / 400; private static volatile DateTimeFormatter TIME_ZONE_FORMATTER; private final ZoneId zoneId; WithTimeZone(ZoneId timeZone) { this.zoneId = timeZone; } @Override public int hashCode() { return zoneId.hashCode() + 951689; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || obj.getClass() != WithTimeZone.class) { return false; } return zoneId.equals(((WithTimeZone) obj).zoneId); } @Override public int getTimeZoneOffsetUTC(long epochSeconds) { /* * Construct an Instant with EPOCH seconds within the range * -31,557,014,135,532,000..31,556,889,832,715,999 * (-999999999-01-01T00:00-18:00.. * +999999999-12-31T23:59:59.999999999+18:00). Too large and too * small EPOCH seconds are replaced with EPOCH seconds within the * range using the 400 years period of the Gregorian calendar. * * H2 has slightly wider range of EPOCH seconds than Instant, and * ZoneRules.getOffset(Instant) does not support all Instant values * in all time zones. */ if (epochSeconds > 31_556_889_832_715_999L) { epochSeconds -= SECONDS_PER_PERIOD; } else if (epochSeconds < -31_557_014_135_532_000L) { epochSeconds += SECONDS_PER_PERIOD; } return zoneId.getRules().getOffset(Instant.ofEpochSecond(epochSeconds)).getTotalSeconds(); } @Override public int getTimeZoneOffsetLocal(long dateValue, long timeNanos) { int second = (int) (timeNanos / DateTimeUtils.NANOS_PER_SECOND); int minute = second / 60; second -= minute * 60; int hour = minute / 60; minute -= hour * 60; return ZonedDateTime.of(LocalDateTime.of(yearForCalendar(DateTimeUtils.yearFromDateValue(dateValue)), DateTimeUtils.monthFromDateValue(dateValue), DateTimeUtils.dayFromDateValue(dateValue), hour, minute, second), zoneId).getOffset().getTotalSeconds(); } @Override public long getEpochSecondsFromLocal(long dateValue, long timeNanos) { int second = (int) (timeNanos / DateTimeUtils.NANOS_PER_SECOND); int minute = second / 60; second -= minute * 60; int hour = minute / 60; minute -= hour * 60; int year = DateTimeUtils.yearFromDateValue(dateValue); int yearForCalendar = yearForCalendar(year); long epoch = ZonedDateTime .of(LocalDateTime.of(yearForCalendar, DateTimeUtils.monthFromDateValue(dateValue), DateTimeUtils.dayFromDateValue(dateValue), hour, minute, second), zoneId) .toOffsetDateTime().toEpochSecond(); return epoch + (year - yearForCalendar) * SECONDS_PER_YEAR; } @Override public String getId() { return zoneId.getId(); } @Override public String getShortId(long epochSeconds) { DateTimeFormatter timeZoneFormatter = TIME_ZONE_FORMATTER; if (timeZoneFormatter == null) { TIME_ZONE_FORMATTER = timeZoneFormatter = DateTimeFormatter.ofPattern("z", Locale.ENGLISH); } return ZonedDateTime.ofInstant(Instant.ofEpochSecond(epochSeconds), zoneId).format(timeZoneFormatter); } /** * Returns a year within the range -999,999,999..999,999,999 for the * given year. Too large and too small years are replaced with years * within the range using the 400 years period of the Gregorian * calendar. * * Because we need them only to calculate a time zone offset, it's safe * to normalize them to such range. * * @param year * the year * @return the specified year or the replacement year within the range */ private static int yearForCalendar(int year) { if (year > 999_999_999) { year -= 400; } else if (year < -999_999_999) { year += 400; } return year; } @Override public String toString() { return "TimeZoneProvider " + zoneId.getId(); } } }
Detected license expression

      
    
Detected license expression (SPDX)

      
    
Percentage of license text
0.83
Copyrights
- end_line: 2
  copyright: Copyright 2004-2023 H2 Group. Multiple-Licensed
  start_line: 2
Holders
- holder: H2 Group. Multiple-Licensed
  end_line: 2
  start_line: 2
Authors

      
    
License expression License clue details
(mpl-2.0 OR epl-1.0) AND proprietary-license {'score': 20.37, 'matcher': '3-seq', 'end_line': 3, 'rule_url': 'https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/mpl-2.0_or_epl-1.0_and_proprietary-license_2.RULE', 'from_file': None, 'start_line': 2, 'matched_text': ' * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,\n * and the EPL 1.0 (https://h2database.com/html/license.html).', 'match_coverage': 20.37, 'matched_length': 11, 'rule_relevance': 100, 'rule_identifier': 'mpl-2.0_or_epl-1.0_and_proprietary-license_2.RULE', 'license_expression': '(mpl-2.0 OR epl-1.0) AND proprietary-license', 'license_expression_spdx': '(MPL-2.0 OR EPL-1.0) AND LicenseRef-scancode-proprietary-license'}
URL Start line End line
https://h2database.com/html/license.html 3 3
Package URL License Primary language
pkg:osgi/com.h2database.source@2.2.220