自己写个python函数解决python时间戳最大为3001年1月1日15时59分59秒的问题
今天碰到一个情况,在oracle查数,某个数的值是个时间值,而且是9999年12月31日,然后python的utils.py就报错了,研究了半天,发现就是时间戳不支持最大值太小的问题。
我没有深入研究过python的时间戳原理,但是一旦时间超过3001年1月1日15时59分59秒就会报错,难道这一天是传说中的世界末日吗?我完全不明白为什么要做这样的限制。
我们对症下药就是了,既然python自己的库没办法转换大值时间戳,那就自己自己造轮子呗。然后把函数放在utils.py的355行位置就ok了。
很久没写csdn的文章了,csdn的界面越改越奇怪。作为一个给程序员使用的博客平台,界面过度简洁用起来反倒很不顺手,连标题都很不明显,我真不知道这样子改版的意义何在。
不罗嗦了,直接上代码吧:
import math
def calcTimeStamp(t):
#时间戳的原点是1970年1月1日0时0分0秒
minute = 60
hour = 60*minute
day = 24*hour
commonyear = 365*day
leapyear = 366*day
bigmonth = 31*day
smallmonth = 30*day
leapfebruary = 29*day
commonfebruary = 28*day
left = t
y = 1970
m = 1 #月,从1月开始
d = 1 #日,从1日开始
h = 0 #时
i = 0 #分
s = 0 #秒
while True:
if y%100!=0 and y%4==0 or y%400==0: #如果是闰年
if left-leapyear>=0:
left -=leapyear
else:
break
else:
if left-commonyear>=0:
left -=commonyear
else:
break
y +=1
isleap = False
if y%100!=0 and y%4==0 or y%400==0: #判断是否闰年
isleap=True
monthnext = 1
if left-bigmonth>=0: #去掉1月份的天数,步进到2月份
left -=bigmonth
m+=1
monthnext = 2
if left-(leapfebruary if isleap else commonfebruary)>=0 and monthnext == 2: #去掉2月份的天数,步进到3月份
left -= leapfebruary if isleap else commonfebruary
m+=1
monthnext = 3
if left-bigmonth>=0 and monthnext == 3: #去掉3月份的天数,步进到4月份
left -=bigmonth
m+=1
monthnext = 4
if left-smallmonth>=0 and monthnext == 4: #去掉4月份的天数,步进到5月份
left -=smallmonth
m+=1
monthnext = 5
if left-bigmonth>=0 and monthnext == 5: #去掉5月份的天数,步进到6月份
left -=bigmonth
m+=1
monthnext = 6
if left-smallmonth>=0 and monthnext == 6: #去掉6月份的天数,步进到7月份
left -=smallmonth
m+=1
monthnext = 7
if left-bigmonth>=0 and monthnext == 7: #去掉7月份的天数,步进到8月份
left -=bigmonth
m+=1
monthnext = 8
if left-bigmonth>=0 and monthnext == 8: #去掉8月份的天数,步进到9月份
left -=bigmonth
m+=1
monthnext = 9
if left-smallmonth>=0 and monthnext == 9: #去掉9月份的天数,步进到10月份
left -=smallmonth
m+=1
monthnext = 10
if left-bigmonth>=0 and monthnext == 10: #去掉10月份的天数,步进到11月份
left -=bigmonth
m+=1
monthnext = 11
if left-smallmonth>=0 and monthnext == 11: #去掉11月份的天数,步进到12月份
left -=smallmonth
m+=1
monthnext = 12
def maxday(y,m):
if m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12:
return 31
elif m==4 or m==6 or m==9 or m==11:
return 30
else:
return 29 if (y%100!=0 and y%4==0 or y%400==0) else 28
d = math.floor(left/day) + 1
left -= (d-1)*day
h = math.floor(left/hour) + 8
left -= (h-8)*hour
i = math.floor(left/minute)
left -= i*minute
s = left
if s>59:
s=0
i+=1
if i>59:
i=0
h+=1
if h>23:
h-=24
d+=1
if d>maxday(y,m):
d=1
m+=1
if m>12:
m=1
y+=1
def mod(x):
return '0' + str(x) if x<10 else str(x)
return mod(y) + '-' + mod(m) + '-' + mod(d) + ' ' + mod(h) + ':' + mod(i) + ':' + mod(s)
#print(calcTimeStamp(2533733280620675))
#
#
import time , datetime
tss1 = '3001-01-01 15:59:59'
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))
print(datetime.datetime.fromtimestamp(timeStamp))
再发一个js版本:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
function p(x){console.log(x)}
function calcTimeStamp(t){
//时间戳的原点是1970年1月1日0时0分0秒
var minute = 60;
var hour = 60*minute;
var day = 24*hour;
var commonyear = 365*day;
var leapyear = 366*day;
var bigmonth = 31*day;
var smallmonth = 30*day;
var leapfebruary = 29*day;
var commonfebruary = 28*day;
var left = t;
var y = 1970;
var m = 1; //月,从1月开始
var d = 1; //日,从1日开始
var h = 0; //时
var i = 0; //分
var s = 0; //秒
while(true){
if(y%100!=0&&y%4==0||y%400==0){ //如果是闰年
if(left-leapyear>=0){
left -=leapyear;
}
else break;
}
else{
if(left-commonyear>=0){
left -=commonyear;
}
else break;
}
y +=1;
}
var isleap = false;
if(y%100!=0&&y%4==0||y%400==0){ //判断是否闰年
isleap=true;
}
var monthnext = 1;
if(left-bigmonth>=0){ //去掉1月份的天数,步进到2月份
left -=bigmonth;
m+=1;
monthnext = 2;
}
if(left-(isleap?leapfebruary:commonfebruary)>=0 && monthnext == 2){ //去掉2月份的天数,步进到3月份
left -=(isleap?leapfebruary:commonfebruary);
m+=1;
monthnext = 3;
}
if(left-bigmonth>=0 && monthnext == 3){ //去掉3月份的天数,步进到4月份
left -=bigmonth;
m+=1;
monthnext = 4;
}
if(left-smallmonth>=0 && monthnext == 4){ //去掉4月份的天数,步进到5月份
left -=smallmonth;
m+=1;
monthnext = 5;
}
if(left-bigmonth>=0 && monthnext == 5){ //去掉5月份的天数,步进到6月份
left -=bigmonth;
m+=1;
monthnext = 6;
}
if(left-smallmonth>=0 && monthnext == 6){ //去掉6月份的天数,步进到7月份
left -=smallmonth;
m+=1;
monthnext = 7;
}
if(left-bigmonth>=0 && monthnext == 7){ //去掉7月份的天数,步进到8月份
left -=bigmonth;
m+=1;
monthnext = 8;
}
if(left-bigmonth>=0 && monthnext == 8){ //去掉8月份的天数,步进到9月份
left -=bigmonth;
m+=1;
monthnext = 9;
}
if(left-smallmonth>=0 && monthnext == 9){ //去掉9月份的天数,步进到10月份
left -=smallmonth;
m+=1;
monthnext = 10;
}
if(left-bigmonth>=0 && monthnext == 10){ //去掉10月份的天数,步进到11月份
left -=bigmonth;
m+=1;
monthnext = 11;
}
if(left-smallmonth>=0 && monthnext == 11){ //去掉11月份的天数,步进到12月份
left -=smallmonth;
m+=1;
monthnext = 12;
}
function maxday(y,m){
if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12){
return 31;
}
else if(m==4 || m==6 || m==9 || m==11){
return 30;
}
else return (y%100!=0&&y%4==0||y%400==0)?29:28;
}
d = Math.floor(left/day) + 1;
left -= (d-1)*day;
h = Math.floor(left/hour) + 8;
left -= (h-8)*hour;
i = Math.floor(left/minute);
left -= i*minute;
s = left;
if(s>59){
s=0;
i+=1;
}
if(i>59){
i=0;
h+=1;
}
if(h>23){
h-=24;
d+=1;
if(d>maxday(y,m)){
d=1;
m+=1;
if(m>12){
m=1;
y+=1;
}
}
}
function mod(x){
return x>10?x:'0'+x;
}
return mod(y) + '-' + mod(m) + '-' + mod(d) + ' ' + mod(h) + ':' + mod(i) + ':' + mod(s);
}
p(calcTimeStamp(new Date('9999-1-31 0:1:02').valueOf()/1000))
</script>
</html>
这个函数的作用,就是将时间戳转成我们平时的标准时间,包括了年月日时分秒,毫秒我就不加了,意义不大,有需要的同学自己弄吧。