0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python_datetimeライブラリで使用頻度が高い標準メソッド10選

Last updated at Posted at 2024-12-28

Python_datetimeライブラリで使用頻度が高い標準メソッド10選

  • 用途: 日付や時間の操作、計算、フォーマット。
  • 例: datetime.now, datetime.timedelta, datetime.strptime
from datetime import datetime, timedelta, timezone

使用率が高いメソッド10選

  1. datetime.now(): 現在の日付と時刻を取得します。

    now = datetime.now()
    print(now)
    
  2. datetime.today(): 現在の日付と時刻を取得します(now()とほぼ同じ)。

    today = datetime.today()
    print(today)
    
  3. datetime.strptime(date_string, format): 文字列を指定したフォーマットでdatetimeオブジェクトに変換します。

    date = datetime.strptime("2023-12-28", "%Y-%m-%d")
    print(date)
    
  4. datetime.strftime(format): datetimeオブジェクトを指定したフォーマットの文字列に変換します。

    now = datetime.now()
    formatted_date = now.strftime("%Y/%m/%d %H:%M:%S")
    print(formatted_date)
    
  5. datetime.date(): datetimeオブジェクトから日付部分を取得します。

    now = datetime.now()
    print(now.date())
    
  6. datetime.time(): datetimeオブジェクトから時刻部分を取得します。

    now = datetime.now()
    print(now.time())
    
  7. timedelta(days=0, seconds=0, ...): 時間差を表すオブジェクトを作成します。

    delta = timedelta(days=7)
    print(datetime.now() + delta)
    
  8. datetime.timestamp(): datetimeオブジェクトをタイムスタンプ(エポック秒)に変換します。

    now = datetime.now()
    print(now.timestamp())
    
  9. datetime.utcfromtimestamp(timestamp): タイムスタンプからUTC日時を生成します。

    utc_time = datetime.utcfromtimestamp(1672531200)
    print(utc_time)
    
  10. datetime.replace(**kwargs): 指定した値でdatetimeオブジェクトを置き換えます。

    now = datetime.now()
    new_time = now.replace(hour=20, minute=0)
    print(new_time)
    

    ↓datetimeのフォーマット各種はこちらから↓

🔳用語説明

エポック秒とは

エポック秒(Epoch Time)は、コンピュータで日時を扱う際によく使われる方法の一つで、「1970年1月1日00時00分00秒(UTC)」 を基準とし、その時刻からの経過秒数を整数値で表したものです。この基準点は「UNIXエポック」と呼ばれています。


エポック秒の特徴

  • 基準時刻: 1970年1月1日00:00:00 UTC(協定世界時)。
  • 整数表現: 小数部を持たず、時刻を「経過秒数」としてシンプルに扱える。
  • タイムゾーン: エポック秒はUTCを基準としており、タイムゾーンの違いを補正しないとローカル時刻との差異が発生します。

エポック秒の例

以下に具体的な例を挙げます。

エポック秒 対応する日時 (UTC)
0 1970-01-01 00:00:00
86400 1970-01-02 00:00:00
1672531200 2023-01-01 00:00:00

エポック秒をPythonで扱う方法

エポック秒を日時に変換するには、Pythonのdatetimeモジュールを使用します。

例: エポック秒を日時に変換(UTC)

from datetime import datetime, timezone

# エポック秒
timestamp = 1672531200

# UTCで日時を取得
utc_time = datetime.fromtimestamp(timestamp, tz=timezone.utc)
print(utc_time)

出力:

2023-01-01 00:00:00+00:00

例: エポック秒をローカル時刻に変換

from datetime import datetime

# エポック秒
timestamp = 1672531200

# ローカルタイムで日時を取得
local_time = datetime.fromtimestamp(timestamp)
print(local_time)

出力例(日本時間の場合):

2023-01-01 09:00:00

注意点

  1. エポック秒はタイムゾーン情報を含まないため、ローカル時間を正確に計算するにはタイムゾーンの考慮が必要です。
  2. UNIXエポック(1970年以前)や非常に未来の時刻では、互換性の問題が発生する場合があります。

これを意識すると、エポック秒を安全かつ効率的に活用できます!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?