间歇性断食计算器
使用16:8、18:6、5:2等方法规划断食时间表。
8h
进食窗口
开始进食 12:00 PM
16h
断食
断食开始 8:00 PM
12:00 PM
开始进食
8:00 PM
停止进食
小贴士
•断食期间可饮用水、黑咖啡或清茶。
•用均衡营养的饮食来打破断食。
•坚持规律最重要——每天相同的时间表效果最佳。
常见问题
代码实现
from datetime import datetime, timedelta
def calculate_fasting_window(start_time_str: str, fasting_hours: int) -> dict:
"""
Calculate fasting and eating windows for intermittent fasting.
start_time_str: when the fast started, e.g. "2025-03-20 20:00"
fasting_hours: duration of fast in hours (e.g. 16 for 16:8)
"""
fmt = "%Y-%m-%d %H:%M"
fast_start = datetime.strptime(start_time_str, fmt)
fast_end = fast_start + timedelta(hours=fasting_hours)
eating_hours = 24 - fasting_hours
eating_end = fast_end + timedelta(hours=eating_hours)
now = datetime.now()
elapsed = (now - fast_start).total_seconds() / 3600
remaining = max(0, fasting_hours - elapsed)
return {
"fast_start": fast_start.strftime(fmt),
"fast_end": fast_end.strftime(fmt),
"eating_window_end": eating_end.strftime(fmt),
"hours_elapsed": round(elapsed, 2),
"hours_remaining": round(remaining, 2),
"protocol": f"{fasting_hours}:{eating_hours}",
}
result = calculate_fasting_window("2025-03-20 20:00", 16)
print(f"Protocol : {result['protocol']}")
print(f"Fast Start : {result['fast_start']}")
print(f"Fast End : {result['fast_end']}")
print(f"Eating Window End : {result['eating_window_end']}")
print(f"Hours Elapsed : {result['hours_elapsed']}")
print(f"Hours Remaining : {result['hours_remaining']}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.