def grdt_to_imdt(grdt: GregorianDateTime, imdt_timezone: t.Optional[str]) -> ImperialDateTime: """Transform grdt to imdt.""" imdt_timezone = imdt_timezone or "+00:00" juld = grdt_to_juld(grdt.to_utc_naive()) tert = juld_to_tert(juld) mrsd = tert_to_mrsd(tert) imsn = mrsd_to_imsn(mrsd) return ImperialDateTime.from_standard_naive(imsn_to_imdt(imsn), imdt_timezone)
def test_imsn_to_imdt(self): """Test conversions ImperialSolNumber to ImperialDateTime.""" for (imdt, imsn) in [ (ImperialDateTime(0, 1, 1, 0, 0, 0, None), ImperialSolNumber(0.0)), (ImperialDateTime(1000, 1, 1, 0, 0, 0, None), ImperialSolNumber(668596.0)), (ImperialDateTime(1, 1, 1, 6, 14, 25, None), ImperialSolNumber(668.26001)), ( ImperialDateTime(1987, 6, 7, 12, 40, 30, None), ImperialSolNumber(1328646, 45630.0), ), (ImperialDateTime(3, 4, 5, 6, 7, 8, None), ImperialSolNumber(2093.25495)), (ImperialDateTime(-1, 24, 28, 0, 0, 0, None), ImperialSolNumber(-1.0)), ]: with self.subTest(imsn=imsn): self.assertEqual(imdt, imsn_to_imdt(imsn))
def to_standard_naive(self) -> "ImperialDateTime": """Convert to naive ImperialDateTime as standard timezone.""" from imperial_calendar.transform import imdt_to_imsn, imsn_to_imdt if self.timezone is None: raise Exception(f"This is naive: {self.__dict__}") imdt = self.copy() imdt.timezone = None imsn = imdt_to_imsn(imdt) imsn.second -= self.offset * 60.0 * 60.0 if imsn.second < 0: imsn.day -= 1 imsn.second += 24.0 * 60.0 * 60.0 elif imsn.second >= 24.0 * 60.0 * 60.0: imsn.day += 1 imsn.second -= 24.0 * 60.0 * 60.0 return imsn_to_imdt(imsn)
def from_standard_naive(cls, imdt: "ImperialDateTime", timezone=str) -> "ImperialDateTime": """From standard timezone naive ImperialDateTime.""" from imperial_calendar.transform import imdt_to_imsn, imsn_to_imdt if not (imdt.timezone is None): raise Exception(f"This is not naive: {imdt.__dict__}") imsn = imdt_to_imsn(imdt) imsn.second += parse_timezone(timezone) * 60.0 * 60.0 if imsn.second < 0: imsn.day -= 1 imsn.second += 24.0 * 60.0 * 60.0 elif imsn.second >= 24.0 * 60.0 * 60.0: imsn.day += 1 imsn.second -= 24.0 * 60.0 * 60.0 imdt = imsn_to_imdt(imsn) imdt.timezone = timezone return imdt
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__, })