#北京时间需加上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写的时间戳转换为北京时间的小工具的更多相关文章

  1. 用Python写一个向数据库填充数据的小工具

    一. 背景 公司又要做一个新项目,是一个合作型项目,我们公司出web展示服务,合作伙伴线下提供展示数据. 而且本次项目是数据统计展示为主要功能,并没有研发对应的数据接入接口,所有展示数据源均来自数据库 ...

  2. python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期

    项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...

  3. JavaScript把客户端时间转换为北京时间

    写在前面 写了一遍又一遍,网页老卡住,没保存下来,不写了. 时间转换代码 //获得北京时间 Date.prototype.getBJDate = function () { //获得当前运行环境时间 ...

  4. Python 将时间戳转换为本地时间并进行格式化

    在python中,时间戳默认是为格林威治时间,而我们为东八区 使用localtime() 本地化时间戳 使用 strftime() 格式化时间戳 time = time.strftime('%Y%m% ...

  5. 几个非常适合练手的python爬虫项目,总有一个能搞定!

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:Python玩家 注意:如果你平时学Python有问题找不到人解答,可以 ...

  6. 有哪些适合新手练手的Python项目?

    http://blog.csdn.net/Lina_ACM/article/details/54581721

  7. 用Python写了个下载快手视频的小脚本

    最近又重新拾起了,对python的热情. 贴个地址: https://github.com/d1y/lovepack/blob/master/kuaishou.py 前戏说明 因为我近乎癫狂的喜欢一个 ...

  8. Python写一个Windows下的android设备截图工具

    界面版 利用python的wx库写个ui界面,用来把android设备的截图输出到电脑屏幕,前提需要安装adb,涉及到的python库也要安装.代码如下: import wx,subprocess,o ...

  9. 使用js将Unix时间戳转换为普通时间

    var unixtime=1358932051;formatTime (time) { let unixtime = time let unixTimestamp = new Date(unixtim ...

随机推荐

  1. mobile_缩放

    document.documentElement.clientWidth       不包含滚动条 window.innerWidth                                  ...

  2. react_app 项目开发 (5)_前后端分离_后台管理系统_开始

    项目描述 技术选型 react API 接口 接口文档,url,请求方式,参数类型, 根据文档描述的方法,进行 postman 测试,看是否能够得到理想的结果 collections - 创建文件取项 ...

  3. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  4. Python基础之语句1

    一.行定义(两类): 1.物理行:程序员编写代码的行. 2.逻辑行:python解释器需要执行的指令. 建议:一个逻辑行在一个物理行上,若一个物理行使用多个逻辑行,需要使用分号隔开:如果逻辑行过长,可 ...

  5. Java作业三(2017-9-25)

    /*程序员龚猛*/ 作业1 public class Variable_Demo$Long{ public static void main(String[]args { int i=3; long ...

  6. day27、28 二十八、项目:选课系统

    选课系统 作业要求 角色:学校.学生.课程.讲师 要求: 1. 创建北京.上海 2 所学校 ----> 创建学校 2. 创建linux , python , go 3个课程 , linux\py ...

  7. centos6.8 编译安装lnmp php7.2 mysql5.6 nginx1.1.4

    编译操作参考版,没有每一步详细操作,慎入 关闭selinux和防火墙 service iptables stop chkconfig iptables off vi /etc/selinux/conf ...

  8. corefx 源码学习:SqlClient 是如何同步建立 Socket 连接的

    在昨天的技术周会上发现 EnyimMemcached 中建立 Socket 连接的代码有问题,今天坐车的时候在手机上阅读 .net core 2.2 的 SqlClient 中同步建立 Socket ...

  9. SQL SERVER 基本操作语句

    Sql 是一种结构化的查询语言:Sql是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理‘关系型数据库’系统:Sql对大小写不敏感:Sql不是数据库,是行业标准,是结构化的查询语言 In ...

  10. Oracle数据泵远程导入数据

    查看现存镜像目录 select * from dba_directories; 创建镜像目录 create or replace directory my_dir as 'local_dir' ; 把 ...