def from_utc_naive(cls, grdt: "GregorianDateTime", timezone: str) -> "GregorianDateTime": """From UTC naive GregorianDateTime.""" from imperial_calendar.transform import grdt_to_juld, juld_to_grdt if not (grdt.timezone is None): raise Exception(f"This is not naive: {grdt.__dict__}") parsed_tz = parse_timezone(timezone) if hasattr(parsed_tz, "localize") and callable( t.cast(t.Any, parsed_tz).localize): dt = datetime( grdt.year, grdt.month, grdt.day, grdt.hour, grdt.minute, grdt.second, tzinfo=utc, ) dt = dt.astimezone(parsed_tz) return cls(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, timezone) juld = grdt_to_juld(grdt) juld.second += (parsed_tz.utcoffset(datetime(1970, 1, 1, 0, 0, 0)) or timedelta(0)).total_seconds() if juld.second < 0.0: juld.day -= 1 juld.second += 24.0 * 60.0 * 60.0 elif juld.second >= 24.0 * 60.0 * 60.0: juld.day += 1 juld.second -= 24.0 * 60.0 * 60.0 grdt = juld_to_grdt(juld) grdt.timezone = timezone return grdt
def to_utc_naive(self) -> "GregorianDateTime": """Convert to naive GregorianDateTime as UTC.""" from imperial_calendar.transform import grdt_to_juld, juld_to_grdt if self.timezone is None: raise Exception(f"This is naive: {self.__dict__}") parsed_tz = parse_timezone(self.timezone) if hasattr(parsed_tz, "localize") and callable( t.cast(t.Any, parsed_tz).localize): dt: datetime = t.cast(t.Any, parsed_tz).localize( datetime(self.year, self.month, self.day, self.hour, self.minute, self.second)) dt = dt.astimezone(utc) return self.__class__(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, None) grdt = self.copy() grdt.timezone = None juld = grdt_to_juld(grdt) juld.second -= (parsed_tz.utcoffset(datetime(1970, 1, 1, 0, 0, 0)) or timedelta(0)).total_seconds() if juld.second < 0.0: juld.day -= 1 juld.second += 24.0 * 60.0 * 60.0 elif juld.second >= 24.0 * 60.0 * 60.0: juld.day += 1 juld.second -= 24.0 * 60.0 * 60.0 return juld_to_grdt(juld)
def imdt_to_grdt(imdt: ImperialDateTime, grdt_timezone: str) -> GregorianDateTime: """Transform imdt to grdt.""" imsn = imdt_to_imsn(imdt.to_standard_naive()) mrsd = imsn_to_mrsd(imsn) tert = mrsd_to_tert(mrsd) juld = tert_to_juld(tert) return GregorianDateTime.from_utc_naive(juld_to_grdt(juld), grdt_timezone)
def test_transform_date(self): """ 日附を變換する. cf. https://ja.wikipedia.org/wiki/%E3%83%A6%E3%83%AA%E3%82%A6%E3%82%B9%E9%80%9A%E6%97%A5 """ for (juld, grdt) in [ (JulianDay(2299161.0), GregorianDateTime(1582, 10, 15, 12, 0, 0, None)), (JulianDay(2345678.0), GregorianDateTime(1710, 2, 23, 12, 0, 0, None)), (JulianDay(2400000.5), GregorianDateTime(1858, 11, 17, 0, 0, 0, None)), (JulianDay(2451545.0), GregorianDateTime(2000, 1, 1, 12, 0, 0, None)), (JulianDay(2456789.0), GregorianDateTime(2014, 5, 11, 12, 0, 0, None)), (JulianDay(2567890.0), GregorianDateTime(2318, 7, 18, 12, 0, 0, None)), (JulianDay(3000000.0), GregorianDateTime(3501, 8, 15, 12, 0, 0, None)), (JulianDay(3456789.0), GregorianDateTime(4752, 4, 7, 12, 0, 0, None)), (JulianDay(4000000.0), GregorianDateTime(6239, 7, 12, 12, 0, 0, None)), ]: with self.subTest(juld=juld): self.assertEqual(grdt, juld_to_grdt(juld))
def datetimes() -> Response: """Get datetimes.""" params = json.loads(t.cast(str, request.args.get("params"))) if "grdt" in params: _grdt = params["grdt"] grdt = GregorianDateTime( _grdt["year"], _grdt["month"], _grdt["day"], _grdt["hour"], _grdt["minute"], _grdt["second"], params["grdt_timezone"], ) juld = grdt_to_juld(grdt.to_utc_naive()) tert = juld_to_tert(juld) mrsd = tert_to_mrsd(tert) imsn = mrsd_to_imsn(mrsd) imdt = ImperialDateTime.from_standard_naive(imsn_to_imdt(imsn), params["imdt_timezone"]) elif "juld" in params: _juld = params["juld"] juld = JulianDay(_juld["day"], _juld["second"]) grdt = GregorianDateTime.from_utc_naive(juld_to_grdt(juld), params["grdt_timezone"]) tert = juld_to_tert(juld) mrsd = tert_to_mrsd(tert) imsn = mrsd_to_imsn(mrsd) imdt = ImperialDateTime.from_standard_naive(imsn_to_imdt(imsn), params["imdt_timezone"]) elif "tert" in params: tert = TerrestrialTime(params["tert"]["terrestrial_time"]) juld = tert_to_juld(tert) grdt = GregorianDateTime.from_utc_naive(juld_to_grdt(juld), params["grdt_timezone"]) mrsd = tert_to_mrsd(tert) imsn = mrsd_to_imsn(mrsd) imdt = ImperialDateTime.from_standard_naive(imsn_to_imdt(imsn), params["imdt_timezone"]) elif "mrsd" in params: mrsd = MarsSolDate(params["mrsd"]["mars_sol_date"]) tert = mrsd_to_tert(mrsd) juld = tert_to_juld(tert) grdt = GregorianDateTime.from_utc_naive(juld_to_grdt(juld), params["grdt_timezone"]) imsn = mrsd_to_imsn(mrsd) imdt = ImperialDateTime.from_standard_naive(imsn_to_imdt(imsn), params["imdt_timezone"]) elif "imsn" in params: _imsn = params["imsn"] imsn = ImperialSolNumber(_imsn["day"], _imsn["second"]) mrsd = imsn_to_mrsd(imsn) tert = mrsd_to_tert(mrsd) juld = tert_to_juld(tert) grdt = GregorianDateTime.from_utc_naive(juld_to_grdt(juld), params["grdt_timezone"]) imdt = ImperialDateTime.from_standard_naive(imsn_to_imdt(imsn), params["imdt_timezone"]) elif "imdt" in params: _imdt = params["imdt"] imdt = ImperialDateTime( _imdt["year"], _imdt["month"], _imdt["day"], _imdt["hour"], _imdt["minute"], _imdt["second"], params["imdt_timezone"], ) imsn = imdt_to_imsn(imdt.to_standard_naive()) mrsd = imsn_to_mrsd(imsn) tert = mrsd_to_tert(mrsd) juld = tert_to_juld(tert) grdt = GregorianDateTime.from_utc_naive(juld_to_grdt(juld), params["grdt_timezone"]) else: raise Exception(f"Unknown pattern of params: {params}") return jsonify({ "grdt": grdt.__dict__, "juld": juld.__dict__, "delta_t": juld.delta_t, "tert": tert.__dict__, "mrls": tert_to_mrls(tert), "mrsd": mrsd.__dict__, "imsn": imsn.__dict__, "imdt": imdt.__dict__, })