#!/usr/bin/env python
#_*_coding:utf-8_*_ # 本地时间 转换 为时间戳
import time
import pytz
import datetime
dateC1=datetime.datetime(2015,11,30,15,55,00)
timestamp2=time.mktime(dateC1.timetuple())
print timestamp2 #时间戳转换为本地时间
import datetime
import time
ltime=time.localtime(1470009000)
ltime=time.localtime(1479285300)
timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime)
print timeStr # UTCS时间转换为时间戳 2016-07-31T16:00:00Z
def utc_to_local(utc_time_str, utc_format='%Y-%m-%dT%H:%M:%SZ'):
local_tz = pytz.timezone('Asia/Chongqing')
local_format = "%Y-%m-%d %H:%M"
utc_dt = datetime.datetime.strptime(utc_time_str, utc_format)
local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
time_str = local_dt.strftime(local_format)
return int(time.mktime(time.strptime(time_str, local_format))) # 本地时间转换为UTC def local_to_utc(local_ts, utc_format='%Y-%m-%dT%H:%MZ'):
local_tz = pytz.timezone('Asia/Chongqing')
local_format = "%Y-%m-%d %H:%M"
time_str = time.strftime(local_format, time.localtime(local_ts))
dt = datetime.datetime.strptime(time_str, local_format)
local_dt = local_tz.localize(dt, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
return utc_dt.strftime(utc_format) # t = u"2016-07-31T16:00:00Z"
t = u"2015-07-31T15:55:00Z"
# t = u"2015-11-30T15:55:00Z"
t1 = u"2015-12-31T15:55:00Z" # 1470441600 1470095400 1470613800
ret = utc_to_local(t)
ret1 = utc_to_local(t1)
print ret,ret1 #1469923200 1470009600

  

python UTC,时间戳,字符串转换的更多相关文章

  1. python 数字和字符串转换问题

    一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) ( ...

  2. python :eval将字符串转换成字典

    #将字符串打印成字典 b=''' {'record': {'weight':20,'server':'100.1.7.9','maxconn':50},'backend': 'www.oldboy.o ...

  3. python学习之字符串转换

    配置环境:python 3.6   python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- def strCase() ...

  4. python 将字节字符串转换成十六进制字符串

    想将一个十六进制字符串解码成一个字节字符串或者将一个字节字符串编码成一个十六进制字符串 1)只是简单的解码或编码一个十六进制的原始字符串,可以使用 binascii模块 >>> s ...

  5. python 字节与字符串转换

    name = 'laogaoyang' nameBytes = name.encode('utf-8') # 字节 nameStr = nameBytes.decode('utf-8')# 字符串 p ...

  6. python 基础 列表 字符串转换

    1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'worl ...

  7. Python 日期和时间戳的转换

    Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...

  8. Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳

    #1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...

  9. 使用python制作时间戳转换工具

    使用python制作时间戳转换工具 python 时间戳转日期 日期转时间戳 前言:作为一个程序员一般情况下,json和时间戳是常用的两个工具,我咨询过很多个朋友,他们一般都是通过在线工具对json进 ...

随机推荐

  1. C++11散列表

    [C++11散列表] 散列表对应于C++03中的hash_xxx,分为set和map两种 上述的类型将满足对一个容器类型的要求,同时也提供访问其中元素的成员函数: insert, erase, beg ...

  2. Codeforces 687C. The Values You Can Make (dp)

    题目链接:http://codeforces.com/problemset/problem/687/C 题目大概说给n个各有价值的硬币,要从它们中选出若干个组合成面值k,而要求的是各个方案里这些选出的 ...

  3. C#.NET 消息机制

    一.消息概述 众人周知,window系统是一个消息驱动的系统, windows操作系统本身有自己的消息队列,消息循环,它捕捉键盘,鼠标的动作生成消息,并将这个消息传给应用程序的消息队列. 余下的工作有 ...

  4. How to use SourceGear DiffMerge in SourceSafe, TFS, and SVN【项目】

    What is DiffMerge DiffMerge is yet-another-diff-and-merge-tool from the fine folks at SourceGear.  I ...

  5. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]

    2.2身份验证开发 在我们的案例中,我们是用户通过Web应用程序进行身份识别. 上面的图示说明了如下的一些概念 l Azure AD 是标识提供程序,负责对组织的目录中存在的用户和应用程序的标识进行验 ...

  6. Nutch 2.2+MySQL+Solr4.2实现网站内容的抓取和索引

    原文地址: http://blog.sina.com.cn/s/blog_3c9872d00101p4f0.html Nutch 2.2.1发布快两月了,该版本与Nutch之前版本相比,有较大变化,特 ...

  7. NotePad++ 列模式(在多行开头统一添加相同内容)

    ==> 按住Alt键不放,用鼠标左键从第一行的开头处按住向下拉,直到所有行 松开Alt键和鼠标左键,你会发现光标变成了一条跨越所有行的竖线 ==> 如果不想使用鼠标操作,可以使用 Alt+ ...

  8. icacls备份与还原ACL列表(NTFS权限)--Robocopy

    icacls c:\windows\* /save AclFile /T- 将 c:\windows 及其子目录下所有文件的 ACL 保存到 AclFile. icacls c:\windows\ / ...

  9. 无法将类型为 excel.applicationclass 的 com 强制转换为接口类型 的解决方法。

    今天碰到客户的电脑在导出EXCEL的时候提示,无法将类型为 excel.applicationclass 的 com 强制转换为接口类型 excel._application 的问题 最后用下面的方法 ...

  10. codechef Arranging Cup-cakes题解

    Arranging Cup-cakes Our Chef is catering for a big corporate office party and is busy preparing diff ...