Exemple #1
0
    def test__to_datetime(self):
        # datetime正常
        input_list = ['2018/11/20', '2018/11/20 13:48', '2018/01/01 00:00:00']
        for s in input_list:
            with self.subTest(s=s):
                actual = Utils.to_datetime(s)
                self.assertIsInstance(actual, datetime)

        # 入力値が文字列だがdatetime変換できない場合、または入力値が文字列以外
        input_list = [20181120, 201811201348, 20200101000000, '8月12日', None]
        for s in input_list:
            with self.subTest(s=s):
                actual = Utils.to_datetime(s)
                self.assertIsInstance(actual, str)
Exemple #2
0
    def _convert_datetime(self, child_dict: dict) -> dict:
        """
        | 辞書内辞書になっている文字列日付時間データを、辞書内日付時間に変換

        | (例)
        | {'start_date': {'#date': '1981-04-23'}}
        | から
        | {'start_date': 1981-04-23T00:00:00}

        :param dict child_dict:
        :return: result
        :rtype: dict
        """
        result = copy.deepcopy(child_dict)
        if isinstance(child_dict, dict):
            try:
                for key, value in child_dict.items():

                    if isinstance(value, dict) and self.date in value:
                        result.update({
                            key:
                            Utils.to_datetime(child_dict[key][self.date])
                        })
            except AttributeError:
                sys.exit(f'日付変換に失敗しました.構造に問題があります. {child_dict}')
        return result
Exemple #3
0
Fichier : db.py Projet : ryde/EDMAN
    def _convert_datetime_dict(self, amend: dict) -> dict:
        """
        辞書内辞書になっている文字列日付時間データを、辞書内日付時間に変換

        (例)
        {'start_date': {'#date': '1981-04-23'}}
        から
        {'start_date': 1981-04-23T00:00:00}
        amendにリストデータがある場合は中身も変換対象とする

        :param dict amend:
        :return: result
        :rtype: dict
        """
        result = copy.deepcopy(amend)
        if isinstance(amend, dict):
            try:
                for key, value in amend.items():
                    if isinstance(value, dict) and self.date in value:
                        result.update(
                            {
                                key: Utils.to_datetime(
                                    amend[key][self.date])
                            })
                    elif isinstance(value, list):

                        buff = [Utils.to_datetime(i[self.date])
                                if isinstance(i, dict) and self.date in i
                                else i
                                for i in value]
                        result.update({key: buff})
                    else:
                        result.update({key: value})
            except AttributeError:
                sys.exit(f'日付変換に失敗しました.構造に問題があります. {amend}')
        return result
Exemple #4
0
    def _date_replace(self, list_data: list) -> list:
        """
        | リスト内の要素に{'#date':日付時間}のデータが含まれていたら
        | datetimeオブジェクトに変換する
        | 例
        | [{'#date':2019-02-28 11:43:22}, ' test_date']
        | ↓
        | [datetime.datetime(2019, 2, 28, 11, 43, 22), 'test_date']

        :param list list_data:
        :return:
        :rtype: list
        """
        return [
            Utils.to_datetime(i[self.date])
            if isinstance(i, dict) and self.date in i else i for i in list_data
        ]