def test_iso_format_with_time_zone_case_1(): # python -m pytest tests/unit/time/test_time.py -s -v -k test_iso_format_with_time_zone_case_1 expected = Time(7, 54, 2.129790999, tzinfo=timezone_utc) assert expected.iso_format() == "07:54:02.129790999+00:00" assert expected.tzinfo == FixedOffset(0) actual = Time.from_iso_format("07:54:02.129790999+00:00") assert expected == actual
def test_to_native(self): t = Time(12, 34, 56.789123456) native = t.to_native() self.assertEqual(t.hour, native.hour) self.assertEqual(t.minute, native.minute) self.assertEqual( 56.789123, nano_add(native.second, nano_div(native.microsecond, 1000000)))
def test_to_native_case_2(): # python -m pytest tests/unit/time/test_time.py -s -v -k test_to_native_case_2 t = Time(12, 34, 56.789123456, tzinfo=timezone_utc) native = t.to_native() assert native.hour == t.hour assert native.minute == t.minute assert nano_add(native.second, nano_div(native.microsecond, 1000000)) == 56.789123 assert native.tzinfo == FixedOffset(0) assert native.isoformat() == "12:34:56.789123+00:00"
def test_to_native_case_1(): # python -m pytest tests/unit/time/test_time.py -s -v -k test_to_native_case_1 t = Time(12, 34, 56.789123456) native = t.to_native() assert native.hour == t.hour assert native.minute == t.minute assert nano_add(native.second, nano_div(native.microsecond, 1000000)) == 56.789123 assert native.tzinfo is None assert native.isoformat() == "12:34:56.789123"
def test_nanosecond_precision(self): t = Time(12, 34, 56.789123456) self.assertEqual(t.hour_minute_second, (12, 34, 56.789123456)) self.assertEqual(t.ticks, 45296.789123456) self.assertEqual(t.hour, 12) self.assertEqual(t.minute, 34) self.assertEqual(t.second, 56.789123456)
def test_midnight(self): t = Time(0, 0, 0) self.assertEqual(t.hour_minute_second, (0, 0, 0)) self.assertEqual(t.ticks, 0) self.assertEqual(t.hour, 0) self.assertEqual(t.minute, 0) self.assertEqual(t.second, 0)
def test_simple_time(self): t = Time(12, 34, 56.789) self.assertEqual(t.hour_minute_second, (12, 34, 56.789)) self.assertEqual(t.ticks, 45296.789) self.assertEqual(t.hour, 12) self.assertEqual(t.minute, 34) self.assertEqual(t.second, 56.789)
def test_from_native(self): native = time(12, 34, 56, 789123) t = Time.from_native(native) self.assertEqual(t.hour, native.hour) self.assertEqual(t.minute, native.minute) self.assertEqual( t.second, nano_add(native.second, nano_div(native.microsecond, 1000000)))
def test_whole_second_time_input(cypher_eval): result = cypher_eval("CYPHER runtime=interpreted WITH $x AS x " "RETURN [x.hour, x.minute, x.second]", x=Time(12, 34, 56)) hour, minute, second = result assert hour == 12 assert minute == 34 assert second == 56
def test_from_native_case_2(): # python -m pytest tests/unit/time/test_time.py -s -v -k test_from_native_case_2 native = time(12, 34, 56, 789123, FixedOffset(0)) t = Time.from_native(native) assert t.hour == native.hour assert t.minute == native.minute assert t.second == nano_add(native.second, nano_div(native.microsecond, 1000000)) assert t.tzinfo == FixedOffset(0)
def test_nanosecond_resolution_time_input(cypher_eval): result = cypher_eval("CYPHER runtime=interpreted WITH $x AS x " "RETURN [x.hour, x.minute, x.second, x.nanosecond]", x=Time(12, 34, 56.789012345)) hour, minute, second, nanosecond = result assert hour == 12 assert minute == 34 assert second == 56 assert nanosecond == 789012345
def test_whole_second_time(self): with self.driver.session() as session: result = session.run("CYPHER runtime=interpreted WITH $x AS x " "RETURN x.hour, x.minute, x.second", x=Time(12, 34, 56)) hour, minute, second = result.single() self.assertEqual(hour, 12) self.assertEqual(minute, 34) self.assertEqual(second, 56)
def test_nanosecond_resolution_time(self): with self.driver.session() as session: result = session.run("CYPHER runtime=interpreted WITH $x AS x " "RETURN x.hour, x.minute, x.second, x.nanosecond", x=Time(12, 34, 56.789012345)) hour, minute, second, nanosecond = result.single() self.assertEqual(hour, 12) self.assertEqual(minute, 34) self.assertEqual(second, 56) self.assertEqual(nanosecond, 789012345)
def test_time_with_numeric_time_offset(self): with self.driver.session() as session: result = session.run("CYPHER runtime=interpreted WITH $x AS x " "RETURN x.hour, x.minute, x.second, x.nanosecond, x.offset", x=Time(12, 34, 56.789012345, tzinfo=FixedOffset(90))) hour, minute, second, nanosecond, offset = result.single() self.assertEqual(hour, 12) self.assertEqual(minute, 34) self.assertEqual(second, 56) self.assertEqual(nanosecond, 789012345) self.assertEqual(offset, "+01:30")
def test_time_with_numeric_time_offset_input(cypher_eval): result = cypher_eval("CYPHER runtime=interpreted WITH $x AS x " "RETURN [x.hour, x.minute, x.second, " " x.nanosecond, x.offset]", x=Time(12, 34, 56.789012345, tzinfo=FixedOffset(90))) hour, minute, second, nanosecond, offset = result assert hour == 12 assert minute == 34 assert second == 56 assert nanosecond == 789012345 assert offset == "+01:30"
def hydrate_time(nanoseconds, tz=None): """ Hydrator for `Time` and `LocalTime` values. :param nanoseconds: :param tz: :return: Time """ seconds, nanoseconds = map(int, divmod(nanoseconds, 1000000000)) minutes, seconds = map(int, divmod(seconds, 60)) hours, minutes = map(int, divmod(minutes, 60)) seconds = (1000000000 * seconds + nanoseconds) / 1000000000 t = Time(hours, minutes, seconds) if tz is None: return t tz_offset_minutes, tz_offset_seconds = divmod(tz, 60) zone = FixedOffset(tz_offset_minutes) return zone.localize(t)
def hydrate_datetime(seconds, nanoseconds, tz=None): """ Hydrator for `DateTime` and `LocalDateTime` values. :param seconds: :param nanoseconds: :param tz: :return: datetime """ minutes, seconds = map(int, divmod(seconds, 60)) hours, minutes = map(int, divmod(minutes, 60)) days, hours = map(int, divmod(hours, 24)) seconds = (1000000000 * seconds + nanoseconds) / 1000000000 t = DateTime.combine(Date.from_ordinal(UNIX_EPOCH_DATE_ORDINAL + days), Time(hours, minutes, seconds)) if tz is None: return t if isinstance(tz, int): tz_offset_minutes, tz_offset_seconds = divmod(tz, 60) zone = FixedOffset(tz_offset_minutes) else: zone = timezone(tz) return zone.localize(t)
def test_now_without_tz(self): t = Time.now() self.assertIsInstance(t, Time)
def test_nanosecond_resolution_localtime_output(cypher_eval): value = cypher_eval("RETURN localtime('12:34:56.789012345')") assert isinstance(value, Time) assert value == Time(12, 34, 56.789012345)
def test_whole_second_localtime_output(cypher_eval): value = cypher_eval("RETURN localtime('12:34:56')") assert isinstance(value, Time) assert value == Time(12, 34, 56)
def test_time_with_numeric_time_offset_output(cypher_eval): value = cypher_eval("RETURN time('12:34:56.789012345+0130')") assert isinstance(value, Time) assert value == Time(12, 34, 56.789012345, tzinfo=FixedOffset(90))
def test_nanosecond_resolution_time_output(cypher_eval): value = cypher_eval("RETURN time('12:34:56.789012345')") assert isinstance(value, Time) assert value == Time(12, 34, 56.789012345, tzinfo=FixedOffset(0))
def test_whole_second_time_output(cypher_eval): value = cypher_eval("RETURN time('12:34:56')") assert isinstance(value, Time) assert value == Time(12, 34, 56, tzinfo=FixedOffset(0))
def test_time_array_input(cypher_eval): data = [Time(12, 34, 56), Time(10, 0, 0)] value = cypher_eval("CREATE (a {x:$x}) RETURN a.x", x=data) assert value == data
def setModelData(self, editor, model, index): ''' this takes the current value of the editor widget, converts it back to a string and stores it back in the item model ''' dataType = index.data(Qt.UserRole + 1) # string, integer, and float use a qlineedit if isinstance(editor, QLineEdit): if dataType == DataType.DATETIME.value: value = editor.text() try: aNeoDateTime = DateTime.from_iso_format(value) saveStr = aNeoDateTime.iso_format() model.setData(index, saveStr, Qt.DisplayRole) except: self.helper.displayErrMsg( "Date Time", "Entered text [{}] is not a valid ISO date time.". format(value)) model.setData(index, value, Qt.DisplayRole) elif dataType == DataType.LOCALDATETIME.value: value = editor.text() try: aNeoDateTime = DateTime.from_iso_format(value) saveStr = aNeoDateTime.iso_format() model.setData(index, saveStr, Qt.DisplayRole) except: self.helper.displayErrMsg( "Date Time", "Entered text [{}] is not a valid ISO date time.". format(value)) model.setData(index, value, Qt.DisplayRole) elif dataType in [DataType.LOCALTIME.value, DataType.TIME.value]: value = editor.text() try: aNeoTime = Time.from_iso_format(value) saveStr = aNeoTime.iso_format() tz = "" if aNeoTime.tzinfo is not None: offset = aNeoTime.tzinfo.utcoffset(self) tz = "%+03d:%02d" % divmod( offset.total_seconds() // 60, 60) returnStr = "{}{}".format(saveStr, tz) model.setData(index, returnStr, Qt.DisplayRole) except: self.helper.displayErrMsg( "Time", "Entered text [{}] is not a valid ISO time.".format( value)) model.setData(index, value, Qt.DisplayRole) else: value = editor.text() model.setData(index, value, Qt.DisplayRole) # boolean datatype uses a listbox elif isinstance(editor, QComboBox): value = editor.currentText() model.setData(index, value, Qt.DisplayRole) elif isinstance(editor, FrmPoint): value = editor.getText() model.setData(index, value, Qt.DisplayRole) elif isinstance(editor, FrmGPoint): value = editor.getText() model.setData(index, value, Qt.DisplayRole) # elif isinstance(editor, FrmTime ): # value = editor.getText() # model.setData(index, value, Qt.DisplayRole) # elif isinstance(editor, FrmDateTime ): # value = editor.getText() # model.setData(index, value, Qt.DisplayRole) elif isinstance(editor, QDateTimeEdit): if dataType == DataType.DATE.value: value = editor.date().toString(Qt.ISODate) model.setData(index, value, Qt.DisplayRole)
def test_iso_format_with_time_zone_case_2(): # python -m pytest tests/unit/time/test_time.py -s -v -k test_iso_format_with_time_zone_case_2 expected = Time.from_iso_format("07:54:02.129790999+01:00") assert expected.tzinfo == FixedOffset(60) assert expected.iso_format() == "07:54:02.129790999+01:00"
def test_bad_attribute(self): t = Time(12, 34, 56.789) with self.assertRaises(AttributeError): _ = t.x
def test_utc_now(self): t = Time.utc_now() self.assertIsInstance(t, Time)
def test_now_with_tz(self): t = Time.now(tz=timezone_us_eastern) self.assertIsInstance(t, Time) self.assertEqual(t.tzinfo, timezone_us_eastern)
def test_str(self): t = Time(12, 34, 56.789123456) self.assertEqual(str(t), "12:34:56.789123456")