解决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结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下