Python timestamp値の取得 - 現在時刻、変換、活用法完全ガイド
Python timestamp値の取得 - 現在時刻、変換、活用法完全ガイド
🎯 核心的解決策(すぐに使用可能)
最もよく使用されるパターン
import time
# 1. 現在のtimestampを取得(小数点を含む)
timestamp = time.time()
print(timestamp)
# 出力: 1717484200.256982
# 2. 整数型timestampを取得(最もよく使用)
timestamp_int = int(time.time())
print(timestamp_int)
# 出力: 1717484200
# 3. ミリ秒単位timestamp(JavaScript互換)
timestamp_ms = int(time.time() * 1000)
print(timestamp_ms)
# 出力: 1717484200256
実務でよく使用される変換
from datetime import datetime
# 4. datetimeをtimestampに変換
dt = datetime.now()
timestamp_from_dt = int(dt.timestamp())
# 5. timestampをdatetimeに変換
dt_from_timestamp = datetime.fromtimestamp(timestamp_int)
print(dt_from_timestamp)
# 出力: 2025-06-04 14:30:00
# 6. 特定日付のtimestampを取得
specific_date = datetime(2025, 12, 25, 0, 0, 0)
christmas_timestamp = int(specific_date.timestamp())
データベース保存用フォーマット
# DynamoDB、MongoDBなどで使用
db_timestamp = {
'created_at': int(time.time()),
'updated_at': int(time.time())
}
# MySQL、PostgreSQLなどで使用
sql_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
📚 詳細説明
背景と必要性
Timestampは特定の時点を数値で表現する方法で、1970年1月1日00:00:00 UTCから経過した秒数を表します。開発においてtimestampが必要な主要な状況:
- データベース保存: 作成・修正時間の記録
- API通信: 時間データの交換
- ログ管理: イベント発生時間の追跡
- キャッシュ期限: TTL(Time To Live)設定
- パフォーマンス測定: 実行時間の計算
様々なtimestamp生成方法
1. timeモジュールの使用
import time
# 現在時刻のtimestamp(float)
current_time = time.time()
print(f"Float timestamp: {current_time}")
# 整数型に変換(秒単位)
int_timestamp = int(current_time)
print(f"Integer timestamp: {int_timestamp}")
# ミリ秒単位(JavaScriptと互換)
ms_timestamp = int(current_time * 1000)
print(f"Millisecond timestamp: {ms_timestamp}")
2. datetimeモジュールの使用
from datetime import datetime, timezone
# 現在時刻のtimestamp
now = datetime.now()
timestamp = int(now.timestamp())
# UTC時間に変換
utc_now = datetime.now(timezone.utc)
utc_timestamp = int(utc_now.timestamp())
# 特定日付のtimestamp
specific_date = datetime(2025, 1, 1, 0, 0, 0)
new_year_timestamp = int(specific_date.timestamp())
実際の活用事例
1. DynamoDB TTL設定
import time
def create_session_data(user_id, session_data, expire_hours=24):
"""セッションデータをDynamoDBに保存(TTL設定)"""
ttl = int(time.time()) + (expire_hours * 3600) # 24時間後に期限切れ
item = {
'user_id': user_id,
'session_data': session_data,
'created_at': int(time.time()),
'ttl': ttl # DynamoDBが自動的に削除
}
return item
# 使用例
session = create_session_data('user123', {'login': True})
print(session)
2. APIログ時間記録
import time
import json
def log_api_request(endpoint, method, response_time):
"""APIリクエストログの記録"""
log_entry = {
'timestamp': int(time.time()),
'endpoint': endpoint,
'method': method,
'response_time_ms': response_time,
'date_readable': datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
}
# ログファイルに保存
with open('api_logs.json', 'a') as f:
f.write(json.dumps(log_entry) + '\n')
# 使用例
log_api_request('/api/users', 'GET', 150)
3. パフォーマンス測定
import time
def measure_execution_time(func):
"""関数実行時間測定デコレータ"""
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
print(f"{func.__name__} 実行時間: {execution_time:.4f}秒")
return result
return wrapper
@measure_execution_time
def slow_function():
time.sleep(2) # 2秒待機
return "完了"
# 使用例
slow_function()
# 出力: slow_function 実行時間: 2.0021秒
タイムゾーン処理と変換
from datetime import datetime, timezone
import pytz
# UTCタイムスタンプ生成
utc_now = datetime.now(timezone.utc)
utc_timestamp = int(utc_now.timestamp())
# 日本時間に変換
jst = pytz.timezone('Asia/Tokyo')
jst_time = datetime.fromtimestamp(utc_timestamp, tz=jst)
# 様々なタイムゾーンのtimestamp
timezones = {
'UTC': timezone.utc,
'JST': pytz.timezone('Asia/Tokyo'),
'PST': pytz.timezone('US/Pacific'),
'EST': pytz.timezone('US/Eastern')
}
for tz_name, tz in timezones.items():
local_time = datetime.now(tz)
timestamp = int(local_time.timestamp())
print(f"{tz_name}: {timestamp} ({local_time.strftime('%Y-%m-%d %H:%M:%S %Z')})")
エラー処理と例外状況
from datetime import datetime
def safe_timestamp_conversion(date_string, format_string='%Y-%m-%d %H:%M:%S'):
"""安全な日付文字列からtimestampへの変換"""
try:
dt = datetime.strptime(date_string, format_string)
return int(dt.timestamp())
except ValueError as e:
print(f"日付フォーマットエラー: {e}")
return None
except Exception as e:
print(f"予期しないエラー: {e}")
return None
# 使用例
valid_date = "2025-06-04 14:30:00"
invalid_date = "2025-13-40 25:70:70"
print(safe_timestamp_conversion(valid_date)) # 正常変換
print(safe_timestamp_conversion(invalid_date)) # None返却
有用なヘルパー関数
import time
from datetime import datetime
class TimestampHelper:
"""Timestamp関連ユーティリティクラス"""
@staticmethod
def now():
"""現在のtimestamp(整数)"""
return int(time.time())
@staticmethod
def now_ms():
"""現在のtimestamp(ミリ秒)"""
return int(time.time() * 1000)
@staticmethod
def to_readable(timestamp):
"""timestampを読みやすい形式に変換"""
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
@staticmethod
def days_ago(days):
"""N日前のtimestamp"""
return int(time.time() - (days * 24 * 3600))
@staticmethod
def days_later(days):
"""N日後のtimestamp"""
return int(time.time() + (days * 24 * 3600))
# 使用例
helper = TimestampHelper()
print(f"現在: {helper.now()}")
print(f"現在 (ms): {helper.now_ms()}")
print(f"読みやすい形式: {helper.to_readable(helper.now())}")
print(f"7日前: {helper.days_ago(7)}")
print(f"30日後: {helper.days_later(30)}")
結論
Pythonでtimestampを扱うことは開発の基本ですが、実際のプロジェクトではタイムゾーン処理、データベース保存、API通信など様々な状況を考慮する必要があります。この記事で提供したコード例を参考に、皆さんのプロジェクトに適した方式を選択してください。
重要なポイント:
- ほとんどの場合
int(time.time())
で十分 - データベース保存時は整数型timestamp推奨
- タイムゾーンが重要な場合はUTC基準で保存
- エラー処理による安全な変換の実装
次のステップ:
- DjangoやFlaskでのtimestamp活用
- 時系列データベースとtimestamp
- マイクロサービス間時間同期方法