Python-datetime、time包常用功能汇总_python yyyy-mm-ddthh mm ssz

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注网络安全)
img

正文

08/16/88 (None);

08/16/1988 (en_US);

16.08.1988 (de_DE)

| (1) |
| %X | 本地化的适当时间表示。 |

21:30:00 (en_US);

21:30:00 (de_DE)

| (1) |
| %% | 字面的 '%' 字符。 | % | |

时间戳

时间戳一般指的是Unix时间/POSIX时间,从1970-01-01 00:00:00到当前的秒数,一般使用10位13位表示

datetime

date

属性(只读)

  • year:int类型,如2023
  • month:int类型,如2
  • day::int类型,如21

方法

  • today:返回当前日期,转字符串使用iso格式,例如 2023-02-21
  • fromtimestamp:从时间戳返回当地时间
  • replace:返回一个date,可以通过参数修改year month day,会进行检查,不符合抛异常ValueError
  • strftime:按照格式返回字符串

datetime

属性(只读)

  • year:int类型,如2023
  • month:int类型,如2
  • day:int类型,如21
  • hour:int类型,小时
  • minute:int类型,分钟
  • second:int类型,秒
  • microsecond:int类型,毫秒

方法

  • today、now:返回本地当前日期时间,例如,2023-02-21 21:09:07.915277
  • utcnow:返回utc时间,即北京时间的8个小时前
  • timestamp
  • fromtimestamp:从时间戳获取本地时间
  • utcfromtimestamp:从时间戳获取utc时间
  • date:返回日期对象
  • time:返回时间对象
  • replace:返回一个datetime对象,可通过参数修改,会进行检查,不符合抛异常ValueError
  • strftime:按照格式返回字符串
  • strptime:返回一个对应于 date_string,根据 format 进行解析得到的 datetime 对象。

timedelta

timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) 用于表示两个时间的间隔
用这个就比较方便,比如,计算昨天,自己写的话还要考虑月份的天数,这个直接减去即可

time

方法

  • time:返回时间戳,float类型
  • localtime:获得时间元组,secs参数可选,接受float
  • mktime:这是 localtime() 的反函数,时间参数t,返回对应的一个float值
  • sleep:程序睡眠,secs参数代表睡眠秒数
  • strftime:返回指定format格式的字符串,t参数可选,默认为localtime返回的值
  • strptime:将string按照指定格式解析,返回一个时间元组

常用

我们常常收到和发送的都是字符串,这里就以字符串处理为例

获取今天凌晨字符串?

def get_today_format(format="%Y-%m-%d 00:00:00"):
    str_time = datetime.today().strftime(format)
    return str_time

默认返回今天凌晨的字符串,可以修改格式

将一个时间格式的字符串转为时间戳

def get\_time\_stamp(str_time,format="%Y-%m-%dT%H:%M:%SZ") -> float:
    s_t = time.strptime(str_time,format)
    mkt = time.mktime(s_t) \* 1000
    return mkt

def get\_date\_time\_stamp(str_time,format="%Y-%m-%dT%H:%M:%SZ") -> float:
    date_time = datetime.strptime(str_time,format)
    time_stamp = date_time.timestamp()\*1000
    return time_stamp

将一个时间戳转为指定格式的字符串

def get\_str\_by\_timestamp(timestamp=time.time(),format="%Y-%m-%d %H:%M:%S"):
    time_tuple = time.localtime(float(timestamp))
    str_time = time.strftime(format,time_tuple)
    return str_time

将一个utc字符串转为本地时间格式字符串(2023-04-01新增)

def utc\_str\_to\_local\_str(utc_str:str,utc_format="%Y-%m-%dT%H:%M:%SZ",local_format="%Y-%m-%d %H:%M:%S") -> str:
    utc_time = datetime.strptime(utc_str,utc_format)
    utc_time += timedelta(hours=8)
    local_str = datetime.strftime(utc_time,local_format)
    return local_str

全部代码

from datetime import date,datetime,timedelta
import time

local_format = "%Y-%m-%d %H:%M:%S"
utc_format = "%Y-%m-%dT%H:%M:%SZ"
chinese_format = "%Y年%m月%d日 %H时%M分%S秒"


def date\_test():
    year,month,day = date.today().year,date.today().month,date.today().day
    print(type(year),year,month,day)
    today = date.today()
    print(type(today), str(today))
    timestamp = date.today().fromtimestamp(datetime.now().timestamp())
    print(timestamp)
    today_but_28 = date.today().replace(day=28)
    print(today_but_28)
    today_format =  today.strftime("%Y/%m/%d")
    print(today_format)
    date_time_format = datetime.strptime(today_format,"%Y/%m/%d")
    print(date_time_format)

def datetime\_test():
    today,now = datetime.today(),datetime.now()
    print(type(today),today,now)
    year,month,day= now.year,now.month,now.day
    hour, minute, second = now.hour,now.minute,now.second
    print(type(year),year,month,day)
    print(type(hour),hour,minute,second)
    utcnow = datetime.utcnow()
    print(utcnow)
    date,time = now.date(),now.time()
    print(type(date),date)
    print(type(time),time)
    today_but_28 = now.replace(day=28,minute=59)
    print(today_but_28)
    today_format = now.strftime("%Y/%m/%d %H:%M:%S")
    print(today_format)

def time\_delta\_test():
    one_day,one_hour = timedelta(days=1),timedelta(hours=1)
    yesterday_same_time = datetime.now() - one_day
    one_hour_later = datetime.now() + one_hour
    print(yesterday_same_time)
    print(one_hour_later)

def time\_test():
    time_stamp_float = time.time()
    print(time_stamp_float)
    s = time.localtime(time_stamp_float)
    print(s)
    time_float = time.mktime(s)
    print(time_float)
    local_str = time.strftime(local_format)
    print(local_str)
    default_format_time = time.strptime(local_str,local_format)
    print(default_format_time)
    time.sleep(1)


def get\_today\_format(format="%Y-%m-%d 00:00:00"):
    str_time = datetime.today().strftime(format)
    return str_time


def get\_time\_stamp(str_time,format="%Y-%m-%dT%H:%M:%SZ") -> float:
    s_t = time.strptime(str_time,format)
    mkt = time.mktime(s_t) \* 1000
    return mkt

def get\_date\_time\_stamp(str_time,format="%Y-%m-%dT%H:%M:%SZ") -> float:
    date_time = datetime.strptime(str_time,format)
    time_stamp = date_time.timestamp()\*1000
    return time_stamp

def get\_str\_by\_timestamp(timestamp=time.time(),format="%Y-%m-%d %H:%M:%S"):
    time_tuple = time.localtime(float(timestamp))
    str_time = time.strftime(format,time_tuple)
    return str_time

def utc\_str\_to\_local\_str(utc_str:str,utc_format="%Y-%m-%dT%H:%M:%SZ",local_format="%Y-%m-%d %H:%M:%S") -> str:
    utc_time = datetime.strptime(utc_str,utc_format)
    utc_time += timedelta(hours=8)
    local_str = datetime.strftime(utc_time,local_format)
    return local_str


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)**
![img](https://img-blog.csdnimg.cn/img_convert/e4e2b97a376c98b79800b6b3603935e3.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

   return local_str


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)**
[外链图片转存中...(img-HXamUmdu-1713394137311)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值