解决python时间戳最大为3001年1月1日15时59分59秒的问题

2024-01-02 02:38

本文主要是介绍解决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秒的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/561113

相关文章

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我