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

31 篇文章 3 订阅
16 篇文章 3 订阅

自己写个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 math

def calcTimeStamp(t):
	#时间戳的原点是1970年1月1日0时0分0秒
	minute = 60
	hour = 60*minute
	day = 24*hour

	commonyear = 365*day
	leapyear = 366*day

	bigmonth = 31*day
	smallmonth = 30*day
	leapfebruary = 29*day
	commonfebruary = 28*day

	left = t
	y = 1970
	m = 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 -=leapyear
			else:
				break
		else:
			if left-commonyear>=0:
				left -=commonyear
			else:
				break
		y +=1

	isleap = False
	if y%100!=0 and y%4==0 or y%400==0: #判断是否闰年
		isleap=True
	
	monthnext = 1
	if left-bigmonth>=0: #去掉1月份的天数,步进到2月份
		left -=bigmonth
		m+=1
		monthnext = 2
	if left-(leapfebruary if isleap else commonfebruary)>=0 and monthnext == 2: #去掉2月份的天数,步进到3月份
		left -= leapfebruary if isleap else commonfebruary
		m+=1
		monthnext = 3
	if left-bigmonth>=0 and monthnext == 3: #去掉3月份的天数,步进到4月份
		left -=bigmonth
		m+=1
		monthnext = 4
	if left-smallmonth>=0 and monthnext == 4: #去掉4月份的天数,步进到5月份
		left -=smallmonth
		m+=1
		monthnext = 5
	if left-bigmonth>=0 and monthnext == 5: #去掉5月份的天数,步进到6月份
		left -=bigmonth
		m+=1
		monthnext = 6
	if left-smallmonth>=0 and monthnext == 6: #去掉6月份的天数,步进到7月份
		left -=smallmonth
		m+=1
		monthnext = 7
	if left-bigmonth>=0 and monthnext == 7: #去掉7月份的天数,步进到8月份
		left -=bigmonth
		m+=1
		monthnext = 8
	if left-bigmonth>=0 and monthnext == 8: #去掉8月份的天数,步进到9月份
		left -=bigmonth
		m+=1
		monthnext = 9
	if left-smallmonth>=0 and monthnext == 9: #去掉9月份的天数,步进到10月份
		left -=smallmonth
		m+=1
		monthnext = 10
	if left-bigmonth>=0 and monthnext == 10: #去掉10月份的天数,步进到11月份
		left -=bigmonth
		m+=1
		monthnext = 11
	if left-smallmonth>=0  and  monthnext == 11: #去掉11月份的天数,步进到12月份
		left -=smallmonth
		m+=1
		monthnext = 12

	def 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 31
		elif m==4 or m==6 or m==9 or m==11:
			return 30
		else:
			return 29 if (y%100!=0 and y%4==0 or y%400==0) else 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

	def 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 , datetime

tss1 = '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>

这个函数的作用,就是将时间戳转成我们平时的标准时间,包括了年月日时分秒,毫秒我就不加了,意义不大,有需要的同学自己弄吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Python中,可以使用时间来比较两个时间的大小。时间是指从1970年1月1午夜(UTC/GMT的午夜)开始所经过的秒数。可以使用datetime模块中的datetime对象的timestamp()方法将时间转换为时间。然后,可以直接使用比较运算符(如>、<、==)来比较两个时间的大小。 以下是一个示例代码,展示了如何比较两个时间的大小: ```python import datetime time1 = datetime.datetime(2022, 1, 1, 12, 0, 0) time2 = datetime.datetime(2022, 1, 1, 12, 30, 0) timestamp1 = time1.timestamp() timestamp2 = time2.timestamp() if timestamp1 > timestamp2: print("time1 is later than time2") elif timestamp1 < timestamp2: print("time1 is earlier than time2") else: print("time1 and time2 are the same") ``` 在上述示例中,我们首先创建了两个datetime对象表示不同的时间。然后,使用timestamp()方法将这些对象转换为时间。最后,我们使用比较运算符比较了这两个时间的大小并打印了相应的结果。 注意,时间是以秒为单位的浮点数,所以可以包含小数部分,可以使用小数部分来表示更精确的时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python时间获取、时间比较、时间推算](https://blog.csdn.net/qq_29707567/article/details/126117552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python比较期大小_Python期的处理——datetime模块](https://blog.csdn.net/weixin_39738667/article/details/110154439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值