Open1
【Python】現在時刻など時刻関連諸々
時間関連
import subprocess
import time
from datetime import datetime, timedelta
from calendar import monthrange
# 時刻取得・操作サンプル
now = datetime.now()
print("今の時間:", now)
tomorrow = now + timedelta(days=1)
print("明日の同じ時間:", tomorrow)
one_hour_later = now + timedelta(hours=1)
print("1時間後:", one_hour_later)
thirty_minutes_later = now + timedelta(minutes=30)
print("30分後:", thirty_minutes_later)
ten_seconds_later = now + timedelta(seconds=10)
print("10秒後:", ten_seconds_later)
today_2359 = now.replace(hour=23, minute=59, second=0, microsecond=0)
print("今日の23:59:", today_2359)
today_0000 = now.replace(hour=0, minute=0, second=0, microsecond=0)
print("今日の00:00:", today_0000)
tomorrow_0000 = (now + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
print("明日の00:00:", tomorrow_0000)
last_day = monthrange(now.year, now.month)[1]
month_end = now.replace(day=last_day, hour=23, minute=59, second=59, microsecond=0)
print("今月末:", month_end)
month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
print("今月初:", month_start)
one_week_later = now + timedelta(weeks=1)
print("1週間後:", one_week_later)
one_week_ago = now - timedelta(weeks=1)
print("1週間前:", one_week_ago)
three_days_later_noon = (now + timedelta(days=3)).replace(hour=12, minute=0, second=0, microsecond=0)
print("3日後の12:00:", three_days_later_noon)
five_minutes_ago = now - timedelta(minutes=5)
print("5分前:", five_minutes_ago)
one_year_ago = now.replace(year=now.year - 1)
print("1年前:", one_year_ago)
one_year_later = now.replace(year=now.year + 1)
print("1年後:", one_year_later)
ten_years_later = now.replace(year=now.year + 10)
print("10年後:", ten_years_later)
def add_months(dt, months):
month = dt.month - 1 + months
year = dt.year + month // 12
month = month % 12 + 1
day = min(dt.day, monthrange(year, month)[1])
return dt.replace(year=year, month=month, day=day)
one_month_later = add_months(now, 1)
print("1か月後:", one_month_later)
one_day_ago = now - timedelta(days=1)
print("1日前:", one_day_ago)
one_hour_ago = now - timedelta(hours=1)
print("1時間前:", one_hour_ago)
one_minute_later = now + timedelta(minutes=1)
print("1分後:", one_minute_later)
one_second_later = now + timedelta(seconds=1)
print("1秒後:", one_second_later)
one_second_ago = now - timedelta(seconds=1)
print("1秒前:", one_second_ago)
weekdays_jp = ["月", "火", "水", "木", "金", "土", "日"]
print("今日の曜日:", weekdays_jp[now.weekday()])
print("ISOフォーマット:", now.isoformat())
print("UNIXタイムスタンプ:", now.timestamp())
def get_next_target_second(now: datetime, interval: int) -> datetime:
# 次の実行タイミング(00, 20, 40秒)
current_second = now.second
next_second = ((current_second // interval) + 1) * interval
if next_second >= 60:
# 次の分へ繰り上げ
now = now + timedelta(minutes=1)
next_second = 0
return now.replace(second=next_second, microsecond=0)
def sleep_until(target_time: datetime):
while True:
now = datetime.now()
remaining = (target_time - now).total_seconds()
if remaining <= 0:
break
time.sleep(min(remaining, 0.1))
def run_every_20_seconds():
interval = 20 # 実行間隔(秒)
while True:
now = datetime.now()
next_exec_time = get_next_target_second(now, interval)
sleep_until(next_exec_time)
result = subprocess.run("echo Hello", capture_output=True, text=True, shell=True)
print(f"[{datetime.now()}] コマンド実行: {result.stdout.strip()}")
if __name__ == "__main__":
run_every_20_seconds()
output
PS> python .\main.py
今の時間: 2025-06-17 23:43:24.664432
明日の同じ時間: 2025-06-18 23:43:24.664432
1時間後: 2025-06-18 00:43:24.664432
30分後: 2025-06-18 00:13:24.664432
10秒後: 2025-06-17 23:43:34.664432
今日の23:59: 2025-06-17 23:59:00
今日の00:00: 2025-06-17 00:00:00
明日の00:00: 2025-06-18 00:00:00
今月末: 2025-06-30 23:59:59
今月初: 2025-06-01 00:00:00
1週間後: 2025-06-24 23:43:24.664432
1週間前: 2025-06-10 23:43:24.664432
3日後の12:00: 2025-06-20 12:00:00
5分前: 2025-06-17 23:38:24.664432
1年前: 2024-06-17 23:43:24.664432
1年後: 2026-06-17 23:43:24.664432
10年後: 2035-06-17 23:43:24.664432
1か月後: 2025-07-17 23:43:24.664432
1日前: 2025-06-16 23:43:24.664432
1時間前: 2025-06-17 22:43:24.664432
1分後: 2025-06-17 23:44:24.664432
1秒後: 2025-06-17 23:43:25.664432
1秒前: 2025-06-17 23:43:23.664432
今日の曜日: 火
ISOフォーマット: 2025-06-17T23:43:24.664432
UNIXタイムスタンプ: 1750171404.664432