爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 9893|回复: 8

[混合编程] [已收纳]python之日期和时间

[复制链接]

新浪微博达人勋

发表于 2020-3-2 16:49:57 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 15195775117 于 2021-1-27 11:09 编辑

今天花了将近1天时间研究了下python时间操作,下面的代码主要是研究格式转换、当前时间和运行时间

#时间模块有3个:
#time、datetime、calendar

import time
import datetime
import calendar

#目前所用时间格式有3种:
#1、字符串2020-3-2 15:11:3
#2、浮点型
#3、struct_time
#字符串和struct_time可互相转换
#浮点型和struct_time可互相转换
#浮点型和字符串不可直接转换

#3种时间格式转换,以s代表字符串型,t代表struct_time型,f代表浮点型

#字符串转struct_time型:
s='2020-3-2 15:11:3'
t=time.strptime(s,'%Y-%m-%d %H:%M:%S')
print('\n字符串→结构体:',t)
#结果:time.struct_time(tm_year=2020, tm_mon=3, tm_mday=2,
# tm_hour=15, tm_min=11, tm_sec=3, tm_wday=0, tm_yday=62, tm_isdst=-1)
print('\n结构体时间细节:',
      t.tm_year,'年',
      t.tm_mon,'月',
      t.tm_mday,'日',
      t.tm_hour,'时',
      t.tm_min,'分',
      t.tm_sec,'秒',
      '周',t.tm_wday+1,#从0开始计
      '一年第',t.tm_yday,'天')#从1开始计
#结果:2020 年 3 月 2 日 15 时 11 分 3 秒 周 1 一年第 62 天

#struct_time型转字符串:
s=time.strftime('%Y-%m-%d %H:%M:%S',t)
print('\n结构体→字符串:',s)
#结果:2020-03-02 15:11:03

#struct_time型转浮点型:
f=time.mktime(t)
print('\n结构体→浮点:',f)
#结果:1583133063.0

#浮点型转struct_time:
t=time.localtime(f)
print('\n浮点→结构体:',t)

#UNIX时间戳是以1970年1月1日为计时起点时间的,此为纪元元年
#当前时间,返回值浮点型:
t1=time.time()
#往后1小时:
t2=t1+3600
#PYTHON与IDL时间的区别:
#1、起始时间不同;2、IDL的1单位代表1天,python代表1秒

#获取UTC的当前时间,返回值是time.struct_time型:
t=time.gmtime()
#获取当前时区的时间,返回值是time.struct_time型:
t=time.localtime()

#运行时间与绝对时间:
#使用process_time可以计算真实运行时间,不会计算sleep的时间
runtime1=time.process_time()
absolutetime1=time.time()
time.sleep(3)
runtime2=time.process_time()
absolutetime2=time.time()
print('\n运行时间=',runtime2-runtime1)
print('绝对时间=',absolutetime2-absolutetime1)
#结果:
#运行时间= 0.0
#绝对时间= 3.0109901428222656


本帖被以下淘专辑推荐:

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2020-3-2 17:20:12 | 显示全部楼层
感谢分享!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2020-3-9 09:49:06 | 显示全部楼层
淘贴了,谢谢楼主整理分享
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 成长值: 0
发表于 2020-3-11 15:53:36 | 显示全部楼层
适合初学者,谢谢!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2020-3-11 20:06:06 | 显示全部楼层
推荐个arrow的包,用起来比datetime要方便
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-6-3 23:33:57 | 显示全部楼层
本帖最后由 15195775117 于 2020-10-28 17:17 编辑

import time
import datetime

string_time='2020-6-3 22:28:00'#字符串形式
#dict_time字典形式
#float_time浮点形式

dict_time=time.strptime(string_time,'%Y-%m-%d %H:%M:%S')
print(dict_time)
#输出:time.struct_time(tm_year=2020, tm_mon=6, tm_mday=3,
#tm_hour=22, tm_min=28, tm_sec=0, tm_wday=2, tm_yday=155, tm_isdst=-1)

del string_time#删除变量

#字典型转字符串
string_time=time.strftime('%Y-%m-%d %H:%M:%S',dict_time)
print(string_time)
#输出:2020-06-03 22:28:00

#时间段的表示:
print(datetime.timedelta(seconds=1))
#输出:0:00:01
print(datetime.timedelta(minutes=1))
#输出:0:01:00
print(datetime.timedelta(hours=1))
#输出:1:00:00
print(datetime.timedelta(days=1))
#输出:1 day, 0:00:00
print(datetime.timedelta(weeks=1))
#输出:7 days, 0:00:00

#以上的表示形式是结构化的,如果想化为秒数,可以使用它们的方法total_seconds(),例如:
print(datetime.timedelta(hours=1).total_seconds())

#日期加减
today=datetime.date.today()
print('今天:',today)
#输出:今天: 2020-06-03

oneday=datetime.timedelta(days=1)
tomorrow=today+oneday
print('明天是',tomorrow)
#输出:明天是 2020-06-04

fourdaysago=today-datetime.timedelta(days=4)
print('4天前是',fourdaysago)
#输出:4天前是 2020-05-30

print('时间差=',tomorrow-fourdaysago)
#输出:时间差= 5 days, 0:00:00
print('时间差(秒)=',(tomorrow-fourdaysago).total_seconds())
#输出:432000.0

密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-6-4 09:30:01 | 显示全部楼层
本帖最后由 15195775117 于 2020-10-28 17:17 编辑

import datetime
print(datetime.date(2020,9,6))
#输出:2020-09-06
print(datetime.time(12,3,45))
#输出:12:03:45
print(datetime.datetime(2020,9,6,12,3,45))
#输出:2020-09-06 12:03:45
#日期时间比较:
print(datetime.date(2020,9,6) > datetime.date(2020,9,7))
#输出:False
print(datetime.time(12,3,45) > datetime.time(12,3,42))
#输出:True
print(datetime.datetime(2020,9,6,12,3,45) >
      datetime.datetime(2020,8,6,12,3,45))
#输出:True
today=datetime.date.today()#今天
now=datetime.datetime.now()#现在
print('现在时间:',now)
#输出:现在时间: 2020-06-04 09:26:13.790593
print('现在世界时:',datetime.datetime.utcnow())
#输出:现在世界时: 2020-06-04 01:26:13.790593
print('今天:',today)
#输出:今天: 2020-06-04
print('数据类型:',type(today))
#输出:数据类型: <class 'datetime.date'>
#分离年月日:
print(today.year,today.month,today.day)
#输出:2020 6 4
print(now.year,now.month,now.day,now.hour,now.minute,now.second)
#输出:2020 6 4 9 26 13


format='%a %b %d %H:%M:%S %Y'
today=datetime.datetime.today()
print(today)
#输出:2020-06-04 09:26:13.790593
str_time=today.strftime(format)#datetime转字符串
print(type(str_time))
#输出:<class 'str'>
#字符串转字典/datetime:
dict_time=datetime.datetime.strptime(str_time,format)
print(type(dict_time))
#输出:<class 'datetime.datetime'>
print(dict_time.day)
#输出:4

密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-6-4 09:48:32 | 显示全部楼层
本帖最后由 15195775117 于 2020-10-28 17:17 编辑

import calendar
#calendar的主要是采用不同格式打印日历
#还可以计算每年母亲节和感恩节的日期
c=calendar.TextCalendar(calendar.MONDAY)#以周一为一周的开始
c.prmonth(2020,6)
'''
输出:
     June 2020
Mo Tu We Th Fr Sa Su
1  2  3  4  5  6  7
8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
'''

密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-12-11 17:04:23 | 显示全部楼层
时间字符串常用格式示例

import
time
s='2020-12-11 17:12:3'
t=time.strptime(s,'%Y-%m-%d %H:%M:%S')
print('\n字符串结构体:',t)
#结果:time.struct_time(tm_year=2020, tm_mon=3, tm_mday=2,
# tm_hour=15, tm_min=11, tm_sec=3, tm_wday=0, tm_yday=62, tm_isdst=-1)
print('\n结构体时间细节:',
      t.tm_year,'',
      t.tm_mon,'',
      t.tm_mday,'',
      t.tm_hour,'',
      t.tm_min,'',
      t.tm_sec,'',
      '',t.tm_wday+1,#0开始计
'一年第',t.tm_yday,'')#1开始计
#结果:2020 3 2 15 11 3 秒 周 1 一年第 62
#struct_time型转字符串:
s=time.strftime('%Y-%m-%d %H:%M:%S 星期%A 一周第%w(0为周日) 月份%B 全年第%j天 全年第%U',t)
print('\n结构体字符串:',s)



结果:
2020-12-11 17:12:03 星期Friday 一周第5天(0为周日) 月份December 全年第346天 全年第49周


密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表