{"id":44172,"date":"2025-08-07T14:37:58","date_gmt":"2025-08-07T05:37:58","guid":{"rendered":"https:\/\/techgym.jp\/?p=44172"},"modified":"2025-10-19T23:37:18","modified_gmt":"2025-10-19T14:37:18","slug":"python-datetimepytz","status":"publish","type":"post","link":"https:\/\/techgym.jp\/column\/python-datetimepytz\/","title":{"rendered":"Python datetime\u30fbpytz\u3067\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u64cd\u4f5c\u5b8c\u5168\u30ac\u30a4\u30c9\u3010\u8a2d\u5b9a\u30fb\u53d6\u5f97\u30fb\u5909\u63db\u30fb\u524a\u9664\u3011"},"content":{"rendered":"\n<p><iframe loading=\"lazy\" width=\"560\" height=\"314\" src=\"\/\/www.youtube.com\/embed\/7iX9nAJE0cE\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Python\u3067\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u3092\u6271\u3046\u65b9\u6cd5\u3092\u3001\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e<code>datetime<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3068<code>pytz<\/code>\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u3066\u8a73\u3057\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u8a2d\u5b9a\u3001\u53d6\u5f97\u3001\u5909\u63db\u3001\u524a\u9664\u306e\u5168\u64cd\u4f5c\u3092\u5b66\u3073\u307e\u3057\u3087\u3046\u3002<\/p>\n\n<h2>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u57fa\u672c\u6982\u5ff5<\/h2>\n<pre><code class=\"language-python\">from datetime import datetime, timezone, timedelta\nimport pytz\n\n# UTC\uff08\u5354\u5b9a\u4e16\u754c\u6642\uff09\nutc_now = datetime.now(timezone.utc)\nprint(f\"UTC: {utc_now}\")\n\n# \u30ed\u30fc\u30ab\u30eb\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\nlocal_now = datetime.now()\nprint(f\"\u30ed\u30fc\u30ab\u30eb: {local_now}\")\n\n# pytz\u306b\u3088\u308b\u65e5\u672c\u6a19\u6e96\u6642\njst = pytz.timezone('Asia\/Tokyo')\njst_now = datetime.now(jst)\nprint(f\"JST: {jst_now}\")\n<\/code><\/pre>\n<h2>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u8a2d\u5b9a<\/h2>\n<h3>\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u8a2d\u5b9a<\/h3>\n<pre><code class=\"language-python\">from datetime import datetime, timezone, timedelta\n\n# UTC\u8a2d\u5b9a\ndt_utc = datetime(2024, 1, 15, 14, 30, 25, tzinfo=timezone.utc)\nprint(f\"UTC: {dt_utc}\")\n\n# \u56fa\u5b9a\u30aa\u30d5\u30bb\u30c3\u30c8\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u8a2d\u5b9a\njst_offset = timezone(timedelta(hours=9))\ndt_jst = datetime(2024, 1, 15, 23, 30, 25, tzinfo=jst_offset)\nprint(f\"JST: {dt_jst}\")\n\n# \u30ca\u30a4\u30fc\u30d6\uff08\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u306a\u3057\uff09\u304b\u3089\u30a2\u30a6\u30a7\u30a2\uff08\u60c5\u5831\u3042\u308a\uff09\u306b\u5909\u63db\nnaive_dt = datetime(2024, 1, 15, 14, 30, 25)\naware_dt = naive_dt.replace(tzinfo=timezone.utc)\nprint(f\"\u5909\u63db\u5f8c: {aware_dt}\")\n<\/code><\/pre>\n<h3>pytz\u3067\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u8a2d\u5b9a<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\n# \u4e3b\u8981\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u8a2d\u5b9a\nutc = pytz.UTC\njst = pytz.timezone('Asia\/Tokyo')\nest = pytz.timezone('US\/Eastern')\npst = pytz.timezone('US\/Pacific')\n\n# localize\u30e1\u30bd\u30c3\u30c9\u3067\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u3092\u8a2d\u5b9a\nnaive_dt = datetime(2024, 1, 15, 14, 30, 25)\njst_dt = jst.localize(naive_dt)\nprint(f\"JST\u8a2d\u5b9a: {jst_dt}\")\n\n# \u590f\u6642\u9593\u306e\u8003\u616e\nest_dt = est.localize(datetime(2024, 7, 15, 14, 30, 25))\nprint(f\"EST\uff08\u590f\u6642\u9593\uff09: {est_dt}\")\n<\/code><\/pre>\n<h2>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u306e\u53d6\u5f97<\/h2>\n<h3>\u73fe\u5728\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u53d6\u5f97<\/h3>\n<pre><code class=\"language-python\">from datetime import datetime\nimport pytz\n\n# datetime\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u53d6\u5f97\njst = pytz.timezone('Asia\/Tokyo')\ndt = jst.localize(datetime(2024, 1, 15, 14, 30, 25))\n\nprint(f\"\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u540d: {dt.tzinfo}\")           # Asia\/Tokyo\nprint(f\"\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u7565\u79f0: {dt.tzname()}\")       # JST\nprint(f\"UTC\u30aa\u30d5\u30bb\u30c3\u30c8: {dt.utcoffset()}\")       # 9:00:00\nprint(f\"\u590f\u6642\u9593: {dt.dst()}\")                   # 0:00:00\n<\/code><\/pre>\n<h3>\u5229\u7528\u53ef\u80fd\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u4e00\u89a7<\/h3>\n<pre><code class=\"language-python\">import pytz\n\n# \u3059\u3079\u3066\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u4e00\u89a7\uff08\u4e00\u90e8\u8868\u793a\uff09\nall_timezones = pytz.all_timezones\nprint(f\"\u7dcf\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u6570: {len(all_timezones)}\")\n\n# \u4e00\u822c\u7684\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u4e00\u89a7\ncommon_timezones = pytz.common_timezones\nprint(\"\\n\u4e00\u822c\u7684\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\uff08\u30a2\u30b8\u30a2\uff09:\")\nasia_timezones = [tz for tz in common_timezones if 'Asia' in tz]\nfor tz in asia_timezones[:5]:\n    print(f\"  {tz}\")\n<\/code><\/pre>\n<h3>\u56fd\u5225\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u53d6\u5f97<\/h3>\n<pre><code class=\"language-python\">import pytz\n\n# \u7279\u5b9a\u306e\u56fd\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\njp_timezones = pytz.country_timezones['JP']\nus_timezones = pytz.country_timezones['US']\n\nprint(f\"\u65e5\u672c\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3: {jp_timezones}\")\nprint(f\"\u30a2\u30e1\u30ea\u30ab\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u6570: {len(us_timezones)}\")\nprint(f\"\u30a2\u30e1\u30ea\u30ab\u4e3b\u8981\u90fd\u5e02: {us_timezones[:5]}\")\n<\/code><\/pre>\n<h2>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u5909\u63db<\/h2>\n<h3>\u57fa\u672c\u7684\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u5909\u63db<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\n# \u5143\u306e\u6642\u523b\uff08JST\uff09\njst = pytz.timezone('Asia\/Tokyo')\noriginal_dt = jst.localize(datetime(2024, 1, 15, 23, 30, 25))\nprint(f\"\u5143\u306e\u6642\u523b\uff08JST\uff09: {original_dt}\")\n\n# \u4ed6\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306b\u5909\u63db\nutc_dt = original_dt.astimezone(pytz.UTC)\nest_dt = original_dt.astimezone(pytz.timezone('US\/Eastern'))\npst_dt = original_dt.astimezone(pytz.timezone('US\/Pacific'))\n\nprint(f\"UTC: {utc_dt}\")\nprint(f\"EST: {est_dt}\")\nprint(f\"PST: {pst_dt}\")\n<\/code><\/pre>\n<h3>\u8907\u6570\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u4e00\u62ec\u5909\u63db<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\ndef convert_to_multiple_timezones(dt, timezones):\n    \"\"\"\u4e00\u3064\u306e\u6642\u523b\u3092\u8907\u6570\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306b\u5909\u63db\"\"\"\n    results = {}\n    for tz_name in timezones:\n        tz = pytz.timezone(tz_name)\n        converted = dt.astimezone(tz)\n        results[tz_name] = converted\n    return results\n\n# \u4f7f\u7528\u4f8b\nbase_time = pytz.timezone('Asia\/Tokyo').localize(\n    datetime(2024, 1, 15, 12, 0, 0)\n)\n\ntarget_zones = [\n    'UTC',\n    'US\/Eastern',\n    'US\/Pacific',\n    'Europe\/London',\n    'Australia\/Sydney'\n]\n\nconversions = convert_to_multiple_timezones(base_time, target_zones)\nprint(\"JST 12:00\u306e\u4e16\u754c\u5404\u5730\u3067\u306e\u6642\u523b:\")\nfor tz_name, converted_time in conversions.items():\n    print(f\"  {tz_name}: {converted_time.strftime('%H:%M (%Z)')}\")\n<\/code><\/pre>\n<h3>\u590f\u6642\u9593\u3092\u8003\u616e\u3057\u305f\u5909\u63db<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\ndef safe_timezone_conversion(dt, target_tz):\n    \"\"\"\u590f\u6642\u9593\u3092\u8003\u616e\u3057\u305f\u5b89\u5168\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u5909\u63db\"\"\"\n    try:\n        if isinstance(target_tz, str):\n            target_tz = pytz.timezone(target_tz)\n        \n        converted = dt.astimezone(target_tz)\n        \n        # \u590f\u6642\u9593\u60c5\u5831\u306e\u8868\u793a\n        dst_info = \"\uff08\u590f\u6642\u9593\uff09\" if converted.dst() else \"\uff08\u6a19\u6e96\u6642\u9593\uff09\"\n        return converted, dst_info\n    except Exception as e:\n        return None, f\"\u30a8\u30e9\u30fc: {e}\"\n\n# \u590f\u6642\u9593\u671f\u9593\u3067\u306e\u5909\u63db\u30c6\u30b9\u30c8\nsummer_jst = pytz.timezone('Asia\/Tokyo').localize(\n    datetime(2024, 7, 15, 12, 0, 0)\n)\n\nest_result, est_info = safe_timezone_conversion(summer_jst, 'US\/Eastern')\nprint(f\"\u590f\u6642\u9593\u671f\u9593 JST \u2192 EST: {est_result} {est_info}\")\n<\/code><\/pre>\n<h2>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u306e\u524a\u9664<\/h2>\n<h3>\u30a2\u30a6\u30a7\u30a2\u304b\u3089\u30ca\u30a4\u30fc\u30d6\u3078\u306e\u5909\u63db<\/h3>\n<pre><code class=\"language-python\">from datetime import datetime\nimport pytz\n\n# \u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u4ed8\u304d\u306e\u6642\u523b\njst = pytz.timezone('Asia\/Tokyo')\naware_dt = jst.localize(datetime(2024, 1, 15, 14, 30, 25))\nprint(f\"\u30a2\u30a6\u30a7\u30a2: {aware_dt}\")\nprint(f\"\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831: {aware_dt.tzinfo}\")\n\n# \u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u524a\u9664\uff08\u30ca\u30a4\u30fc\u30d6\u306b\u5909\u63db\uff09\nnaive_dt = aware_dt.replace(tzinfo=None)\nprint(f\"\u30ca\u30a4\u30fc\u30d6: {naive_dt}\")\nprint(f\"\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831: {naive_dt.tzinfo}\")\n<\/code><\/pre>\n<h3>UTC\u306b\u5909\u63db\u3057\u3066\u304b\u3089\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u524a\u9664<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\ndef remove_timezone_as_utc(aware_dt):\n    \"\"\"UTC\u306b\u5909\u63db\u3057\u3066\u304b\u3089\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u524a\u9664\"\"\"\n    utc_dt = aware_dt.astimezone(pytz.UTC)\n    naive_utc = utc_dt.replace(tzinfo=None)\n    return naive_utc\n\n# \u4f7f\u7528\u4f8b\njst_dt = pytz.timezone('Asia\/Tokyo').localize(\n    datetime(2024, 1, 15, 23, 30, 25)\n)\n\nnaive_utc = remove_timezone_as_utc(jst_dt)\nprint(f\"\u5143\u306e\u6642\u523b\uff08JST\uff09: {jst_dt}\")\nprint(f\"UTC\u5909\u63db\u5f8c\u30ca\u30a4\u30fc\u30d6: {naive_utc}\")\n<\/code><\/pre>\n<h2>\u5b9f\u7528\u7684\u306a\u5fdc\u7528\u4f8b<\/h2>\n<h3>\u4e16\u754c\u6642\u8a08\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\nclass WorldClock:\n    def __init__(self):\n        self.timezones = {\n            '\u6771\u4eac': 'Asia\/Tokyo',\n            '\u30cb\u30e5\u30fc\u30e8\u30fc\u30af': 'US\/Eastern',\n            '\u30ed\u30f3\u30c9\u30f3': 'Europe\/London',\n            '\u30b7\u30c9\u30cb\u30fc': 'Australia\/Sydney',\n            '\u30ed\u30b5\u30f3\u30bc\u30eb\u30b9': 'US\/Pacific'\n        }\n    \n    def show_current_times(self):\n        \"\"\"\u73fe\u5728\u6642\u523b\u3092\u5404\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u3067\u8868\u793a\"\"\"\n        utc_now = datetime.now(pytz.UTC)\n        \n        print(\"=== \u4e16\u754c\u6642\u8a08 ===\")\n        for city, tz_name in self.timezones.items():\n            tz = pytz.timezone(tz_name)\n            local_time = utc_now.astimezone(tz)\n            print(f\"{city:8}: {local_time.strftime('%Y-%m-%d %H:%M:%S %Z')}\")\n\n# \u4f7f\u7528\u4f8b\nworld_clock = WorldClock()\nworld_clock.show_current_times()\n<\/code><\/pre>\n<h3>\u4f1a\u8b70\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u30fc<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\nclass MeetingScheduler:\n    def __init__(self):\n        self.participants = {\n            'Tokyo': 'Asia\/Tokyo',\n            'New York': 'US\/Eastern',\n            'London': 'Europe\/London'\n        }\n    \n    def schedule_meeting(self, base_city, base_time_str):\n        \"\"\"\u6307\u5b9a\u90fd\u5e02\u306e\u6642\u523b\u3067\u4f1a\u8b70\u3092\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\"\"\"\n        # \u57fa\u6e96\u6642\u523b\u306e\u89e3\u6790\n        base_tz = pytz.timezone(self.participants[base_city])\n        base_dt = datetime.strptime(base_time_str, '%Y-%m-%d %H:%M')\n        base_aware = base_tz.localize(base_dt)\n        \n        print(f\"=== \u4f1a\u8b70\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\uff08{base_city}\u57fa\u6e96\uff09===\")\n        \n        for city, tz_name in self.participants.items():\n            tz = pytz.timezone(tz_name)\n            local_time = base_aware.astimezone(tz)\n            \n            # \u55b6\u696d\u6642\u9593\u30c1\u30a7\u30c3\u30af\n            hour = local_time.hour\n            if 9 &lt;= hour &lt;= 17:\n                status = \"\u2705 \u55b6\u696d\u6642\u9593\u5185\"\n            else:\n                status = \"\u274c \u55b6\u696d\u6642\u9593\u5916\"\n            \n            print(f\"{city:10}: {local_time.strftime('%m\/%d %H:%M')} {status}\")\n\n# \u4f7f\u7528\u4f8b\nscheduler = MeetingScheduler()\nscheduler.schedule_meeting('Tokyo', '2024-01-15 14:00')\n<\/code><\/pre>\n<h3>\u30ed\u30b0\u30d5\u30a1\u30a4\u30eb\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u5909\u63db<\/h3>\n<pre><code class=\"language-python\">import pytz\nimport re\nfrom datetime import datetime\n\ndef convert_log_timestamps(log_content, source_tz, target_tz):\n    \"\"\"\u30ed\u30b0\u30d5\u30a1\u30a4\u30eb\u306e\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u5225\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306b\u5909\u63db\"\"\"\n    source = pytz.timezone(source_tz)\n    target = pytz.timezone(target_tz)\n    \n    # \u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u30d1\u30bf\u30fc\u30f3\uff08\u4f8b\uff1a2024-01-15 14:30:25\uff09\n    pattern = r'(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})'\n    \n    def convert_timestamp(match):\n        timestamp_str = match.group(1)\n        # \u5143\u306e\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u3067\u30ed\u30fc\u30ab\u30e9\u30a4\u30ba\n        dt = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')\n        source_dt = source.localize(dt)\n        # \u30bf\u30fc\u30b2\u30c3\u30c8\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306b\u5909\u63db\n        target_dt = source_dt.astimezone(target)\n        return target_dt.strftime('%Y-%m-%d %H:%M:%S %Z')\n    \n    return re.sub(pattern, convert_timestamp, log_content)\n\n# \u4f7f\u7528\u4f8b\nsample_log = \"\"\"\n2024-01-15 14:30:25 INFO: Application started\n2024-01-15 14:31:00 ERROR: Connection failed\n2024-01-15 14:31:15 INFO: Retry successful\n\"\"\"\n\nconverted_log = convert_log_timestamps(sample_log, 'UTC', 'Asia\/Tokyo')\nprint(\"\u5909\u63db\u5f8c\u306e\u30ed\u30b0:\")\nprint(converted_log)\n<\/code><\/pre>\n<h2>\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h2>\n<h3>\u66d6\u6627\u306a\u6642\u523b\u306e\u51e6\u7406<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\ndef safe_localize(tz, dt, is_dst=None):\n    \"\"\"\u590f\u6642\u9593\u5207\u308a\u66ff\u3048\u6642\u306e\u66d6\u6627\u306a\u6642\u523b\u3092\u5b89\u5168\u306b\u51e6\u7406\"\"\"\n    try:\n        return tz.localize(dt, is_dst=is_dst)\n    except pytz.AmbiguousTimeError:\n        print(f\"\u66d6\u6627\u306a\u6642\u523b: {dt}\")\n        # \u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u6a19\u6e96\u6642\u9593\u3092\u9078\u629e\n        return tz.localize(dt, is_dst=False)\n    except pytz.NonExistentTimeError:\n        print(f\"\u5b58\u5728\u3057\u306a\u3044\u6642\u523b: {dt}\")\n        return None\n\n# \u590f\u6642\u9593\u5207\u308a\u66ff\u3048\u6642\u306e\u30c6\u30b9\u30c8\neastern = pytz.timezone('US\/Eastern')\n# 2024\u5e74\u306e\u590f\u6642\u9593\u7d42\u4e86\u6642\uff08\u66d6\u6627\u306a\u6642\u523b\uff09\nambiguous_time = datetime(2024, 11, 3, 1, 30)\nresult = safe_localize(eastern, ambiguous_time)\nprint(f\"\u51e6\u7406\u7d50\u679c: {result}\")\n<\/code><\/pre>\n<h3>\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u5909\u63db\u306e\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u30af\u30e9\u30b9<\/h3>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\nclass TimezoneConverter:\n    def __init__(self):\n        self.cache = {}\n    \n    def get_timezone(self, tz_name):\n        \"\"\"\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u53d6\u5f97\"\"\"\n        if tz_name not in self.cache:\n            self.cache[tz_name] = pytz.timezone(tz_name)\n        return self.cache[tz_name]\n    \n    def convert(self, dt, from_tz, to_tz):\n        \"\"\"\u5b89\u5168\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u5909\u63db\"\"\"\n        try:\n            # \u6587\u5b57\u5217\u306e\u5834\u5408\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\n            if isinstance(from_tz, str):\n                from_tz = self.get_timezone(from_tz)\n            if isinstance(to_tz, str):\n                to_tz = self.get_timezone(to_tz)\n            \n            # \u30ca\u30a4\u30fc\u30d6\u306a\u5834\u5408\u306ffrom_tz\u3067\u30ed\u30fc\u30ab\u30e9\u30a4\u30ba\n            if dt.tzinfo is None:\n                dt = from_tz.localize(dt)\n            \n            # \u30bf\u30fc\u30b2\u30c3\u30c8\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306b\u5909\u63db\n            return dt.astimezone(to_tz)\n        \n        except Exception as e:\n            print(f\"\u5909\u63db\u30a8\u30e9\u30fc: {e}\")\n            return None\n\n# \u4f7f\u7528\u4f8b\nconverter = TimezoneConverter()\nnaive_dt = datetime(2024, 1, 15, 14, 30, 25)\nresult = converter.convert(naive_dt, 'Asia\/Tokyo', 'US\/Eastern')\nprint(f\"\u5909\u63db\u7d50\u679c: {result}\")\n<\/code><\/pre>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li><strong>\u8a2d\u5b9a\u65b9\u6cd5<\/strong>\uff1a\n<ul>\n<li>\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\uff1a<code>timezone()<\/code>, <code>replace(tzinfo=...)<\/code><\/li>\n<li>pytz\uff1a<code>localize()<\/code>, <code>timezone()<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5909\u63db\u65b9\u6cd5<\/strong>\uff1a<code>astimezone()<\/code>\u3067\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u5909\u63db<\/li>\n<li><strong>\u524a\u9664\u65b9\u6cd5<\/strong>\uff1a<code>replace(tzinfo=None)<\/code>\u3067\u30ca\u30a4\u30fc\u30d6\u306b\u5909\u63db<\/li>\n<li><strong>\u5b9f\u7528\u4f8b<\/strong>\uff1a\u4e16\u754c\u6642\u8a08\u3001\u4f1a\u8b70\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u30fc\u3001\u30ed\u30b0\u5909\u63db<\/li>\n<li><strong>\u6ce8\u610f\u70b9<\/strong>\uff1a\u590f\u6642\u9593\u306e\u8003\u616e\u3001\u66d6\u6627\u306a\u6642\u523b\u306e\u51e6\u7406<\/li>\n<\/ul>\n<p>\u9069\u5207\u306a\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u51e6\u7406\u306b\u3088\u308a\u3001\u56fd\u969b\u7684\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u304c\u52b9\u7387\u7684\u306b\u884c\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u25a0<a href=\"https:\/\/amzn.to\/3VxGkpx\">\u3089\u304f\u3089\u304fPython\u587e &#8211; \u8aad\u3080\u3060\u3051\u3067\u30de\u30b9\u30bf\u30fc<\/a><\/h2>\n\n\n\n<p><iframe loading=\"lazy\" width=\"560\" height=\"314\" src=\"\/\/www.youtube.com\/embed\/7iX9nAJE0cE\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n\n\n\n<p>\u25a0\u30d7\u30ed\u30f3\u30d7\u30c8\u3060\u3051\u3067\u30aa\u30ea\u30b8\u30ca\u30eb\u30a2\u30d7\u30ea\u3092\u958b\u767a\u30fb\u516c\u958b\u3057\u3066\u307f\u305f\uff01\uff01<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"vGmHYCMvgV\"><a href=\"https:\/\/techgym.jp\/column\/ori-app\/\">\u30d7\u30ed\u30f3\u30d7\u30c8\u3060\u3051\u3067\u30aa\u30ea\u30b8\u30ca\u30eb\u30a2\u30d7\u30ea\u3092\u958b\u767a\u30fb\u516c\u958b\u3057\u3066\u307f\u305f\uff01\uff01<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;\u30d7\u30ed\u30f3\u30d7\u30c8\u3060\u3051\u3067\u30aa\u30ea\u30b8\u30ca\u30eb\u30a2\u30d7\u30ea\u3092\u958b\u767a\u30fb\u516c\u958b\u3057\u3066\u307f\u305f\uff01\uff01&#8221; &#8212; \u3010\u30c6\u30c3\u30af\u30b8\u30e0\u3011\u683c\u5b89\u30fb\u5bfe\u9762\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\" src=\"https:\/\/techgym.jp\/column\/ori-app\/embed\/#?secret=xztX55IYk2#?secret=vGmHYCMvgV\" data-secret=\"vGmHYCMvgV\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u25a0AI\u6642\u4ee3\u306e\u7b2c\u4e00\u6b69\uff01\u300cAI\u99c6\u52d5\u958b\u767a\u30b3\u30fc\u30b9\u300d\u306f\u3058\u3081\u307e\u3057\u305f\uff01<\/p>\n\n\n\n<p>\u30c6\u30c3\u30af\u30b8\u30e0\u6771\u4eac\u672c\u6821\u3067\u5148\u884c\u958b\u59cb\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"y48gQSSgCT\"><a href=\"https:\/\/techgym.jp\/about\/ai-driven-development\/\">AI\u99c6\u52d5\u958b\u767a\/\u751f\u6210AI\u30a8\u30f3\u30b8\u30cb\u30a2\u30b3\u30fc\u30b9\uff08\u521d\u5fc3\u8005\u5411\u3051\uff09<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;AI\u99c6\u52d5\u958b\u767a\/\u751f\u6210AI\u30a8\u30f3\u30b8\u30cb\u30a2\u30b3\u30fc\u30b9\uff08\u521d\u5fc3\u8005\u5411\u3051\uff09&#8221; &#8212; \u3010\u30c6\u30c3\u30af\u30b8\u30e0\u3011\u683c\u5b89\u30fb\u5bfe\u9762\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\" src=\"https:\/\/techgym.jp\/about\/ai-driven-development\/embed\/#?secret=LLiDdLlOvT#?secret=y48gQSSgCT\" data-secret=\"y48gQSSgCT\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u25a0\u30c6\u30c3\u30af\u30b8\u30e0\u6771\u4eac\u672c\u6821<\/p>\n\n\n\n<p>\u300c\u6b66\u7530\u587e\u300d\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u7248\u3068\u3044\u3048\u3070\u300c\u30c6\u30c3\u30af\u30b8\u30e0\u300d\u3002<br>\u8b1b\u7fa9\u52d5\u753b\u306a\u3057\u3001\u6559\u79d1\u66f8\u306a\u3057\u3002\u300c\u9032\u6357\u7ba1\u7406\u3068\u30b3\u30fc\u30c1\u30f3\u30b0\u300d\u3067\u52b9\u7387\u5b66\u7fd2\u3002<br>\u3088\u308a\u65e9\u304f\u3001\u3088\u308a\u5b89\u304f\u3001\u3057\u304b\u3082\u5bfe\u9762\u578b\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"2eNgSMQxGG\"><a href=\"https:\/\/techgym.jp\/tokyo\/tokyo_honko\/\">\u30c6\u30c3\u30af\u30b8\u30e0\u6771\u4eac\u672c\u6821<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;\u30c6\u30c3\u30af\u30b8\u30e0\u6771\u4eac\u672c\u6821&#8221; &#8212; \u3010\u30c6\u30c3\u30af\u30b8\u30e0\u3011\u683c\u5b89\u30fb\u5bfe\u9762\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\" src=\"https:\/\/techgym.jp\/tokyo\/tokyo_honko\/embed\/#?secret=vOFGYbriOA#?secret=2eNgSMQxGG\" data-secret=\"2eNgSMQxGG\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\uff1c\u77ed\u671f\u8b1b\u7fd2\uff1e5\u65e5\u30675\u4e07\u5186\u306e\u300cPython\u30df\u30cb\u30ad\u30e3\u30f3\u30d7\u300d\u958b\u50ac\u4e2d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"BpL1KOSeuo\"><a href=\"https:\/\/techgym.jp\/event\/nagatacho_camp\/\">\u3010\u6700\u901f\u30fb\u78ba\u5b9f\u3011\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u653b\u7565\u300c\u6c38\u7530\u753aPython\u30df\u30cb\u30ad\u30e3\u30f3\u30d7\u300d\u30105\u65e5\u9593\u30675\u4e07\u5186\u3011<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;\u3010\u6700\u901f\u30fb\u78ba\u5b9f\u3011\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u653b\u7565\u300c\u6c38\u7530\u753aPython\u30df\u30cb\u30ad\u30e3\u30f3\u30d7\u300d\u30105\u65e5\u9593\u30675\u4e07\u5186\u3011&#8221; &#8212; \u3010\u30c6\u30c3\u30af\u30b8\u30e0\u3011\u683c\u5b89\u30fb\u5bfe\u9762\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\" src=\"https:\/\/techgym.jp\/event\/nagatacho_camp\/embed\/#?secret=lOHZBFU1Qc#?secret=BpL1KOSeuo\" data-secret=\"BpL1KOSeuo\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\uff1c\u67081\u958b\u50ac\uff1e\u653e\u9001\u4f5c\u5bb6\u306b\u3088\u308b\u6620\u50cf\u30c7\u30a3\u30ec\u30af\u30bf\u30fc\u990a\u6210\u8b1b\u5ea7<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"FlhrykFcbW\"><a href=\"https:\/\/techgym.jp\/event\/video_director\/\">\u73fe\u5f79\u653e\u9001\u4f5c\u5bb6\u304c\u6559\u3048\u308b\u52d5\u753b\u8b1b\u5ea7\uff01\u300e\uff24\uff2f\uff27\uff21\u300f<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;\u73fe\u5f79\u653e\u9001\u4f5c\u5bb6\u304c\u6559\u3048\u308b\u52d5\u753b\u8b1b\u5ea7\uff01\u300e\uff24\uff2f\uff27\uff21\u300f&#8221; &#8212; \u3010\u30c6\u30c3\u30af\u30b8\u30e0\u3011\u683c\u5b89\u30fb\u5bfe\u9762\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\" src=\"https:\/\/techgym.jp\/event\/video_director\/embed\/#?secret=Ub9qbRRt1w#?secret=FlhrykFcbW\" data-secret=\"FlhrykFcbW\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\uff1c\u30aa\u30f3\u30e9\u30a4\u30f3\u7121\u6599\uff1e\u30bc\u30ed\u304b\u3089\u59cb\u3081\u308bPython\u7206\u901f\u8b1b\u5ea7<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"h5e1g8VUFM\"><a href=\"https:\/\/techgym.jp\/tokyo_python\/\">\u3010\u7121\u6599\u30fb\u30aa\u30f3\u30e9\u30a4\u30f3\u3011\u30bc\u30ed\u304b\u3089\u306f\u3058\u3081\u308bPython\u7206\u901f\u8b1b\u5ea7<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;\u3010\u7121\u6599\u30fb\u30aa\u30f3\u30e9\u30a4\u30f3\u3011\u30bc\u30ed\u304b\u3089\u306f\u3058\u3081\u308bPython\u7206\u901f\u8b1b\u5ea7&#8221; &#8212; \u3010\u30c6\u30c3\u30af\u30b8\u30e0\u3011\u683c\u5b89\u30fb\u5bfe\u9762\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\" src=\"https:\/\/techgym.jp\/tokyo_python\/embed\/#?secret=zb6FyEJAkc#?secret=h5e1g8VUFM\" data-secret=\"h5e1g8VUFM\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3067\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u3092\u6271\u3046\u65b9\u6cd5\u3092\u3001\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306edatetime\u30e2\u30b8\u30e5\u30fc\u30eb\u3068pytz\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u3066\u8a73\u3057\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u8a2d\u5b9a\u3001\u53d6\u5f97\u3001\u5909\u63db\u3001\u524a\u9664\u306e\u5168\u64cd\u4f5c\u3092\u5b66\u3073\u307e\u3057\u3087\u3046\u3002 \u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u57fa\u672c\u6982\u5ff5 fr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":42501,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-44172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-column"],"views":49,"jetpack_featured_media_url":"\/wp-content\/uploads\/2025\/07\/f3403acf5c65aedec0dba821c4c26404.png","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/posts\/44172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/comments?post=44172"}],"version-history":[{"count":0,"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/posts\/44172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/media\/42501"}],"wp:attachment":[{"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/media?parent=44172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/categories?post=44172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techgym.jp\/wp-json\/wp\/v2\/tags?post=44172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}