简体   繁体   English

从纪元到相对日期的秒数

[英]Seconds since epoch to relative date

I'm working with dates since epoch, and already got, for example: 我从epoch开始使用日期,并且已经有了,例如:

date = 6928727.56235

I'd like to transform this into another relative format, so that I'll be able to transform this into something relative to epoch. 我想将其转换为另一种相对格式,以便我能够将其转换为相对于纪元的内容。

Using time.gmtime(date), it returned 使用time.gmtime(date),它返回

year=1970, mon=3, day=22, hour=4, min=38, sec=47

I think epoch starts in '01/01/1970 00:00:00', so the method should return the relative date in something like: 我认为epoch开始于'01 / 01/1970 00:00:00',所以该方法应该返回相对日期,例如:

'2 months 21 days 04:38:47'

Anything that help? 有什么帮助吗?

The method should return the relative date in something like: '2 months 22 days 04:38:47' 该方法应该返回相对日期,例如:'2个月22天04:38:47'

You can't do that, since a month is between 28 and 31 days long. 你不能这样做,因为一个月是28到31天。 The statement "2 months and 22 days" could mean anything between 81 and 84 days. 声明“2个月和22天”可能意味着81到84天之间的任何事情。 (Or between 78 and 84 days, if the months doesn't have to be consecutive). (或者在78到84天之间,如果月份不必连续)。

So what you want is simply nonsensical. 所以你想要的只是荒谬的。 A relative date time can only be counted in days, hours and seconds, until the difference becomes so big that the amount of days no longer matters, in which case you can start counting in months or years (but then you can't include days anymore). 相对日期时间只能以天,小时和秒计算,直到差异变得太大以至于天数不再重要,在这种情况下,您可以开始计算数月或数年(但之后您不能包括天数)了)。

So you can say "five years and two months", or "80 days and three hours", or "two hundred years". 所以你可以说“五年零两个月”,或“80天三小时”,或“两百年”。 But you can not say "two months and three days" or "five years and 20 days". 但你不能说“两个月零三天”或“五年零二十天”。 The statements simply make no sense. 这些陈述毫无意义。

Therefore, the correct answer is indeed eumiros 因此,正确的答案确实是eumiros

timedelta(seconds=6928727.56235)

But now you also know why. 但现在你也知道为什么了。

(Unless of course, you with month actually mean moon-cycles, which do have a fixed length. :)) (当然,除非你用月份实际意味着月亮周期,它确实具有固定的长度。:))

from datetime import timedelta

a = timedelta(seconds=6928727.56235)

# a is now datetime.timedelta(80, 16727, 562350)

print "%d days %02d:%02d:%02d" % (a.days, a.seconds / 3600, (a.seconds / 60) % 60, a.seconds % 60)

Returns 80 days 04:38:47 , which is correct, but not exactly what OP wanted (80 days instead of 2 months 21 days). 返回80 days 04:38:47 ,这是正确的,但不完全是OP想要的(80天而不是2个月21天)。

time.gmtime returns a ParseTuple object and you can use the individual elements of the tuple do your calculation. time.gmtime返回一个ParseTuple对象,您可以使用元组的各个元素进行计算。 Something like this 像这样的东西

>>> time_epoch = time.gmtime(0)
>>> time_at_hand = time.gmtime(6928727.56235)
>>> print "It's been %d days somewhat like  %d months, %d days and %d hours, %d  minutes, %d seconds " % (time_at_hand.tm_yday - time_epoch.tm_yday, time_at_hand.tm_mon - time_epoch.tm_mon , time_at_hand.tm_mday - time_epoch.tm_mday, time_at_hand.tm_hour - time_epoch.tm_hour, time_at_hand.tm_min - time_epoch.tm_min, time_at_hand.tm_sec - time_epoch.tm_sec)
It's been 80 days somewhat like  2 months, 21 days and 4 hours, 38  minutes, 47 seconds 

Similar to Eumiro's Answer, but no need to divide by 60 etc - instead, use Pandas Timestamp and take the difference from beginning of epoch. 类似于Eumiro的答案,但不需要除以60等 - 相反,使用Pandas时间戳并从时代的开始采取差异。

date = 6928727.56235
import pandas as pd
pd.Timestamp(datetime.fromtimestamp(date))-pd.Timestamp(datetime.fromtimestamp(0))

returns 回报

Timedelta('80 days 04:38:47.562350')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM