解决python时间戳最大为3001年1月1日15时59分59秒的问题
生活随笔
收集整理的這篇文章主要介紹了
解决python时间戳最大为3001年1月1日15时59分59秒的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自己寫個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 mathdef calcTimeStamp(t):#時間戳的原點是1970年1月1日0時0分0秒minute = 60hour = 60*minuteday = 24*hourcommonyear = 365*dayleapyear = 366*daybigmonth = 31*daysmallmonth = 30*dayleapfebruary = 29*daycommonfebruary = 28*dayleft = ty = 1970m = 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 -=leapyearelse:breakelse:if left-commonyear>=0:left -=commonyearelse:breaky +=1isleap = Falseif y%100!=0 and y%4==0 or y%400==0: #判斷是否閏年isleap=Truemonthnext = 1if left-bigmonth>=0: #去掉1月份的天數,步進到2月份left -=bigmonthm+=1monthnext = 2if left-(leapfebruary if isleap else commonfebruary)>=0 and monthnext == 2: #去掉2月份的天數,步進到3月份left -= leapfebruary if isleap else commonfebruarym+=1monthnext = 3if left-bigmonth>=0 and monthnext == 3: #去掉3月份的天數,步進到4月份left -=bigmonthm+=1monthnext = 4if left-smallmonth>=0 and monthnext == 4: #去掉4月份的天數,步進到5月份left -=smallmonthm+=1monthnext = 5if left-bigmonth>=0 and monthnext == 5: #去掉5月份的天數,步進到6月份left -=bigmonthm+=1monthnext = 6if left-smallmonth>=0 and monthnext == 6: #去掉6月份的天數,步進到7月份left -=smallmonthm+=1monthnext = 7if left-bigmonth>=0 and monthnext == 7: #去掉7月份的天數,步進到8月份left -=bigmonthm+=1monthnext = 8if left-bigmonth>=0 and monthnext == 8: #去掉8月份的天數,步進到9月份left -=bigmonthm+=1monthnext = 9if left-smallmonth>=0 and monthnext == 9: #去掉9月份的天數,步進到10月份left -=smallmonthm+=1monthnext = 10if left-bigmonth>=0 and monthnext == 10: #去掉10月份的天數,步進到11月份left -=bigmonthm+=1monthnext = 11if left-smallmonth>=0 and monthnext == 11: #去掉11月份的天數,步進到12月份left -=smallmonthm+=1monthnext = 12def 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 31elif m==4 or m==6 or m==9 or m==11:return 30else:return 29 if (y%100!=0 and y%4==0 or y%400==0) else 28d = math.floor(left/day) + 1left -= (d-1)*dayh = math.floor(left/hour) + 8left -= (h-8)*houri = math.floor(left/minute)left -= i*minutes = leftif s>59:s=0i+=1if i>59:i=0h+=1if h>23:h-=24d+=1if d>maxday(y,m):d=1m+=1if m>12:m=1y+=1def 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 , datetimetss1 = '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>這個函數的作用,就是將時間戳轉成我們平時的標準時間,包括了年月日時分秒,毫秒我就不加了,意義不大,有需要的同學自己弄吧。
總結
以上是生活随笔為你收集整理的解决python时间戳最大为3001年1月1日15时59分59秒的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CTF 大小写字母转换 try lowe
- 下一篇: 用python selenium实现一个