日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python输出一年有多少天多少时分秒_python:计算在1901年1月1日至2000年12月31日间共有多少个星期天落在每月的第一天上...

發布時間:2024/3/24 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python输出一年有多少天多少时分秒_python:计算在1901年1月1日至2000年12月31日间共有多少个星期天落在每月的第一天上... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

# -*- coding: UTF-8 -*-

""" Created on 2017/4/2 @author: cat """

import logging

""" 根據下列信息計算在1901年1月1日至2000年12月31日間共有多少個星期天落在每月的第一天上? a) 1900.1.1是星期一 b) 1月,3月,5月,7月,8月,10月和12月是31天 c) 4月,6月,9月和11月是30天 d) 2月是28天,在閏年是29天 e) 公元年數能被4整除且又不能被100整除是閏年 f) 能直接被400整除也是閏年 輸出格式: 一個正整數 """

logging.basicConfig(level=logging.DEBUG,

format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

datefmt='%a, %d %b %Y %H:%M:%S')

def is_leap(year):

""" 是否是閏年 :param year: :return: 閏年-> true ; 平年-> false """

return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

def year_total_day(year):

return 366 if is_leap(year) else 365

def month_total_day(year, month):

""" 一個月有多少天 :return: 當月的天數 """

if month < 1 or month > 12:

message = "month only range in [1,12] ! but current month is {0} ".format(month)

raise ValueError(message)

if month in [1, 3, 5, 7, 8, 10, 12]:

total = 31

elif month in [4, 6, 9, 11]:

total = 30

else:

if is_leap(year):

total = 29

else:

total = 28

# logging.warn("total === {0}".format(total))

return total

# print month_total_day(2003, 2)

# print year_total_day(0)

def d_value(srcyear, srcmonth, srcday, destyear, destmonth, destday):

""" temp函數,用于計算兩個時間直接相差的天數 比如2016.3.25日 減去 2008.8.8日 等于多少天 :return: 相差的天數 """

if srcyear > destyear or (srcyear == destyear and srcmonth > destmonth) or (

srcyear == destyear and srcmonth > destmonth and srcday > destday):

message = "起始時間不得大于結束時間,這樣子是無法比較的:\n" \

"起始時間:{0}/{1}/{2} 結束時間:{3}/{4}/{5}" \

.format(srcyear, srcmonth, srcday, destyear, destmonth, destday)

raise ValueError(message)

else:

# 都回到公元元年吧 1900.1.1 --> 0年的0月0日吧

st = 0

for y in range(0, srcyear):

st += year_total_day(y)

# logging.info("st1 = {0} , y = {1}".format(st,y))

for m in range(1, srcmonth):

st += month_total_day(srcyear, m)

# logging.info("st2 = {0}".format(st))

st += srcday

# logging.info("st = {0}".format(st))

dt = 0

for y in range(0, destyear):

dt += year_total_day(y)

# logging.info("yy = "+str(yy)+ "destday = "+str(destyear))

for m in range(1, destmonth):

dt += month_total_day(srcyear, m)

dt += destday

# logging.info("dt = {0}".format(dt))

return dt - st

def weekday(year, month, day):

""" 獲取每天是星期幾(基于1900.1.1 是周一) :param year: 2017 :param month: 04 :param day: 02 :return: 1 - 7 ==》周一 - 周日 """

ds = d_value(1900, 1, 1, year, month, day) + 1

weekday_num = ds % 7

return 7 if weekday_num == 0 else weekday_num

# print weekday(2017, 4, 2)

def get_all_lucky():

ret = []

cnt = 0

for year in range(1900, 2000 + 1):

for month in range(1, 12 + 1):

weekday_num = weekday(year, month, 1)

if weekday_num == 1:

cnt += 1

item = (year, month, 1)

ret.append(item)

print "size = {0}".format(cnt)

return ret

p = 0

for it in get_all_lucky():

p += 1

if p % 5 == 0:

print

print it,

總結

以上是生活随笔為你收集整理的python输出一年有多少天多少时分秒_python:计算在1901年1月1日至2000年12月31日间共有多少个星期天落在每月的第一天上...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。