练手——用Python写的时间戳转换为北京时间的小工具
#北京时间需加上8小时
bj = 8*3600
def time_stamp(times):
#一天总秒数
nonDaySeconds = 24*3600
leapmonths = [31,29,31,30,31,30,31,31,30,31,30,31]
nonleapmonths = [31,28,31,30,31,30,31,31,30,31,30,31]
#四年为一个周期,从1972年开始
fourYearDays = (366+365+365+365)
year = 0
currentDays = 0
sumDay = 0
month = 0
#计算自1970年以来经过的天数
nDays = int(times/nonDaySeconds)
#计算当天已经过了多少毫秒
currentDaySeconds = times%nonDaySeconds
#计算自1973年以来经过的多少个完整4年周期,并计算上一个闰年是哪一年
lastLeapYear = int((nDays-(365+365+366))/fourYearDays)*4+(1970+2)
#计算不足一个4年周期的天数
lDays = (nDays-(365+365+366))%fourYearDays
#print(lastLeapYear,lDays)
#判断当前属于四年周期的哪一年,并计算当年已经走过的总天数
if lDays < 365:
year = 1
currentDays = lDays
elif lDays< (365+365):
year = 2
currentDays = lDays - 365
elif lDays < (365+365+365):
year = 3
currentDays = lDays - 365 - 365
elif lDays < (365+365+365+366):
year = 4
currentDays = lDays - 365 - 365 - 365
currentYear = lastLeapYear + year
#print(currentYear,currentDays)
#计算几月几号
if year == 4:
#如果是闰年
for i in range(len(leapmonths)):
if sumDay <= currentDays:
sumDay += leapmonths[i]
month += 1
day = (currentDays - sumDay + 1) + leapmonths[month-1]
else:
for i in range(len(nonleapmonths)):
if sumDay <= currentDays:
sumDay += nonleapmonths[i]
month += 1
day = (currentDays - sumDay + 1) + nonleapmonths[month-1]
#print(month,day)
#计算时分秒
currentHour = int(currentDaySeconds/(60*60))
currentMinute = int((currentDaySeconds%(60*60))/60)
currentSeconds = (currentDaySeconds%(60*60))%60
#print(currentHour,currentMinute,currentSeconds)
print(str(currentYear)+' 年 '+judgetime(month)+' 月 '+judgetime(day)+' 日 '+
judgetime(currentHour)+':'+judgetime(currentMinute)+':'+judgetime(currentSeconds))
def judgetime(value):
if value < 10:
return '0' + str(value)
else:
return str(value)
练手——用Python写的时间戳转换为北京时间的小工具的更多相关文章
- 用Python写一个向数据库填充数据的小工具
一. 背景 公司又要做一个新项目,是一个合作型项目,我们公司出web展示服务,合作伙伴线下提供展示数据. 而且本次项目是数据统计展示为主要功能,并没有研发对应的数据接入接口,所有展示数据源均来自数据库 ...
- python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期
项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...
- JavaScript把客户端时间转换为北京时间
写在前面 写了一遍又一遍,网页老卡住,没保存下来,不写了. 时间转换代码 //获得北京时间 Date.prototype.getBJDate = function () { //获得当前运行环境时间 ...
- Python 将时间戳转换为本地时间并进行格式化
在python中,时间戳默认是为格林威治时间,而我们为东八区 使用localtime() 本地化时间戳 使用 strftime() 格式化时间戳 time = time.strftime('%Y%m% ...
- 几个非常适合练手的python爬虫项目,总有一个能搞定!
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:Python玩家 注意:如果你平时学Python有问题找不到人解答,可以 ...
- 有哪些适合新手练手的Python项目?
http://blog.csdn.net/Lina_ACM/article/details/54581721
- 用Python写了个下载快手视频的小脚本
最近又重新拾起了,对python的热情. 贴个地址: https://github.com/d1y/lovepack/blob/master/kuaishou.py 前戏说明 因为我近乎癫狂的喜欢一个 ...
- Python写一个Windows下的android设备截图工具
界面版 利用python的wx库写个ui界面,用来把android设备的截图输出到电脑屏幕,前提需要安装adb,涉及到的python库也要安装.代码如下: import wx,subprocess,o ...
- 使用js将Unix时间戳转换为普通时间
var unixtime=1358932051;formatTime (time) { let unixtime = time let unixTimestamp = new Date(unixtim ...
随机推荐
- [LeetCode] Ambiguous Coordinates 模糊的坐标
We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)". Then, we re ...
- linux --- Ansible篇
ansible背景 1.什么是ansible? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优 ...
- 小甲鱼零基础python课后题 P22 021函数:lambda表达式
0.请使用lambda表达式将下边函数转变为匿名函数 def fun_A(x,y=3): return x*y 答: lambda x,y=3:x*y 1.请将下边的匿名函数转变为普通的屌丝函数 la ...
- CentOS最基本的20个常用命令
1. man 对你熟悉或不熟悉的命令提供帮助解释eg:man ls 就可以查看ls相关的用法注:按q键或者ctrl+c退出,在linux下可以使用ctrl+c终止当前程序运行. 2. ls 查看目录或 ...
- CSIS 1119B/C Introduction to Data Structures and Algorithms
CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...
- Linux 部署 xxl-job 注意问题
问题:Failed to create parent directories for [/data/applogs/xxl-job/xxl-job-admin.log][原因:权限不足] 启动终端: ...
- Windows服务器外网无法访问web的解决方法
windows环境下使用集成 IIS服务器时一般不会发生外网无法访问的问题,而使用apache.kangle.lighttpd.niginx.tomcat等时:服务器上可通过配置的域名访问网站,pin ...
- linux 对MTD分区nand flash的烧写和读取
使用mtd-utils工具实现对flash的升级分区的烧写yaffs2 yaffs2的格式是根据所使用的nandflash来制作的,不同的nandflash,得到的yaffs2是不一样的,具体可以参考 ...
- mysql--实现oracle的row_number() over功能
有时候我们想要得到每个分组的前几条记录,这个时候oracle中row_number函数使用非常方便,但可惜mysql没有.网上搜了些实现方法. 表flow_task有phaseno(序列号),obje ...
- MySQL 允许局域网内其他网段主机访问本地MySql数据库
Mac下和Windows下均适合