Python——time模块&datetime模块

来源:互联网 发布:大学生网络受骗案例 编辑:程序博客网 时间:2024/05/16 17:32

python中有一个时间模块time,它使用的是Unix timestamps.不明显显示年月日,而是一个浮点数,表示从1970到现在所经历的秒数。利用time()函数可以获得当前这个Unix timestamps时间。

import timecurrent_time = time.time()'''current_time  : 1462328968.143729'''

Converting Timestamps

由于Unix timestamps可读性很差,因此需要将其转换为我们常见的时间表达,利用gmtime()可以转换过来,gmtime()函数返回一个struct_time 实例,一个struct_time 实例包含以下几个属性:

  • tm_year: The year of the timestamp
  • tm_mon: The month of the timestamp (1-12)
  • tm_mday: The day in the month of the timestamp (1-31)
  • tm_hour: The hour of the timestamp (0-23)
  • tm_min: The minute of the timestamp (0-59)
import timecurrent_time = time.time()current_struct_time = time.gmtime(current_time)current_hour = current_struct_time.tm_hourprint(current_struct_time )'''time.struct_time(tm_year=2016, tm_mon=5, tm_mday=4, tm_hour=2, tm_min=33, tm_sec=24, tm_wday=2, tm_yday=125, tm_isdst=0)'''

UTC

UTC(Coordinated Universal Time)表示世界标准时间 ,是最主要的世界时间标准,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间。因此上面的结果看起来并不是本地的时间,需要将其转换为本地时间。datetime模块对时间提供了一个更好的支持,它有一个datetime类,这个类的属性与struct_time 相似:

import datetimecurrent_datetime = datetime.datetime.now()current_year = current_datetime.yearcurrent_month = current_datetime.monthprint(current_datetime)'''2016-05-04 02:39:20.161759'''

Timedelta

  • 现在知道如何显示时间,接下来学习如何进行时间的计算。datetime模块提供了一个timedelta类可以实现时间的计算。可以用weeks,days,hours,minutes等属性实例化一个timedelta对象,然后与当前时间进行运算:
import datetimetoday = datetime.datetime.now()diff = datetime.timedelta(days = 1)tomorrow = today + diffyesterday = today - diff

Formatting Dates

前面打印一个datetime对象显示的时间是:2016-05-04 02:39:20.161759。我们可以让它看起来更清晰,利用datetime对象调用strftime()函数可以将其转化得更清楚。strftime()函数需要带几个参数:

  • %a : 星期几的缩写,比如:Sun, Mon, …, Sat (en_US);So, Mo, …, Sa (de_DE)
  • %A : 星期几的全称,比如:Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE)
  • %w : 星期几的数值表示,0表示Sunday,6表示Saturday
  • %d : 天数,01, 02, …, 31
  • %b : 月份缩写,Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE)
  • %B : 月份全称,January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE)
    ……
import datetime'''mystery_date : datetime.datetime(2015, 12, 31, 0, 0)'''mystery_date_formatted_string = mystery_date.strftime("%I:%M%p on %A %B %d, %Y")print(mystery_date_formatted_string)'''12:00AM on Thursday December 31, 2015'''

Parsing Dates

上面我们讲一个datetime对象转化为一个格式化字符串,我们也可以将格式化字符串转化为datetime对象。datetime.datetime中有一个函数strptime(),它有两个参数:

  • The date string (e.g. “Mar 03, 2010”)
  • The format string (e.g. “%b %d, %Y”)
import datetime'''mystery_date_formatted_string : '12:00AM on Thursday January 02, 2003''''mystery_date = datetime.datetime.strptime(mystery_date_formatted_string, "%I:%M%p on %A %B %d, %Y")print(mystery_date)'''2003-01-02 00:00:00'''

AskReddit Data

Reddit是一个新闻网站,用户可以提交链接,文本的帖子,和其他类型的内容给有相同兴趣爱好的一组人,这些组被称为subreddits,专门从事特定的主题。其中很有名的一个subreddits是AskReddit。同来供用户提问,其他的人在评论区回复这些问题。我们提取了AskReddit2015年的top1000个帖子,帖子的属性有:

  • Title – The title of the post
  • Score – The number of upvotes the post received
  • Time – When the post was posted (timestamp)
  • Gold – How much Reddit Gold was given to the post
  • NumComs – Number of comments the post received

Reformatting Our Data

  • 利用首先datetime.datetime.fromtimestamp函数将帖子中的Unix timestamp时间转换为datetime时间。
import datetimeprint(posts[0])'''['What\'s your internet "white whale", something you\'ve been searching for years to find with no luck?', '11510', '1433213314.0', '1', '26195']'''for row in posts:    day = datetime.datetime.fromtimestamp(float(row[2]))    row[2] = dayprint(posts[0])'''['What\'s your internet "white whale", something you\'ve been searching for years to find with no luck?', '11510', datetime.datetime(2015, 6, 2, 2, 48, 34), '1', '26195']'''

Counting Posts In March

  • 计算三月份总共有多少个帖子:
march_count = 0for row in posts:    if row[2].month == 3:        march_count += 1

Counting Posts In Any Month

march_count = 0def count_posts_in_month(month):    count = 0    for row in posts:        if row[2].month == month:            count += 1    return countfeb_count = count_posts_in_month(2)aug_count = count_posts_in_month(8)
0 0
原创粉丝点击