1.time模块

时间戳:1970年,1月1日开始
时间元祖:包含日期,时间,保存日期结构的元祖对象
格式化时间日期:按照指定的标记进行格式化处理

时间戳

import time

time_num = time.time()                 # 获取时间戳
time_tuple = time.localtime(time_num) # 时间戳转为时间元祖
res_res = time.mktime(time_tuple) # 时间元祖转换回时间戳
format_time= time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) #日期格式化
res_tuple_time = time.strptime(format_time, '%Y-%m-%d %H:%M:%S') print("时间戳:",time_num)
print("时间戳转为时间元祖:",time_tuple)
print("时间元祖转为时间戳:",res_res)
print("格式化日期显示:", format_time)
print("格式化时间转回时间元祖:", res_tuple_time)
"""
时间戳: 1629636166.1266477
时间戳转为时间元祖: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=22, tm_hour=20, tm_min=42, tm_sec=46, tm_wday=6, tm_yday=234, tm_isdst=0)
时间元祖转为时间戳: 1629636166.0
格式化日期显示: 2021-08-22 20:42:46
格式化日期转回时间元祖: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=22, tm_hour=20, tm_min=42, tm_sec=46, tm_wday=6, tm_yday=234, tm_isdst=-1)
""" print("格式化日期显示-只显示日期:", time.strftime("%F", time.localtime()))
print("格式化日期显示-只显示时间:", time.strftime("%T", time.localtime()))
"""
格式化日期显示-只显示日期: 2021-08-22
格式化日期显示-只显示日期: 20:47:15
"""

时间戳的应用

import time
res = time.time()
print(res) #获取时间戳:1629537871.082874 #时间戳的使用,计算程序执行时间
def sum():
s = 0
for i in range(29999999):
s += i
return s
def main():
qidong1 = time.time()
cpu_time1= time.process_time()
sum()
qidong2 = time.time()
cpu_time2 = time.process_time()
print("【程序CPU耗时统计】:{}秒".format(cpu_time2-cpu_time1))
print("【程序计算耗时统计】:{}秒".format(qidong2-qidong1))
main()
"""
1629538723.3552346
【程序CPU耗时统计】:1.859375秒
【程序计算耗时统计】:1.8734791278839111秒
"""

2.日期模块 calendar

import calendar

calendar.setfirstweekday(calendar.SUNDAY) #设置一周的开始周几,默认周一

print(calendar.calendar(2021))   # 显示年历
print(calendar.month(2021, 8)) # 显示月历,返回结果可看出不准,8月第一天是星期日,不是周六
print(calendar.monthrange(2021, 8)) #返回该年月的:第一天是星期几,一个月的天数【实测不准!!】
print(calendar.isleap(2020)) # 判断是否闰年
print(calendar.leapdays(2012,2020)) #判断俩年份间的闰年个数,包头不包尾
"""
年历代码不展示 Su Mo Tu We Th Fr Sa
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 31 (6, 31)
True
2
"""

3.模块datetime

datetime.date日期

from datetime import date

print("=======datetime.date日期操作演示===========")
print(date.today()) #获取今天的日期
res = date(2021,9,17)
print(res.weekday()) #获取指定日期,是星期几
print(res.isoformat()) #获取指定日期,格式化
"""
=======datetime.date日期操作演示===========
2021-08-22
4
2021-09-17
"""

datetime.time时间

from datetime import time   # 和time.time模块不能同时导入
from datetime import datetime
from datetime import timedelta
time_str = time(23, 59, 59)
print("获取指定时间为:{}时{}分{}秒".format(time_str.hour, time_str.minute, time_str.second ))
date_res = datetime(2021, 8, 22, 22, 54, 27) #只需要传入年月日时分秒
print("获取指定日期时间:", date_res)
print("=======datetime.timedelta时间操作演示===========")
print("再加X小时,实际日期为:",date_res + timedelta(hours=30))
print("再加上X天,实际日期为:",date_res + timedelta(days=20))
print("再减去X天,实际日期为:",date_res - timedelta(days=20))
"""
获取指定时间为:23时59分59秒
获取指定日期时间: 2021-08-22 22:54:27
=======datetime.timedelta时间操作演示===========
再加X小时,实际日期为: 2021-08-24 04:54:27
再加上X天,实际日期为: 2021-09-11 22:54:27
再减去X天,实际日期为: 2021-08-02 22:54:27
"""

atetime.tzinfo时区

from datetime import tzinfo
from datetime import datetime
from datetime import timedelta # 继承tzinfo,覆写方法
class UTC(tzinfo):
def __init__(self, offset=0):
self.__offset = 0
def tzname(self, dt):
return "UTC时区:".format(self.__offset)
def utcoffset(self, dt):
return timedelta(hours=self.__offset)
def dst(self, dt):
return timedelta(hours=self.__offset)
if __name__ == '__main__':
china_date = datetime(2021, 8, 22, 22, 54, 27, tzinfo=UTC(8))
gemena_date = datetime(2021, 8, 22, 22, 54, 27, tzinfo=UTC(1))
print("=======datetime.tzinfo时区演示===========")
print("北京时间:", china_date)
print("德国时间:", gemena_date)
print("北京转德国时间:", china_date.astimezone(UTC(1)))
print(china_date - gemena_date) """
=======datetime.tzinfo时区演示===========
北京时间: 2021-08-22 22:54:27+00:00
德国时间: 2021-08-22 22:54:27+00:00
北京转德国时间: 2021-08-22 22:54:27+00:00
0:00:00
"""
 

Python入门-系统模块time的更多相关文章

  1. Python 入门之 模块

    Python 入门之 模块 1.模块 (1)模块是什么? ​ 将一些常用的功能封装到一个文件中,那么这个存储着很多常用的功能的py文件,就是模块. 模块就是文件,存放一堆常用的函数.模块,就是一些常用 ...

  2. python 入门学习---模块导入三种方式及中文凝视

    Python 有三种模块导入函数 1. 使用import 导入模块 import modname : 模块是指一个能够交互使用,或者从还有一Python 程序訪问的代码段.仅仅要导入了一个模块,就能够 ...

  3. Python 常用系统模块整理

    Python中的常用的系统模块中部分函数等的整理 random: 随机数 sys: 系统相关 os: 系统相关的 subprocess: 执行新的进程 multiprocessing: 进程相关 th ...

  4. python入门之模块

    序什么是包包的作用一.什么是模块二.模块的三种来源三.模块的四种表现形式四.为什么要使用模块五. 如何创建,编写模块,并使用模块5.1 给模块起别名5.2 模块的导入方式5.3 循环导入问题 TOC ...

  5. Python入门-pip模块管理工具

    安装 # 在线安装 pip install <包名> 安装后,该模块文件会在安装python环境目录:lib/packages目录下 # 安装本地安装包 pip install <目 ...

  6. Python入门-常用模块

    1.sys,os import sys import os #获取当前的路径 print(sys.path[0]) print(os.getcwd()) print(os.path.abspath(& ...

  7. [笔记] Python入门---time模块

    #__author:Mifen #date: 2018/12/6 import time ''' 时间戳是一种用于表示时间的方式.从1970年1月1日0时0分0秒0毫秒开始到指定时间的秒数.世间戳也叫 ...

  8. python系统模块

    Python中大多数系统接口都集中在两个模块:sys和os.这么说有点过于简单化 还有一些其他的表转模块也属于这个领域他们包括: glob 用于文件名的扩展 socket 用于网络连接和进程间通信(I ...

  9. Python基础入门-os模块

    今天我们来介绍一下os模块中常用的一些方法,当然python中的os模块中提供的使用方法有很多,但是这里面小编会列举出来一些和实际工作中应用的相关的方法,而且会有一些实际的例子方便大家对os模块理解. ...

随机推荐

  1. vue2版本中slot的基本使用详解

    前言 在vue的开发过程中,我们会经常使用到vue的slot插槽组件,vue官方文档的描述: Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案 ...

  2. VMware15安装 CentOS7 步骤

  3. 初学者都能学会的ElasticSearch入门实战

    大家好,我是咔咔 不期速成,日拱一卒 项目中准备使用ElasticSearch,之前只是对ElasticSearch有过简单的了解没有系统的学习,本系列文章将从基础的学习再到深入的使用. 咔咔之前写了 ...

  4. [SPDK/NVMe存储技术分析]002 - SPDK官方介绍

    Introduction to the Storage Performance Development Kit (SPDK) | SPDK概述 By Jonathan S. (Intel), Upda ...

  5. Badger简单使用

    Badger简介 badger 是 dgraph 开源的 LSMTree 的 KV 引擎,它相比 leveldb 有 KV 分离.事务.并发合并等增强,是 go 生态中比较生产级的存储引擎了. 文档: ...

  6. Github使用指南(学习中随时更新)

    注册好一个账号后先创建一个仓库 点击"Create repository"创建一个版本库 填好带*号的必填项,选择是要公开仓库还是私人使用,勾选自动添加README选项 READM ...

  7. MAVEN setting文件

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  8. @SpringBootApplication注释在内部有什么用处?

    作为Spring引导文档,@SpringBootApplication注释等同于同时使用@Configuration.@EnableAutoConfiguration和@ComponentScan及其 ...

  9. 简述在 MySQL 数据库中 MyISAM 和 InnoDB 的区别 ?

    MyISAM: 第 134 页 共 485 页不支持事务,但是每次查询都是原子的: 支持表级锁,即每次操作是对整个表加锁: 存储表的总行数: 一个 MYISAM 表有三个文件:索引文件.表结构文件.数 ...

  10. 怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的 字符串?

    String s1 = "你好"; String s2 = new String(s1.getBytes("GB2312"), "ISO-8859-1 ...