Python之 time 模块
时间模块的转换关系与方式:


#!/usr/bin/env python
# -*- coding:utf8 -*-
import time # 时间戳
print('\ntime.time() -->',time.time()) # 1557393149.3275213(秒)
'''
* 是从1970年开始算到现在的秒数
1970年1月1日凌晨0点开始算 * 可以根据得到的值推出对应的年份时间 * 用于 计算
''' # * * * * * * * * * * # 结构化时间 # * * * * * * * * * * * * * * #
# 当地时间
print('\n括号内无参数时==》',time.localtime())
# 输出该秒数所对应的当地的具体时间
print('括号内有参数时==》',time.localtime(1457393149))
# 输出对应的时间的某个元素,如年份,星期几
t = time.localtime()
print('\nt.tm_year -->',t.tm_year)
print('t.tm_wday -->',t.tm_wday)
'''
struct_time元组的元素
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
索引 属性 值
0 tm_year(年) 比如 2011
1 tm_mon(月) 1 - 12
2 tm_day(日) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min(分) 0 - 59
5 tm_sec(秒) 0 - 61
6 tm_wday(weekday) 0 - 6 (0表示周日)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否为夏令时) 默认为 -1
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
'''
# * * * * * * * * * * * * * * * * * * * * * * * * #
# 世界标准时间 UTC
print('\ntime.gmtime() -->',time.gmtime())
# * * * * * * * * * * * * * * * * * * * * * * * * #
# 将结构化时间转换成时间戳
print('\ntime.mktime(time.localtime()):')
print(time.mktime(time.localtime()))
'''
# 结构化时间 和 时间戳 的相互转换方式:
* 时间戳(1457393149) ==> 结构化时间
print(time.localtime(1457393149))
* 结构化时间 ==》 时间戳
print(time.mktime(time.localtime()))
# 意义:
你并不能知道你到时候能取到结构化时间还能取到一个时间戳
你并不知道你会拿到什么
但有一点是肯定的:
如果学到了这几点的相互转换的方式,
它给我什么,我都能拿到让它变成我想要的东西
'''
# * * * * * * * * * * * * * * * * * * * * * * * * #
# 将 结构化时间 转成 字符串时间
'''
# 格式固定
%Y ==> 年
%m ==> 月
%d ==> 日 %X ==> 时 分 秒 %H ==> 时
%M ==> 分
%S ==> 秒 %a ==> 星期(英文缩写)
%b ==> 月(英文缩写) '''
print('\ntime.strftime("%Y-%m-%d %X",time.localtime()):')
print(time.strftime("%Y-%m-%d %X",time.localtime())) # 间隔的符号可以自己定义
# 将 字符串时间 转成 结构化时间
# 第一个参数是 字符串时间, 第二个参数是 字符串的结构
'''
# 格式固定
%Y ==> 年
%m ==> 月
%d ==> 日 %X ==> 时 分 秒 %H ==> 时
%M ==> 分
%S ==> 秒 %a ==> 星期(英文缩写)
%b ==> 月(英文缩写) '''
print("\ntime.strptime('2018:12:23:15:50:24','%Y:%m:%d:%X'):")
print(time.strptime('2018:12:23:15:50:24','%Y:%m:%d:%X'))
# * * * * * * * * * * * * * * * * * * * * * * * * # # 输出 时间字符串
# *注意:他们的输出的时间格式固定排好了,不能动
# --》 %a %b %d %H:%M:%S %Y 串
print("\ntime.asctime() -->",time.asctime())
print('time.ctime() -->',time.ctime()) # 程序 睡眠
# 线程推迟的时间运行,单位为 秒
# time.sleep(1) # 让程序睡眠 1 秒后继续进行 # clock()
# 不怎么常用
'''
注意:
* 在不同的系统上含义不同。
在 UNIX 系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间差)
在 WINDOWS 中,第一次调用,返回的是进程运行的实际实际,
而第二次之后的调用是自第一次调用以后到现在的运行时间,即两次时间差
''' import datetime
print('datetime.datetime.now() -->',datetime.datetime.now())
Python之 time 模块的更多相关文章
- python之platform模块
python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)
一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...
- linux下python调用c模块
在C调用Python模块时需要初始化Python解释器,导入模块等,但Python调用C模块却比较简单,下面还是以helloWorld.c 和 main.py 做一说明: (1)编写C代码,hel ...
- Python学习之模块进程函数详解
今天在看<Beginning Linux Programming>中的进程相关部分,讲到Linux几个进程相关的系统函数: system , exec , fork ,wait . Pyt ...
- python基础——第三方模块
python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的. 如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了. 如果你正在使用Window ...
- python基础——使用模块
python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...
- python 中time模块使用
在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主 ...
- Python之logging模块
一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...
随机推荐
- LeetCode 1139. Largest 1-Bordered Square
原题链接在这里:https://leetcode.com/problems/largest-1-bordered-square/ 题目: Given a 2D grid of 0s and 1s, r ...
- Time of Trial
Time of Trial(思维) 题目大意:一个人想逃,一个人要阻止那个人逃跑 ,阻止者念每一个符咒的时间为b[i],逃跑者念每一个符咒的时间为a[i],逃跑者如果念完了第i个符咒,阻止者还没有念完 ...
- javascript 中的方法注入
js 中的方法注入 java中很多框架支持 apo 的注入, js中也可以类似的进行实现 主要是通过扩展js中方法的老祖 Function 对象来进行实现. Function.prototype.af ...
- OpenFOAM——设置自定义非均匀场区域
在使用OpenFOAM进行计算的时候,我们需要对计算域设置非均匀场,比如最典型的溃坝算例,在开始计算以前,我们需要首先设定某一区域的水的体积分数为1,就是下面这样的: 有可能我们在计算传热问题的时候, ...
- 可变参数的函数(c++)【转载】
摘自<c语言精彩编程百例>,要定义可变参数的函数,在c++当中当包含<cstdarg>,在c语言当中当包含<stdarg.h>,使用任何可变长度的变元被访问之前,必 ...
- #C++初学记录ACM补题(D. Candies!)前缀和运算。
D - Candies! Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...
- Goroutine调度器
前言 并发(并行)一致都是编程语言的核心主题,不同于其他语言,例如C/C++语言用户序自行借助pthread创建线程,Golang天然就给出了并发解决方案:goroutine. Goroutine 写 ...
- 自己搭建gitlab服务,组员不能上传代码
原因是因为 没有拉分支 直接在master 上开撸代码 ,master 分支 默认是受保护的,具体操作如下
- SQL server 表结构转Oracle SQL脚本
SQL server 表结构转Oracle SQL脚本 /****** Object: StoredProcedure [dbo].[getOracle] Script Date: 2019/7/25 ...
- Spring Boot方式的Dubbo项目
项目依赖 需要org.apache.dubbo.dubbo-dependencies-bom, 需要org.apache.dubbo.dubbo-spring-boot-starter, 当前版本有2 ...