def test_datetime_with_time_zone(self): oldDefaultTimeZone = TimeZone.getDefault() try: TimeZone.setDefault(TimeZone.getTimeZone("GMT+4")) c = tWithDateTimeZCursor(self.context) zoneId = ZoneId.of("GMT+2") #This is the datetime we will insert localDateTime = LocalDateTime.of(2017, Month.DECEMBER, 31, 22, 0, 0) zonedDateTime = ZonedDateTime.of(localDateTime, zoneId) #This is the datetime we expect the database to receive utcDateTime = LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0) c.eventDate = zonedDateTime c.insert() c.clear() c.first() zoneIdAfterSelect = ZoneId.of("GMT+4") self.assertEquals(utcDateTime, c.eventDate.toLocalDateTime()) self.assertEquals(zoneIdAfterSelect, c.eventDate.getZone()) finally: TimeZone.setDefault(oldDefaultTimeZone)
def to_java_zoneddatetime(value): """ Converts any of the supported date types to ``java.time.ZonedDateTime``. If ``value`` does not have timezone information, the system default will be used. Examples: .. code-block:: java_time = to_java_zoneddatetime(items["date_item"]) Args: value: the value to convert Returns: java.time.ZonedDateTime: the converted value Raises: TypeError: if the type of ``value`` is not supported by this module """ if isinstance(value, ZonedDateTime): return value timezone_id = ZoneId.systemDefault() # java.time.LocalDateTime if isinstance(value, LocalDateTime): return value.atZone(timezone_id) # python datetime if isinstance(value, datetime.datetime): if value.tzinfo is not None: timezone_id = ZoneId.ofOffset( "GMT", ZoneOffset.ofTotalSeconds( int(value.utcoffset().total_seconds()))) return ZonedDateTime.of(value.year, value.month, value.day, value.hour, value.minute, value.second, value.microsecond * 1000, timezone_id) # java.util.Calendar if isinstance(value, Calendar): return ZonedDateTime.ofInstant(value.toInstant(), ZoneId.of(value.getTimeZone().getID())) # java.util.Date if isinstance(value, Date): return ZonedDateTime.ofInstant( value.toInstant(), ZoneId.ofOffset( "GMT", ZoneOffset.ofHours(0 - value.getTimezoneOffset() / 60))) # Joda DateTime if JodaDateTime and isinstance(value, JodaDateTime): return value.toGregorianCalendar().toZonedDateTime() # openHAB DateTimeType if DateTimeType and isinstance(value, DateTimeType): return to_java_zoneddatetime(value.getZonedDateTime()) # Eclipse Smarthome DateTimeType if EclipseDateTimeType and isinstance(value, EclipseDateTimeType): return to_java_zoneddatetime(value.calendar) # Legacy (OH1.x compat) DateTimeType if LegacyDateTimeType and isinstance(value, LegacyDateTimeType): return to_java_zoneddatetime(value.calendar) raise TypeError("Unknown type: {}".format(str(type(value))))
def to_java_zoneddatetime(value): """Converts any known DateTime type to a ``java.time.ZonedDateTime`` type. Args: value: any known DateTime value. Returns: | A ``java.time.ZonedDateTime`` representing ``value``. | If ``value`` does not have timezone information, the system default will be used. Raises: TypeError: type of ``value`` is not recognized by this package. """ timezone_id = ZoneId.systemDefault() if isinstance(value, ZonedDateTime): return value # java.time.LocalDateTime if isinstance(value, LocalDateTime): return value.atZone(timezone_id) # python datetime if isinstance(value, datetime.datetime): if value.tzinfo is not None: timezone_id = ZoneId.ofOffset( "GMT", ZoneOffset.ofTotalSeconds(value.utcoffset().total_seconds())) return ZonedDateTime.of(value.year, value.month, value.day, value.hour, value.minute, value.second, value.microsecond * 1000, timezone_id) # java.util.Calendar if isinstance(value, Calendar): return ZonedDateTime.ofInstant(value.toInstant(), ZoneId.of(value.getTimeZone().getID())) # java.util.Date if isinstance(value, Date): return ZonedDateTime.ofInstant( value.toInstant(), ZoneId.ofOffset( "GMT", ZoneOffset.ofTotalSeconds(value.getTimezoneOffset() * 60))) # Joda DateTime if isinstance(value, DateTime): return value.toGregorianCalendar().toZonedDateTime() # OH DateTimeType or ESH DateTimeType if isinstance(value, (LegacyDateTimeType, DateTimeType)): return to_java_zoneddatetime(value.calendar) raise TypeError("Unknown type: " + str(type(value)))
def to_java_zoneddatetime(value): '''Returns java.time.ZonedDateTime (with system timezone, if none specified). Accepts any date type used by this module.''' timezone_id = ZoneId.systemDefault() if isinstance(value, ZonedDateTime): return value # java.time.LocalDateTime if isinstance(value, LocalDateTime): return value.atZone(timezone_id) # python datetime if isinstance(value, datetime.datetime): if value.tzinfo is not None: timezone_id = ZoneId.ofOffset( "GMT", ZoneOffset.ofTotalSeconds(value.utcoffset().total_seconds())) return ZonedDateTime.of(value.year, value.month, value.day, value.hour, value.minute, value.second, value.microsecond * 1000, timezone_id) # java.util.Calendar if isinstance(value, Calendar): return ZonedDateTime.ofInstant(value.toInstant(), ZoneId.of(value.getTimeZone().getID())) # java.util.Date if isinstance(value, Date): return ZonedDateTime.ofInstant( value.toInstant(), ZoneId.ofOffset( "GMT", ZoneOffset.ofTotalSeconds(value.getTimezoneOffset() * 60))) # Joda DateTime if isinstance(value, DateTime): return value.toGregorianCalendar().toZonedDateTime() # OH DateTimeType or ESH DateTimeType if isinstance(value, (LegacyDateTimeType, DateTimeType)): return to_java_zoneddatetime(value.calendar) raise Exception("Invalid conversion: " + str(type(value)))
def onConfigure(self): self.withLabel("Type date-time arg action") self.withArgs([ DateTimeType("arg").withLabel("Date-time non nullable").withDefaultValue(LocalDateTime.of(2020, 1, 1, 12, 0)), DateTimeType("argNullable").withLabel("Date-time nullable").withNullable(), DateTimeType("argDefault").withLabel("Date-time with default").withDefaultValue(LocalDateTime.of(2020, 1, 1, 12, 0)), DateTimeType("argReadOnlyNullable").withLabel("Date-time read only nullable").withReadOnly().withNullable() .withDefaultValue(LocalDateTime.of(2020, 1, 1, 12, 0)), DateTimeType("argReadOnlyNonNullable").withLabel("Date-time read only non nullable").withReadOnly() .withDefaultValue(LocalDateTime.of(2020, 1, 1, 12, 0)), DateTimeType("argDate").withDate().withLabel("Date non nullable").withDefaultValue(LocalDate.of(2020, 1, 1)), DateTimeType("argDateNullable").withDate().withLabel("Date nullable").withNullable(), DateTimeType("argDateDefault").withDate().withLabel("Date with default").withDefaultValue(LocalDate.of(2020, 1, 1)), DateTimeType("argDateReadOnlyNullable").withDate().withLabel("Date read only nullable").withReadOnly().withNullable() .withDefaultValue(LocalDate.of(2020, 1, 1)), DateTimeType("argDateReadOnlyNonNullable").withDate().withLabel("Date read only non nullable").withReadOnly() .withDefaultValue(LocalDate.of(2020, 1, 1)), DateTimeType("argTime").withTime().withFormat("HH:mm").withLabel("Time non nullable").withDefaultValue(LocalTime.of(12, 0)), DateTimeType("argTimeNullable").withTime().withFormat("HH:mm").withLabel("Time nullable").withNullable(), DateTimeType("argTimeDefault").withTime().withFormat("HH:mm").withLabel("Time with default").withDefaultValue(LocalTime.of(12, 0)), DateTimeType("argTimeReadOnlyNullable").withTime().withFormat("HH:mm").withLabel("Time read only nullable").withReadOnly().withNullable() .withDefaultValue(LocalTime.of(12, 0)), DateTimeType("argTimeReadOnlyNonNullable").withTime().withFormat("HH:mm").withLabel("Time read only non nullable").withReadOnly() .withDefaultValue(LocalTime.of(12, 0)), DateTimeType("argDateTimeZoneReadOnlyNullable").withDateTimeZone().withLabel("Date-time-zone read only nullable").withReadOnly().withNullable() .withDefaultValue(ZonedDateTime.of(LocalDateTime.of(2020, 1, 1, 12, 0), ZoneId.of("America/Detroit"))), DateTimeType("argDateTimeZoneReadOnlyNonNullable").withDateTimeZone().withLabel("Date-time-zone read only non nullable").withReadOnly() .withDefaultValue(ZonedDateTime.of(LocalDateTime.of(2020, 1, 1, 12, 0), ZoneId.of("America/Detroit"))), DateTimeType("argInstantReadOnlyNullable").withInstant().withLabel("Instant read only nullable").withReadOnly().withNullable() .withDefaultValue(Instant.now()), DateTimeType("argInstantReadOnlyNonNullable").withInstant().withLabel("Instant read only non nullable").withReadOnly() .withDefaultValue(Instant.now()), # DATE_TIME_ZONE and INSTANT editing is not supported yet. ]).withResult(StringType())
def onCall(self, type): if type == "nonNullable": return DynamicValue(LocalDateTime.of(2020, 1, 1, 12, 0), DateTimeType().withLabel("Date-time non nullable")) elif type == "nullable": return DynamicValue(None, DateTimeType().withLabel("Date-time nullable").withNullable()) elif type == "readOnlyNullable": return DynamicValue(None, DateTimeType().withLabel("Date-time read only nullable").withReadOnly().withNullable()) elif type == "readOnlyNonNullable": return DynamicValue(LocalDateTime.of(2020, 1, 1, 12, 0), DateTimeType().withLabel("Date-time read only non nullable").withReadOnly()) if type == "dateNonNullable": return DynamicValue(LocalDate.of(2020, 1, 1), DateTimeType().withDate().withLabel("Date non nullable")) elif type == "dateNullable": return DynamicValue(None, DateTimeType().withDate().withLabel("Date nullable").withNullable()) elif type == "dateReadOnlyNullable": return DynamicValue(None, DateTimeType().withDate().withLabel("Date read only nullable").withReadOnly().withNullable()) elif type == "dateReadOnlyNonNullable": return DynamicValue(LocalDate.of(2020, 1, 1), DateTimeType().withDate().withLabel("Date-time read only non nullable").withReadOnly()) if type == "timeNonNullable": return DynamicValue(LocalTime.of(12, 0), DateTimeType().withTime().withFormat("HH:mm").withLabel("Time non nullable")) elif type == "timeNullable": return DynamicValue(None, DateTimeType().withTime().withFormat("HH:mm").withLabel("Time nullable").withNullable()) elif type == "timeReadOnlyNullable": return DynamicValue(None, DateTimeType().withTime().withFormat("HH:mm").withLabel("Time read only nullable").withReadOnly().withNullable()) elif type == "timeReadOnlyNonNullable": return DynamicValue(LocalTime.of(12, 0), DateTimeType().withTime().withFormat("HH:mm").withLabel("Time read only non nullable").withReadOnly()) if type == "dateTimeZoneReadOnlyNullable": return DynamicValue(None, DateTimeType().withDateTimeZone().withLabel("Date-time-zone read only nullable").withReadOnly().withNullable()) elif type == "dateTimeZoneReadOnlyNonNullable": return DynamicValue(ZonedDateTime.of(LocalDateTime.of(2020, 1, 1, 12, 0), ZoneId.of("America/Detroit")), DateTimeType().withDateTimeZone().withLabel("Date-time-zone read only non nullable").withReadOnly()) if type == "instantReadOnlyNullable": return DynamicValue(None, DateTimeType().withInstant().withLabel("Instant read only nullable").withReadOnly().withNullable()) elif type == "instantReadOnlyNonNullable": return DynamicValue(Instant.now(), DateTimeType().withInstant().withLabel("Instant read only non nullable").withReadOnly()) else: return None
from mil.army.usace.hec.vortex.math import Normalizer from mil.army.usace.hec.vortex.io import DataReader from java.time import ZonedDateTime from java.time import LocalDateTime from java.time import ZoneId from java.time import Duration source = "C:/Temp/qpe.dss" normals = "C:/Temp/prism.dss" sourceGrids = DataReader.getVariables(source) normalGrids = DataReader.getVariables(normals) start = ZonedDateTime.of(LocalDateTime.of(2017, 1, 1, 0, 0), ZoneId.of("UTC")) end = ZonedDateTime.of(LocalDateTime.of(2017, 1, 3, 0, 0), ZoneId.of("UTC")) interval = Duration.ofDays(1) destination = 'C:/Temp/normalized.dss' options = {'partF': 'my normalized grids'} normalizer = Normalizer.builder() \ .startTime(start) \ .endTime(end) \ .interval(interval) \ .pathToSource(source) \ .sourceVariables(sourceGrids) \ .pathToNormals(normals) \ .normalsVariables(normalGrids) \ .destination(destination) \
def __init__(self, zid=ZoneId.of("UTC")): self.zid = zid self.fb = self.format_builder()