Esempio n. 1
0
    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])
Esempio n. 2
0
    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)
Esempio n. 3
0
    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
Esempio n. 4
0
    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)
Esempio n. 5
0
    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)