从字符串日期中提取年份和儒略日 [英] Extract day of year and Julian day from a string date

查看:36
本文介绍了从字符串日期中提取年份和儒略日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中有一个字符串 "2012.11.07".我需要将它转换为日期对象,然后获取年中的第几天儒略日的整数值.可能吗?

解决方案

首先,您可以将其转换为 datetime.datetime 这样的对象:

<预><代码>>>>导入日期时间>>>fmt = '%Y.%m.%d'>>>s = '2012.11.07'>>>dt = datetime.datetime.strptime(s, fmt)>>>dtdatetime.datetime(2012, 11, 7, 0, 0)

然后你就可以使用datetime上的方法得到你想要的……除了datetime没有你直接想要的函数,所以你需要转换为时间元组

<预><代码>>>>tt = dt.timetuple()>>>tt.tm_yday312

儒略日"一词有几种不同的含义.如果您正在寻找 2012312,则必须间接执行此操作,例如,以下之一.

<预><代码>>>>int('%d%03d' % (tt.tm_year, tt.tm_yday))2012312>>>tt.tm_year * 1000 + tt.tm_yday2012312

如果您正在寻找不同的含义,您应该可以从这里找出来.例如,如果您想要自公元前 4713 年 1 月 1 日以来的天数"的含义,并且您有一个需要公历年和年中日的公式,则您可以插入上面的这两个值.(如果您有一个公式需要公历年、月和日,您甚至不需要 timetuple 步骤.)如果您不知道从那里去哪里,请询问更多详细信息.

如果你没有公式——即使你已经有了——你最好的办法可能是在 PyPI 和 ActiveState 周围寻找预先存在的模块.例如,快速搜索会找到名为 jdcal 的内容.我以前从未见过它,但是快速 pip install jdcal 和自述文件的简短浏览,我能够做到这一点:

<预><代码>>>>总和(jdcal.gcal2jd(dt.year,dt.month,dt.day))2456238.5

这与 USN 儒略日期转换器给我的结果相同.

如果您想要整数儒略日,而不是小数儒略日期,则必须决定要向哪个方向舍入 - 向 0、向负无穷大、将中午四舍五入到第二天、将中午向偶数四舍五入等.(请注意,儒略日期被定义为从公元前 4713 年 1 月 1 日中午开始,所以 2012 年 11 月 7 日的一半是 2456238,另一半是 2456239,只有你知道你想要哪一个......)例如,向 0 取整:

<预><代码>>>>int(sum(jdcal.gcal2jd(dt.year, dt.month, dt.day)))2456238

I have a string "2012.11.07" in python. I need to convert it to date object and then get an integer value of day of year and also Julian day. Is it possible?

解决方案

First, you can convert it to a datetime.datetime object like this:

>>> import datetime
>>> fmt = '%Y.%m.%d'
>>> s = '2012.11.07'
>>> dt = datetime.datetime.strptime(s, fmt)
>>> dt
datetime.datetime(2012, 11, 7, 0, 0)

Then you can use the methods on datetime to get what you want… except that datetime doesn't have the function you want directly, so you need to convert to a time tuple

>>> tt = dt.timetuple()
>>> tt.tm_yday
312

The term "Julian day" has a few different meanings. If you're looking for 2012312, you have to do that indirectly, e.g., one of the following.

>>> int('%d%03d' % (tt.tm_year, tt.tm_yday))
2012312
>>> tt.tm_year * 1000 + tt.tm_yday
2012312

If you're looking for a different meaning, you should be able to figure it out from here. For example, if you want the "days since 1 Jan 4713 BC" meaning, and you have a formula that requires Gregorian year and day in year, you've got those two values above to plug in. (If you have a formula that takes Gregorian year, month, and day, you don't even need the timetuple step.) If you can't work out where to go from there, ask for further details.

If you don't have a formula—and maybe even if you already do—your best bet is probably to look around PyPI and ActiveState for pre-existing modules. For example, a quick search turned up something called jdcal. I'd never seen it before, but a quick pip install jdcal and a brief skim of the readme, and I was able to do this:

>>> sum(jdcal.gcal2jd(dt.year, dt.month, dt.day))
2456238.5

That's the same result that the USN Julian date converter gave me.

If you want integral Julian day, instead of fractional Julian date, you have to decide which direction you want to round—toward 0, toward negative infinity, rounding noon up to the next day, rounding noon toward even days, etc. (Note that Julian date is defined as starting since noon on 1 Jan 4713BC, so half of 7 Nov 2012 is 2456238, the other half is 2456239, and only you know which one of those you want…) For example, to round toward 0:

>>> int(sum(jdcal.gcal2jd(dt.year, dt.month, dt.day)))
2456238

这篇关于从字符串日期中提取年份和儒略日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆