python学习之路(常用系统模块1--时间模块)
2017-12-21 16:42
567 查看
python学习之路(常用系统模块1–时间模块)
导入方法
import os # 直接导入 import os,sys # 多个中间用逗号隔开 #--------------------------------------- # 用from 导入是直接把里面的代码直接到入到这个文件中 from test2 import * # 用* 是将方法全部导入进来 ----不建议使用---- from test2 import hellos # 这样就是只导入一个方法。建议用这种方式。避免冲突 hellos() # 所以里面的方法可以直接调用。 # import 导入的 需要以导入名称为前缀去调用。 from test2 import hellos as test2_hellos # 把导入进来的方法加别名
import 文件名 :就是将这个文件全部进行封装,然后赋值给这个文件名
from 文件名 import 方法名 这个是直接将这个代码导入到当前文件
package 包其实就是一个目录,但是下面包含一个
__init__.py文件
import 包名 就是要解释包下面的
__init__.py的文件
在本级不能直接import父级目录,具体import方式,看
http://blog.csdn.net/tianrun1110/article/details/78856188
一般情况下,如果自己的代码调用,用from比较好。可以省去加前面的前缀
模块的分类
模块分为:1. 标准库,就是python自带的,比如:os,sys
2. 开源模块,别人写的,开源了,可以拿来用
3. 就是自己定义的。(都是废话。。。。)
时间模块(time,datetime)
时间的表示形式1. 普通的字符串形式
'2017-12-22 00:17:22'
2. 时间戳
1513873215.5972586,这就是当前的时间戳,就是由
'1970-01-01 00:00:00'到现在的秒值
3. 元组(struct_time)有九个因素(tm_year=2017, tm_mon=12, tm_mday=22, tm_hour=0, tm_min=32, tm_sec=51, tm_wday=4, tm_yday=356, tm_isdst=0)解释:(年,月,日,时,分,秒,一周的第几天,一年的第几天,时区)
官方解释9个元素 The other representation is a tuple of 9 integers giving local time. The tuple items are: year (including century, e.g. 1998) month (1-12) day (1-31) hours (0-23) minutes (0-59) seconds (0-59) weekday (0-6, Monday is 0) Julian day (day in the year, 1-366) DST (Daylight Savings Time) flag (-1, 0 or 1) If the DST flag is 0, the time is given in the regular time zone; if it is 1, the time is given in the DST time zone; if it is -1, mktime() should guess based on the date and time.
time
以下的方法都需要import time# 获取时间戳 import time time.time() # 1513873215.5972586
# UTC和本地标准时间之间的秒差 >>> time.timezone -28800 >>> 28800/3600 # 秒转化成小时 8.0 # 说明中国比标准时间早8个小时。
>>>time.localtime() # 获取时间元组 time.struct_time(tm_year=2017, tm_mon=12, tm_mday=22, tm_hour=9, tm_min=59, tm_sec=7, tm_wday=4, tm_yday=356, tm_isdst=0)
time.sleep(2) # 睡眠2秒
>>> time.gmtime() # 获取 UTC 的时间 元组 可以跟下面的对比,差8个小时 time.struct_time(tm_year=2017, tm_mon=12, tm_mday=22, tm_hour=2, tm_min=37, tm_sec=32, tm_wday=4, tm_yday=356, tm_isdst=0) >>> time.localtime() time.struct_time(tm_year=2017, tm_mon=12, tm_mday=22, tm_hour=10, tm_min=38, tm_sec=3, tm_wday=4, tm_yday=356, tm_isdst=0) # 取值:time.localtime().tm_year 元组取值方法,key-value的形式
>>> time.mktime(time.localtime()) # 将元组时间变成时间戳 1513910891.0
>>> time.strftime('%Y-%m-%d %H:%M:%S') # 格式化时间 '2017-12-22 10:57:16' >>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) # 将元组时间格式化 '2017-12-22 11:05:17' >>> time.strptime('2017-12-22 11:05:17','%Y-%m-%d %H:%M:%S') # 将字符串转化成元组格式 time.struct_time(tm_year=2017, tm_mon=12, tm_mday=22, tm_hour=11, tm_min=5, tm_sec=17, tm_wday=4, tm_yday=356, tm_isdst=-1)
%Y Year with century as a decimal number. ------------年 %m Month as a decimal number [01,12]. ---------------月[01,12] %d Day of the month as a decimal number [01,31].------------日[01,31] %H Hour (24-hour clock) as a decimal number [00,23].-----------时[00,23] %M Minute as a decimal number [00,59].--------------------分[00,59] %S Second as a decimal number [00,61].----------------秒[00,61] __以下不常用,自己了解一下吧。__ %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM.
>>> time.asctime() # 将 元组 时间转化成下面的格式 'Fri Dec 22 11:18:41 2017' >>> time.asctime(time.localtime()) # 参数为元组 'Fri Dec 22 11:19:12 2017' >>> time.ctime(time.time()) # 将时间戳转化成下面这种格式 'Fri Dec 22 11:29:39 2017' >>> time.ctime() 'Fri Dec 22 11:29:49 2017'
datetime
以下的调用都需要import datetime>>> datetime.datetime.now() # 获取当前时间 datetime.datetime(2017, 12, 22, 11, 36, 4, 429930) >>> print(datetime.datetime.now()) 2017-12-22 11:37:11.093838 >>> print(datetime.datetime.now()+datetime.timedelta(3)) # 时间加3天 timedelta() 参数是以天为单位 2017-12-25 11:39:55.048170 >>> print(datetime.datetime.now()+datetime.timedelta(-3)) # 三天以前 2017-12-19 11:41:16.839893 >>> print(datetime.datetime.now()+datetime.timedelta(hours = 3)) # 三个小时以后 2017-12-22 14:54:28.364319 >>> print(datetime.datetime.now()+datetime.timedelta(minutes = 30)) # 30分钟以后 2017-12-22 12:25:56.177746 >>> date_time = datetime.datetime.now() >>> print(date_time.replace(minute=3,hour=2)) 2017-12-22 02:03:06.576819
相关文章推荐
- python学习之路(常用系统模块4--shutil模块)
- python 学习笔记 13 -- 常用的时间模块之time
- Python 之路 Day5 - 常用模块学习
- python 学习笔记 14 -- 常用的时间模块之datetime
- Python 之路 Day5 - 常用模块学习
- Python3内置模块详解1 (Python 之路 Day5 - 常用模块学习 )
- Python 之路 Day5 - 常用模块学习
- python之路-16-常用模块学习
- python 学习笔记 os模块常用项
- Python模块学习之os常用函数
- python入门学习(1)——脚本(常用系统相关命令)
- Python学习-时间模块
- python自动化运维学习笔记1- psutil系统性能信息模块
- Python+MySQL学习之路:二、MySQLdb常用命令
- python3 常用模块(时间、os路径及文件操作、字符串比较,文件内容比较等模块)
- 【学习ios之路:Objective-C】OC中常用的系统排序方法
- 我的python学习之路----包、模块
- python 常用模块学习记录
- Python常用模块种类的系统介绍及与操作系统相关的调用和操作
- 学习Python必须要知道的常用模块