解决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

相关文章

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu