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】

Last updated at Posted at 2025-05-10

労働基準法 第三十四条(休憩)

使用者は、労働時間が六時間を超える場合においては少くとも四十五分、八時間を超える場合においては少くとも一時間の休憩時間を労働時間の途中に与えなければならない。

説明

労働基準法第34条の休憩時間を計算する

労働時間 休憩時間
6:00 0分
6:01 45分
8:00 45分
8:01 60分
14:00 60分
14:01 105分
16:00 105分
16:01 120分

休憩時間を繰り返し計算し取得するパターン

from datetime import datetime, timedelta
from operator import itemgetter

# 休憩時間データ(降順ソート)
rest_schedule = sorted([
    {"work_minutes": 360, "rest_minutes": 45},
    {"work_minutes": 480, "rest_minutes": 60},
], key=itemgetter("work_minutes"), reverse=True)

# 分を h:mm (時間:分) 形式に変換
def convert_to_h_mm(minutes: int) -> str:
    hours = minutes // 60
    mins = minutes % 60
    return f"{hours}:{mins:02d}"

# 労働時間に応じた休憩時間を計算
def calculate_rest_time(work_minutes: int) -> int:
    total_rest = 0
    remaining_minutes = work_minutes

    while remaining_minutes > 0:
        for condition in rest_schedule:
            if remaining_minutes > condition["work_minutes"]:
                total_rest += condition["rest_minutes"]
                remaining_minutes -= condition["work_minutes"]
                break
        else:
            break

    return total_rest

# テストケース
test_cases = [360, 361, 480, 481, 840, 841, 960, 961]

for minutes in test_cases:
    rest_time = calculate_rest_time(minutes)
    print(f"労働時間 {convert_to_h_mm(minutes)} ({minutes} 分) → 休憩時間 {rest_time}")

# 労働時間 6:00 (360 分) → 休憩時間 0 分
# 労働時間 6:01 (361 分) → 休憩時間 45 分
# 労働時間 8:00 (480 分) → 休憩時間 45 分
# 労働時間 8:01 (481 分) → 休憩時間 60 分
# 労働時間 14:00 (840 分) → 休憩時間 60 分
# 労働時間 14:01 (841 分) → 休憩時間 105 分
# 労働時間 16:00 (960 分) → 休憩時間 105 分
# 労働時間 16:01 (961 分) → 休憩時間 120 分

休憩時間をすべて定義し1回で取得するパターン

# 休憩時間データ
rest_schedule = {
    361: 45,
    481: 60,
    841: 105,
    961: 120,
    1321: 165,
    1441: 180
}

# 分を h:mm (時間:分) 形式に変換
def convert_to_h_mm(minutes: int) -> str:
    hours = minutes // 60
    mins = minutes % 60
    return f"{hours}:{mins:02d}"

# 労働時間に応じた休憩時間を取得
def get_rest_time(work_minutes: int) -> int:
    for limit in sorted(rest_schedule.keys(), reverse=True): # 降順ソート
        if work_minutes >= limit:
            return rest_schedule[limit]
    return 0  # 該当なしの場合は0分

# テストケース
test_cases = [360, 361, 480, 481, 840, 841, 960, 961, 1320, 1321, 1440, 1441]

for minutes in test_cases:
    rest_time = get_rest_time(minutes)
    print(f"労働時間 {convert_to_h_mm(minutes)} ({minutes} 分) → 休憩時間 {rest_time}")

# 労働時間 6:00 (360 分) → 休憩時間 0 分
# 労働時間 6:01 (361 分) → 休憩時間 45 分
# 労働時間 8:00 (480 分) → 休憩時間 45 分
# 労働時間 8:01 (481 分) → 休憩時間 60 分
# 労働時間 14:00 (840 分) → 休憩時間 60 分
# 労働時間 14:01 (841 分) → 休憩時間 105 分
# 労働時間 16:00 (960 分) → 休憩時間 105 分
# 労働時間 16:01 (961 分) → 休憩時間 120 分
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?