0
点赞
收藏
分享

微信扫一扫

根据天数计算具体日期


问题描述:Landsat数据的命名规范中对于日期是:年份+该年的第几天
具体参见:​​​Landsat File Naming Convention​​​
那如果通过天数计算具体的日期呢?
下面给出计算源码(Python版本):
Python3下运行通过

def is_leap_year(year):
# 判断闰年的方法是该年能被4整除且不能被100整除,或者是可以被400整除
if ((year%4 == 0) and (year%100 != 0)) or (year%400 == 0):
return True
else:
return False

def calculate_date(year, num):
days_of_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap_year(year):
days_of_year[1] = 29

month = num // 30 #可能是month月或者month+1月
days = 0
for i in range(month):
days += days_of_year[i]
if days >= num: #应该不会出现大于的情况,最多是等于的情况
days -= days_of_year[month-1]
else:
month += 1
date = num - days
return (year, month, date) #返回年月日的一个tuple


if __name__ == '__main__':
year = '2000' #四位
days = '061' #三位
print(year + days + '=', end='')
date = calculate_date(int(year), int(days))
print(str(date[0]) + '-' + str(date[1]) + '-' + str(date[2]))


举报

相关推荐

0 条评论