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

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

问题描述

我在python中有一个字符串"2012.11.07".我需要将其转换为日期对象,然后获取整数值每年的天以及朱利安日.有可能吗?

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?

推荐答案

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

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)

然后,您可以使用datetime上的方法来获取所需的内容...除了datetime没有直接所需的功能之外,因此您需要转换为

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

朱利安日"一词有一些不同的含义.如果您要查找2012312,则必须间接执行此操作,例如,执行以下操作之一.

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

如果您要查找其他含义,则应该可以从这里弄清楚.例如,如果您想要自4713年1月1日起的天数"的含义,并且您有一个要求使用公历年份和年份的公式,那么您需要在上面插入这两个值.(如果您有一个公式需要公历的年,月和日,甚至不需要timetuple步骤.)如果您无法确定从那里去哪里,请询问更多详细信息.

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.

如果您没有公式,甚至可能已经有了公式,您最好的选择就是在PyPI和ActiveState周围查找预先存在的模块.例如,快速搜索打开了名为 jdcal 的内容.我以前从未看过它,但是快速阅读了pip install jdcal和简短的自述文件,就可以做到这一点:

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

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

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

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

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天全站免登陆