Python-时间处理

来源:互联网 发布:linux解压gz文件命令 编辑:程序博客网 时间:2024/06/05 00:14

1. 时间字符串 --> 时间戳


timestr= '2017-06-06 10:00:00'
seconds =  time.mktime(time.strptime(timestr, '%Y-%m-%d %H:%M:%S'))

将时间元组转换成时间戳
time.mktime(timetuple) :


将时间戳转会为时间元组

time.localtime([timestamp]):


 

2. 时间戳 --> 时间字符串

1) time 模块


timestamp = time.time()
timestruct = time.localtime(timestamp)
timestr = time.strftime('%Y-%m-%d %H:%M:%S', timestruct)

    

2) datetime 模块

import datetime
timestamp = 1482374997.55
datetime_struct = datetime.datetime.fromtimestamp(timestamp)

print datetime_struct.strftime('%Y-%m-%d %H:%M:%S')

>> 2016-12-22 10:49:57



datetime_struct = datetime.datetime.utcfromtimestamp(timestamp)

print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') 

>> 2016-12-22 02:49:57


fromtimestamp(timestamp[, tz]):将时间戳转为当地的时间元组
utcfromtimestamp(timestamp):将时间戳转为UTC的时间元组。以北京为例:utc时间比北京当地时间少8个小时。


 
3. 时间差计算
 

def preday_get(d):

    """    

    返回上一天的日期时间

    :return

    date_from: 2017-05-01 00:00:00

    date_to: 2017-05-01 23:59:59

    """

    oneday = datetime.timedelta(days=1)

    day = d - oneday

    date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)

    date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)

#     print '---'.join([str(date_from), str(date_to)])

    return date_from, date_to


def preweek_get(d):

    """    

    返回上一周的日期时间

    :return

    date_from: 2017-05-01 00:00:00

    date_to: 2017-05-07 23:59:59

    """

    dayscount = datetime.timedelta(days=d.isoweekday())

    dayto = d - dayscount

    sixdays = datetime.timedelta(days=6)

    dayfrom = dayto - sixdays

    date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day,0, 0, 0)

    date_to = datetime.datetime(dayto.year, dayto.month, dayto.day,23, 59, 59)

#     print '---'.join([str(date_from), str(date_to)])

    return date_from, date_to


def premonth_get(d):

    """    

    返回上个月第一个天和最后一天的日期时间

    :return

    date_from: 2017-04-01 00:00:00

    date_to: 2017-04-30 23:59:59

    """

    dayscount = datetime.timedelta(days=d.day)

    dayto = d - dayscount

    date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0)

    date_to = datetime.datetime(dayto.year, dayto.month, dayto.day,23, 59, 59)

#     print '---'.join([str(date_from), str(date_to)])

    return date_from, date_to


def nextmonth_get(d):

    """    

    返回下个月第一个天

    :return  date: 2017-06-01 00:00:00

    """

    date = datetime.datetime(d.year, d.month+1,1, 0, 0, 0)

#     print '---'.join([str(date_from), str(date_to)])

    return date


def nextweek_get(d):

    """    

    返回下周第一个天

    :return  date: 2017-06-01 00:00:00

    """

    dayscount = datetime.timedelta(days=d.isoweekday())

    dayfrom = d - dayscount

    stepdate = datetime.timedelta(days=8)

    dayto = dayfrom + stepdate

    date = datetime.datetime(dayto.year, dayto.month, dayto.day, 00, 00, 00)

    return date



 


4. 任意时间字符串转换时间对象

import time
from dateutil import parser
 
time_string = time.ctime() # 'Thu Dec 22 10:35:25 2016',这里可以是任意的时间格式
datetime_struct = parser.parse(time_string)
print type(datetime_struct) # <type 'datetime.datetime'>
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 13:58:59

原创粉丝点击