python获取指定时间差的时间
在分析数据的时间经常需要截取一定范围时间的数据,比如三天之内,两小时前等等时间要求的数据,因此将该部分经常需要用到的功能模块化,方便以后以后用到的时候复用。在此,也分享给大家。
<span style="font-size:18px;">#coding:utf-8
'''
Created on 2016年5月3日 @author: baocheng
'''
import time
import sys
reload(sys) def get_day_of_day(UTC=False, days=0, hours=0, miutes=0, seconds=0):
'''''
if days>=0,date is larger than today
if days<0,date is less than today
date format = "YYYY-MM-DD"
'''
now = time.time()
timeNew = now + days*24*60*60 + hours*60*60 + miutes*60 + seconds
if UTC :
timeNew = timeNew + time.timezone
t = time.localtime(timeNew)
return time.strftime('%Y-%m-%d %H:%M:%S', t) #使用UTC时间 两小时前
t = get_day_of_day(True,0,-2)
print t
#当地时间 三天前
t = get_day_of_day(False,-3)
print t
#当地时间 三天后
t = get_day_of_day(False,3)
print t</span>
运行后所得结果:
<span style="font-size:18px;">2016-05-03 10:25:56
2016-04-30 20:25:56
2016-05-06 20:25:56</span>
个人博客新址 :dataminingclub
python获取指定时间差的时间的更多相关文章
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- Python获取指定目录下所有子目录、所有文件名
需求 给出制定目录,通过Python获取指定目录下的所有子目录,所有(子目录下)文件名: 实现 import os def file_name(file_dir): for root, dirs, f ...
- Python获取指定文件夹下的文件名
本文采用os.walk()和os.listdir()两种方法,获取指定文件夹下的文件名. 一.os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, t ...
- python 获取文件大小,创建时间和访问时间
# -*- coding: UTF8 -*- import timeimport datetime import os 1. '''把时间戳转化为时间: 1479264792 to 2016-11-1 ...
- python 获取文件的修改时间
os.path.getmtime(name) #获取文件的修改时间 os.stat(path).st_mtime#获取文件的修改时间 os.stat(path).st_ctime #获取文件修改时间 ...
- Python获取指定路径下所有文件的绝对路径
需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...
- python获取指定时间段内的随机不重复的时间点
上篇 <python时间时分秒与秒数的互相转换>http://www.cnblogs.com/gayhub/p/6154707.html 提到了把时间转成秒数的方法, 这篇写写转换成秒数后 ...
- python 通过pytz模块进行时区的转换,获取指定时区的时间
import pytz import time import datetime print(pytz.country_timezones('cn')) # 查询中国所拥有的时区 print(pytz. ...
- iPhone 获取指定格式的时间和日期
1. 显示当前的年月日+时间 NSDate* date = [NSDate date]; NSDateFormatter* formatter = [[[NSDateFormatter alloc] ...
随机推荐
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- [测试题]gene
Description Input Output Sample Input 3A+00A+A+ 00B+D+A- B-C+00C+ Sample Output bounded Hint 题解 //It ...
- [Vijos 2024]无向图最短路径
Description 无向图最短路径问题,是图论中最经典也是最基础的问题之一.本题我们考虑一个有 $n$ 个结点的无向图 $G$.$G$ 是简单完全图,也就是说 $G$ 中没有自环,也没有重边,但任 ...
- ●UOJ 131 [NOI2015] 品酒大会
题链: http://uoj.ac/problem/131 题解: 网上大多数的方法都是用并查集维护.这里呢,给出另一种自己YY的解法(但实际上本质差不多吧): 后缀数组,RMQ,单调栈 1).预处理 ...
- ●BZOJ 1042 [HAOI2008]硬币购物
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1042 题解: 容斥原理,dp预处理首先跑个无限物品的背包dp求出dp[i]表示在四种物品都有 ...
- bzoj 1217: [HNOI2003]消防局的设立
Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了 ...
- bzoj3702二叉树 线段树合并
3702: 二叉树 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 600 Solved: 272[Submit][Status][Discuss] ...
- 谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?
在这篇客座文章中,可口可乐公司的 Patrick Brandt 将向我们介绍他们如何使用 AI 和 TensorFlow 实现无缝式购买凭证. 可口可乐的核心忠诚度计划于 2006 年以 MyCoke ...