如何从python time.time()计算当前月份

如何从python time.time()计算当前月份,python,python-2.7,Python,Python 2.7,我正在尝试获取当前的月数,即3(今天是2016年3月4日)。所以我在跑步: from time import time Ys=12*30*24*3600 Ms=Ys/12 m=(trunc(time())%Ys)/Ms print m 结果是10 有趣的是,当我跑步时: Ds=Ms/30 d=(trunc(time())%Ms)/Ds 那么结果是正确的:4 为什么m不是3而是10?时间模块基于自纪元以来的相对时间 要获取当前日期,应使用datetime模块: 要完成: import dat

我正在尝试获取当前的月数,即3(今天是2016年3月4日)。所以我在跑步:

from time import time
Ys=12*30*24*3600
Ms=Ys/12
m=(trunc(time())%Ys)/Ms
print m
结果是10

有趣的是,当我跑步时:

Ds=Ms/30
d=(trunc(time())%Ms)/Ds
那么结果是正确的:4


为什么m不是3而是10?

时间
模块基于自纪元以来的相对时间

要获取当前日期,应使用
datetime
模块:

要完成:

import datetime
today=datetime.datetime.now()
month=today.month()

要获取当前日期,请执行以下操作:

from datetime import datetime
d = datetime.today()
当月:

print d.month
完整日期:

print d.isoformat() // yields '2016-03-04T15:08:04.145000'

我建议使用
datetime
获取人类可读的时间

例如:

from datetime import datetime
a = datetime.now()
print a.month
3
您的示例失败了,因为您有几年(其中一些闰年)的月天数不同。这在某种程度上是不准确的。

time.time()
不会返回年初以来的秒数;它返回自启动后的秒数。要使用
时间
模块获取当月的当前日期,请执行以下操作:

month = time.localtime().tm_month
month = datetime.today().month
然而,还有另一个日期库,叫做。要从中获取月份,请执行以下操作:

month = time.localtime().tm_month
month = datetime.today().month

下面介绍如何在不使用任何库函数的情况下将Unix历元时间转换为(UTC)公历日期。下面的代码仅导入
time
,以验证计算的日期是否与
time.gmtime
返回的日期相同

请注意,在2038年1月19日星期二03:14:08 UTC时,Unix时间戳的32位版本将停止工作,因为它将溢出签名32位数字中可以保存的最大值。因此,不要试图在32位系统上调用
time.gmtime
,以获取此类时间戳。当然,下面使用的算法没有这些限制

#!/usr/bin/env python

''' Convert Unix Epoch seconds to (proleptic) Gregorian date

    Algorithm by E. G. Richards, https://en.wikipedia.org/wiki/Julian_day

    See http://stackoverflow.com/q/35796786/4014959

    Python implementation by PM 2Ring 2016.03.07
'''

from time import gmtime

def epoch_seconds_to_gregorian_date(eseconds):
    # Algorithm parameters for Gregorian calendar
    y = 4716; j = 1401; m = 2; n = 12; r = 4; p = 1461 
    v = 3; u = 5; s = 153; w = 2; B = 274277; C = -38

    #Julian day, rounded
    J = int(0.5 + eseconds / 86400.0 + 2440587.5)

    f = J + j + (((4 * J + B) // 146097) * 3) // 4 + C
    e = r * f + v
    g = (e % p) // r
    h = u * g + w
    D = (h % s) // u + 1
    M = (h // s + m) % n + 1
    Y = (e // p) - y + (n + m - M) // n

    return Y, M, D

# Tests    
def test(s):
    t = gmtime(s)
    gmdate = t.tm_year, t.tm_mon, t.tm_mday
    e2gdate = epoch_seconds_to_gregorian_date(s)
    assert gmdate == e2gdate, (t, gmdate, e2gdate)
    return '%d.%d.%d' % e2gdate    

print 'hours'
for i in xrange(25):
    s = 3600 * i
    print i, test(s) 

print '\ndays'
for i in xrange(32):
    s = 86400 * i
    print i, test(s)

print '\n2 days by seconds...'
for s in xrange(86400 * 2):
    test(s)

n = 50
print '\n%d years by days...' % n
for i in xrange(365 * n):
    s = 86400 * i    
    test(s)

print 'Ok'    
输出

hours
0 1970.1.1
1 1970.1.1
2 1970.1.1
3 1970.1.1
4 1970.1.1
5 1970.1.1
6 1970.1.1
7 1970.1.1
8 1970.1.1
9 1970.1.1
10 1970.1.1
11 1970.1.1
12 1970.1.1
13 1970.1.1
14 1970.1.1
15 1970.1.1
16 1970.1.1
17 1970.1.1
18 1970.1.1
19 1970.1.1
20 1970.1.1
21 1970.1.1
22 1970.1.1
23 1970.1.1
24 1970.1.2

days
0 1970.1.1
1 1970.1.2
2 1970.1.3
3 1970.1.4
4 1970.1.5
5 1970.1.6
6 1970.1.7
7 1970.1.8
8 1970.1.9
9 1970.1.10
10 1970.1.11
11 1970.1.12
12 1970.1.13
13 1970.1.14
14 1970.1.15
15 1970.1.16
16 1970.1.17
17 1970.1.18
18 1970.1.19
19 1970.1.20
20 1970.1.21
21 1970.1.22
22 1970.1.23
23 1970.1.24
24 1970.1.25
25 1970.1.26
26 1970.1.27
27 1970.1.28
28 1970.1.29
29 1970.1.30
30 1970.1.31
31 1970.2.1

2 days by seconds...

50 years by days...
Ok
now
1457355412.48 (2016, 3, 7, 12, 56, 52.476011991500854) time.struct_time(tm_year=2016, tm_mon=3, tm_mday=7, tm_hour=12, tm_min=56, tm_sec=52, tm_wday=0, tm_yday=67, tm_isdst=0)

hours
0 1970.01.01 00:00:00.000
1 1970.01.01 01:00:00.000
2 1970.01.01 02:00:00.000
3 1970.01.01 03:00:00.000
4 1970.01.01 04:00:00.000
5 1970.01.01 05:00:00.000
6 1970.01.01 06:00:00.000
7 1970.01.01 07:00:00.000
8 1970.01.01 08:00:00.000
9 1970.01.01 09:00:00.000
10 1970.01.01 10:00:00.000
11 1970.01.01 11:00:00.000
12 1970.01.01 12:00:00.000
13 1970.01.01 13:00:00.000
14 1970.01.01 14:00:00.000
15 1970.01.01 15:00:00.000
16 1970.01.01 16:00:00.000
17 1970.01.01 17:00:00.000
18 1970.01.01 18:00:00.000
19 1970.01.01 19:00:00.000
20 1970.01.01 20:00:00.000
21 1970.01.01 21:00:00.000
22 1970.01.01 22:00:00.000
23 1970.01.01 23:00:00.000
24 1970.01.02 00:00:00.000

days
0 1970.01.01 00:00:00.000
1 1970.01.02 00:00:00.000
2 1970.01.03 00:00:00.000
3 1970.01.04 00:00:00.000
4 1970.01.05 00:00:00.000
5 1970.01.06 00:00:00.000
6 1970.01.07 00:00:00.000
7 1970.01.08 00:00:00.000
8 1970.01.09 00:00:00.000
9 1970.01.10 00:00:00.000
10 1970.01.11 00:00:00.000
11 1970.01.12 00:00:00.000
12 1970.01.13 00:00:00.000
13 1970.01.14 00:00:00.000
14 1970.01.15 00:00:00.000
15 1970.01.16 00:00:00.000
16 1970.01.17 00:00:00.000
17 1970.01.18 00:00:00.000
18 1970.01.19 00:00:00.000
19 1970.01.20 00:00:00.000
20 1970.01.21 00:00:00.000
21 1970.01.22 00:00:00.000
22 1970.01.23 00:00:00.000
23 1970.01.24 00:00:00.000
24 1970.01.25 00:00:00.000
25 1970.01.26 00:00:00.000
26 1970.01.27 00:00:00.000
27 1970.01.28 00:00:00.000
28 1970.01.29 00:00:00.000
29 1970.01.30 00:00:00.000
30 1970.01.31 00:00:00.000
31 1970.02.01 00:00:00.000

2 days by seconds...
..................

50 years by days...

Random seconds
..................................................
Ok

这是一个改进的版本,它还返回小时、分钟和秒。此代码处理分数秒,但是,
time.struct\u time
的字段都是整数,因此我的
test
函数不能与分数秒一起使用

#!/usr/bin/env python

''' Convert Unix Epoch seconds to (proleptic) Gregorian date

    Algorithm by E. G. Richards, https://en.wikipedia.org/wiki/Julian_day

    See http://stackoverflow.com/q/35796786/4014959

    Python implementation by PM 2Ring 2016.03.07
'''

from time import time, gmtime
from random import randint
import sys    

def epoch_seconds_to_gregorian_date(eseconds):
    # Algorithm parameters for Gregorian calendar
    y = 4716; j = 1401; m = 2; n = 12; r = 4; p = 1461 
    v = 3; u = 5; s = 153; w = 2; B = 274277; C = -38

    #Julian day, rounded
    J = int(0.5 + eseconds / 86400.0 + 2440587.5)

    #Date calculation
    f = J + j + (((4 * J + B) // 146097) * 3) // 4 + C
    e = r * f + v
    g = (e % p) // r
    h = u * g + w
    D = (h % s) // u + 1
    M = (h // s + m) % n + 1
    Y = (e // p) - y + (n + m - M) // n

    #Time calculation
    seconds = eseconds % 86400
    t = int(seconds)
    hr, t = divmod(t, 3600)
    mn = t // 60
    seconds -= 3600 * hr + 60 * mn

    return Y, M, D, hr, mn, seconds

# Tests
def test(s):
    t = gmtime(s)
    gmdate = t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec
    e2gdate = epoch_seconds_to_gregorian_date(s)
    assert gmdate == e2gdate, (s, gmdate, e2gdate)
    return '%d.%02d.%02d %02d:%02d:%06.3f' % e2gdate

print 'now'
s = time()
print s, epoch_seconds_to_gregorian_date(s), gmtime(s)

print '\nhours'
for i in xrange(25):
    s = 3600 * i
    print i, test(s) 

print '\ndays'
for i in xrange(32):
    s = 86400 * i
    print i, test(s)

print '\n2 days by seconds...'
for i in xrange(86400 * 2):
    test(i)
    if i % 10000 == 0:
        sys.stderr.write('.')
sys.stderr.write('\n')

n = 50
print '\n%d years by days...' % n
for i in xrange(365 * n):
    s = 86400 * i    
    test(s)    

n = 500000
print '\nRandom seconds'
for i in xrange(n):
    s = randint(0, 2147483647)
    test(s)
    if i % 10000 == 0:
        sys.stderr.write('.')
sys.stderr.write('\n')

print 'Ok'
输出

hours
0 1970.1.1
1 1970.1.1
2 1970.1.1
3 1970.1.1
4 1970.1.1
5 1970.1.1
6 1970.1.1
7 1970.1.1
8 1970.1.1
9 1970.1.1
10 1970.1.1
11 1970.1.1
12 1970.1.1
13 1970.1.1
14 1970.1.1
15 1970.1.1
16 1970.1.1
17 1970.1.1
18 1970.1.1
19 1970.1.1
20 1970.1.1
21 1970.1.1
22 1970.1.1
23 1970.1.1
24 1970.1.2

days
0 1970.1.1
1 1970.1.2
2 1970.1.3
3 1970.1.4
4 1970.1.5
5 1970.1.6
6 1970.1.7
7 1970.1.8
8 1970.1.9
9 1970.1.10
10 1970.1.11
11 1970.1.12
12 1970.1.13
13 1970.1.14
14 1970.1.15
15 1970.1.16
16 1970.1.17
17 1970.1.18
18 1970.1.19
19 1970.1.20
20 1970.1.21
21 1970.1.22
22 1970.1.23
23 1970.1.24
24 1970.1.25
25 1970.1.26
26 1970.1.27
27 1970.1.28
28 1970.1.29
29 1970.1.30
30 1970.1.31
31 1970.2.1

2 days by seconds...

50 years by days...
Ok
now
1457355412.48 (2016, 3, 7, 12, 56, 52.476011991500854) time.struct_time(tm_year=2016, tm_mon=3, tm_mday=7, tm_hour=12, tm_min=56, tm_sec=52, tm_wday=0, tm_yday=67, tm_isdst=0)

hours
0 1970.01.01 00:00:00.000
1 1970.01.01 01:00:00.000
2 1970.01.01 02:00:00.000
3 1970.01.01 03:00:00.000
4 1970.01.01 04:00:00.000
5 1970.01.01 05:00:00.000
6 1970.01.01 06:00:00.000
7 1970.01.01 07:00:00.000
8 1970.01.01 08:00:00.000
9 1970.01.01 09:00:00.000
10 1970.01.01 10:00:00.000
11 1970.01.01 11:00:00.000
12 1970.01.01 12:00:00.000
13 1970.01.01 13:00:00.000
14 1970.01.01 14:00:00.000
15 1970.01.01 15:00:00.000
16 1970.01.01 16:00:00.000
17 1970.01.01 17:00:00.000
18 1970.01.01 18:00:00.000
19 1970.01.01 19:00:00.000
20 1970.01.01 20:00:00.000
21 1970.01.01 21:00:00.000
22 1970.01.01 22:00:00.000
23 1970.01.01 23:00:00.000
24 1970.01.02 00:00:00.000

days
0 1970.01.01 00:00:00.000
1 1970.01.02 00:00:00.000
2 1970.01.03 00:00:00.000
3 1970.01.04 00:00:00.000
4 1970.01.05 00:00:00.000
5 1970.01.06 00:00:00.000
6 1970.01.07 00:00:00.000
7 1970.01.08 00:00:00.000
8 1970.01.09 00:00:00.000
9 1970.01.10 00:00:00.000
10 1970.01.11 00:00:00.000
11 1970.01.12 00:00:00.000
12 1970.01.13 00:00:00.000
13 1970.01.14 00:00:00.000
14 1970.01.15 00:00:00.000
15 1970.01.16 00:00:00.000
16 1970.01.17 00:00:00.000
17 1970.01.18 00:00:00.000
18 1970.01.19 00:00:00.000
19 1970.01.20 00:00:00.000
20 1970.01.21 00:00:00.000
21 1970.01.22 00:00:00.000
22 1970.01.23 00:00:00.000
23 1970.01.24 00:00:00.000
24 1970.01.25 00:00:00.000
25 1970.01.26 00:00:00.000
26 1970.01.27 00:00:00.000
27 1970.01.28 00:00:00.000
28 1970.01.29 00:00:00.000
29 1970.01.30 00:00:00.000
30 1970.01.31 00:00:00.000
31 1970.02.01 00:00:00.000

2 days by seconds...
..................

50 years by days...

Random seconds
..................................................
Ok

time
不返回年初以来的秒数,有些月份没有30天。那么为什么天、小时和分钟是正确的呢?与
date.fromtimestamp(time.time())
相同,因为它们不受您的近似“每月天数”计算的影响。但去年甚至还有闰秒,所以在你的例子中秒应该是关闭的。那么“每月天数”的更好近似值是什么呢?我今天(2016年3月7日)运行了历元秒到公历日期(time()),结果是(62650,3,11):…@Lamer:我不知道你做错了什么。我刚刚做了
s=time();打印s,历元秒到格里高利日期,gmtime(s)
,得到这个结果:
1457347949.53(2016,3,7)time.struct_time(tm_year=2016,tm_mon=3,tm_mday=7,tm_hour=10,tm_min=52,tm_sec=29,tm_wday=0,tm_yday=67,tm_isdst=0)
PM 2Ring:)我实际上在调用time.time():,这可能是个打字错误。现在它正确地显示了日期。谢谢如何计算小时、分和秒?是的。为了完整起见,虽然这个问题只问一个月,我已经知道如何找到时间,但它可能对其他人有帮助。@lalamer:当然。这也是我的想法。