前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >根据天数计算具体日期

根据天数计算具体日期

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-22 10:51:04
1.9K0
发布2019-01-22 10:51:04
举报
文章被收录于专栏:给永远比拿愉快

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

代码语言:javascript
复制
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]))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年09月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档