def test_06(self): logger = HenriqueLogger.func_level2logger(self.test_06, logging.DEBUG) Chatroom.chatrooms2upsert([ChatroomKakaotalk.chatroom()]) ServerDoc.codenames2delete([Server.Codename.MARIS]) sender_name = "iris" channel_user_codename = ChannelUserKakaotalk.sender_name2codename( sender_name) ChannelUser.channel_users2upsert( [ChannelUserKakaotalk.sender_name2channel_user(sender_name)]) tz = pytz.timezone("Asia/Seoul") now_tz = DatetimeTool.datetime2truncate_seconds(datetime.now(tz)) # hour = (now_tz + timedelta(seconds=3600)).hour packet1 = { KhalaPacket.Field.TEXT: "?남만 {}".format(now_tz.strftime("%I:%M %p")), KhalaPacket.Field.CHATROOM: KakaotalkUWOChatroom.codename(), KhalaPacket.Field.CHANNEL_USER: channel_user_codename, KhalaPacket.Field.SENDER_NAME: sender_name, } NanbanSkill.packet2response(packet1) packet2 = { KhalaPacket.Field.TEXT: "?남만 +2분 1초", KhalaPacket.Field.CHATROOM: KakaotalkUWOChatroom.codename(), KhalaPacket.Field.CHANNEL_USER: channel_user_codename, KhalaPacket.Field.SENDER_NAME: sender_name, } NanbanSkill.packet2response(packet2) doc = ServerDoc.codename2doc(Server.Codename.MARIS) dt_nanban = DatetimeTool.astimezone( ServerNanban.nanban2datetime(ServerDoc.doc2nanban(doc)), tz) logger.debug({ "now_tz": now_tz, "dt_nanban": dt_nanban, 'now_tz.strftime("%I:%M %p")': now_tz.strftime("%I:%M %p"), }) ref = (now_tz + timedelta(seconds=2 * 60 + 1)).timetz() self.assertEquals(dt_nanban.timetz(), ref)
def test_08(self): tz = pytz.timezone("Asia/Seoul") dt = DatetimeTool.astimezone(datetime.now(pytz.utc), tz) dt_truncate = DatetimeTool.truncate(dt, DatetimeUnit.SECOND) self.assertEqual(dt.year, dt_truncate.year) self.assertEqual(dt.month, dt_truncate.month) self.assertEqual(dt.day, dt_truncate.day) self.assertEqual(dt.hour, dt_truncate.hour) self.assertEqual(dt.minute, dt_truncate.minute) if dt.second: self.assertNotEqual(dt.second, dt_truncate.second) if dt.microsecond: self.assertNotEqual(dt.microsecond, dt_truncate.microsecond)
def test_06(self): dt_start = datetime(2020, 2, 28, 22, 22, 22, 222222) dt_end = datetime(2021, 2, 28, 22, 22, 22, 222222) hyp = DatetimeTool.datetime_span2years([dt_start, dt_end]) ref = 1 self.assertEqual(hyp, ref)
def test_02(self): dt_now = datetime.now(pytz.utc) dt_from = dt_now - timedelta(seconds=60) dt_result = DatetimeTool.from_pivot_period2next( dt_from, dt_now, timedelta(days=1)) self.assertEqual(dt_result, dt_from + timedelta(days=1))
def nanban_datetime2upsert_mongo( cls, packet, datetime_nanban, ): logger = HenriqueLogger.func_level2logger( cls.nanban_datetime2upsert_mongo, logging.DEBUG) server_codename = HenriquePacket.packet2server(packet) text_in = KhalaPacket.packet2text(packet) # server = Server.codename2server(server_codename) dt_utc = DatetimeTool.astimezone(datetime_nanban, pytz.utc) # raise Exception({"datetime_in":datetime_in,"dt_utc":dt_utc}) nanban = { ServerNanban.Field.DATETIME: dt_utc, ServerNanban.Field.COMMAND_IN: text_in, } doc_this = { ServerDoc.Field.CODENAME: server_codename, ServerDoc.Field.NANBAN: nanban, } logger.debug({ "datetime_nanban": datetime_nanban, "doc_this": doc_this, }) ServerDoc.docs2upsert([doc_this])
def test_01(self): Chatroom.chatrooms2upsert([ChatroomKakaotalk.chatroom()]) now_utc = datetime.now(pytz.utc) sender_name = "iris" channel_user_codename = ChannelUserKakaotalk.sender_name2codename( sender_name) packet = { KhalaPacket.Field.TEXT: "?남만 지금", KhalaPacket.Field.CHATROOM: KakaotalkUWOChatroom.codename(), KhalaPacket.Field.CHANNEL_USER: channel_user_codename, KhalaPacket.Field.SENDER_NAME: sender_name, } NanbanSkill.nanban_datetime2upsert_mongo( packet, now_utc, ) response = NanbanSkill.server_lang2lookup(Server.Codename.MARIS, "ko") hyp = "\n".join( ListTool.indexes2filtered(response.splitlines(), [0, 2])) utc_nanban = now_utc + NanbanTimedelta.period() tz = pytz.timezone("Asia/Seoul") # now_tz = DatetimeTool.astimezone(now_utc, tz) now_nanban = DatetimeTool.astimezone(utc_nanban, tz) ref = """[남만시각] 글로벌서버 다음 남만 시각: {} (KST) / 약 2시간 후""".format( now_nanban.strftime("%I:%M:%S %p").lstrip("0")) # pprint(hyp) self.assertEqual(hyp, ref)
def datetime2text(cls, dt, tzdb): tz_abbr = TimezoneTool.tzdb2abbreviation(tzdb) dt_tz = DatetimeTool.astimezone(dt, pytz.timezone(tzdb)) str_datetime = dt_tz.strftime("%I:%M:%S %p").lstrip("0") str_out = "{} ({})".format(str_datetime, tz_abbr) return str_out
def server2datetime_nanban( cls, server_codename, dt_pivot, ): server_doc = ServerDoc.codename2doc(server_codename) if not server_doc: return None nanban_prev = ServerDoc.doc2nanban(server_doc) nanban_datetime_prev = ServerNanban.nanban2datetime(nanban_prev) # dt_nanban_raw # utc_now = datetime.now(tz=pytz.utc) nanban_datetime_this = DatetimeTool.from_pivot_period2next( nanban_datetime_prev, dt_pivot, NanbanTimedelta.period()) if nanban_datetime_this != nanban_datetime_prev: nanban = DictTool.filter( lambda k, v: v is not None, { ServerNanban.Field.DATETIME: nanban_datetime_this, ServerNanban.Field.COMMAND_IN: ServerNanban.nanban2command_in(nanban_prev), }) doc = { ServerDoc.Field.CODENAME: server_codename, ServerDoc.Field.NANBAN: nanban, } ServerDoc.docs2upsert([doc]) return nanban_datetime_this
def test_04(self): dt_pivot = datetime(2022, 2, 2, 22, 22, 22, 222222) hyp = DatetimeTool.floor( dt_pivot, DatetimeUnit.MILLISECOND, ) ref = datetime(2022, 2, 2, 22, 22, 22, 222000) self.assertEqual(hyp, ref)
def test_03(self): tz_la = pytz.timezone("America/Los_Angeles") dt_from = datetime(2020, 10, 31, tzinfo=tz_la) dt_to = datetime(2020, 11, 2, tzinfo=tz_la) hyp = DatetimeTool.datetime_pair2days_difference(( dt_from, dt_to, )) self.assertEqual(hyp, 2)
def test_09(self): tz = pytz.timezone("Asia/Seoul") dt_pivot = datetime(2020, 1, 1, 5, 15, 31, tzinfo=pytz.utc) dt_01 = DatetimeTool.datetime2nearest( dt_pivot, datetime(2020, 1, 1, 5, tzinfo=pytz.utc), timedelta(minutes=2), Nearest.PAST, ) self.assertEqual(dt_01, datetime(2020, 1, 1, 5, 14, tzinfo=pytz.utc)) dt_02 = DatetimeTool.datetime2nearest( dt_pivot, datetime(2020, 1, 1, 5, tzinfo=pytz.utc), timedelta(minutes=2), Nearest.COMING, ) self.assertEqual(dt_02, datetime(2020, 1, 1, 5, 16, tzinfo=pytz.utc))
def datefmt(cls): from foxylib.tools.datetime.datetime_tool import DatetimeTool return DatetimeTool.iso8601()
def packet2response(cls, packet): logger = HenriqueLogger.func_level2logger(cls.packet2response, logging.DEBUG) logger.debug({"packet": packet}) server_codename = HenriquePacket.packet2server(packet) chatroom = Chatroom.codename2chatroom( KhalaPacket.packet2chatroom(packet)) locale = Chatroom.chatroom2locale(chatroom) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) tz = pytz.timezone(HenriqueLocale.lang2tzdb(lang)) dt_now = datetime.now(tz) text_in = KhalaPacket.packet2text(packet) config = {HenriqueEntity.Config.Field.LOCALE: locale} # entity_list = RelativeTimedeltaEntity.text2entity_list(text_in, config=config) entity_list = HenriqueEntity.text_extractors2entity_list( text_in, cls.config2extractors(config), ) logger.debug({ "len(entity_list)": len(entity_list), "entity_list": entity_list, }) if not entity_list: return cls.server_lang2lookup(server_codename, lang) if len(entity_list) != 1: return # Invalid request entity = l_singleton2obj(entity_list) if FoxylibEntity.entity2type( entity) == RelativeTimedeltaEntity.entity_type(): reldelta = RelativeTimedeltaEntity.entity2relativedelta(entity) dt_in = cls.relativedelta2nanban_datetime( server_codename, reldelta, ) # raise Exception({"dt_in":dt_in, "reldelta":reldelta}) if dt_in is None: msg_error = NanbanSkillError.codename_lang2text( NanbanSkillError.Codename.NO_PREV_NANBAN_ERROR, lang) raise HenriqueCommandError(msg_error) logger.debug({ "reldelta": reldelta, }) elif FoxylibEntity.entity2type(entity) == TimeEntity.entity_type(): time_in = TimeEntity.value2datetime_time( FoxylibEntity.entity2value(entity)) dt_in = PytzTool.localize(datetime.combine(dt_now.date(), time_in), tz) logger.debug({ "time_in": time_in, "dt_in": dt_in, }) else: raise RuntimeError({ "Invalid entity type: {}".format( FoxylibEntity.entity2type(entity)) }) dt_nearest = DatetimeTool.datetime2nearest(dt_in, dt_now, NanbanTimedelta.period(), Nearest.COMING) logger.debug({ "text_in": text_in, "dt_now": dt_now, "dt_in": dt_in, "dt_nearest": dt_nearest, }) cls.nanban_datetime2upsert_mongo(packet, dt_nearest) return cls.server_lang2lookup(server_codename, lang)
def tz2now(cls, tz): dt_natural = DatetimeTool.tz2now(tz) dt_truncated = DatetimeTool.floor(dt_natural, unit=DatetimeUnit.MILLISECOND) return dt_truncated