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

相关文章

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

如何解决线上平台抽佣高 线下门店客流少的痛点!

目前,许多传统零售店铺正遭遇客源下降的难题。尽管广告推广能带来一定的客流,但其费用昂贵。鉴于此,众多零售商纷纷选择加入像美团、饿了么和抖音这样的大型在线平台,但这些平台的高佣金率导致了利润的大幅缩水。在这样的市场环境下,商家之间的合作网络逐渐成为一种有效的解决方案,通过资源和客户基础的共享,实现共同的利益增长。 以最近在上海兴起的一个跨行业合作平台为例,该平台融合了环保消费积分系统,在短

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss