前言:

环境:python解释器是windows平台3.6.1版本.

Python中主要使用1.元组(struct_time对象) 2.格式化的时间字符串 3.时间戳 这三种形式表示时间.本文主要讲述利用time模块的内置函数进行三种形式互相转化.

from time import *

tuple1 = (2017, 9, 23, 14, 45, 10, 0, 0, 0)

def demo1():
"""
struct_time构造函数
"""
struct_time1 = struct_time(tuple1) # 把9个元素的元组转化为struct_time类的对象
print(type(struct_time1), struct_time1) def demo2():
"""
strftime函数用法:把元组(struct_time类是tuple类的子类)转化为字符串
"""
strftime1 = strftime('%Y-%m-%d %H:%M:%S %A %p %a %z %b %B %I %c')
print(type(strftime1), strftime1)
strftime2 = strftime('%Y-%m-%d', tuple1)
print(type(strftime2), strftime2)
strftime2 = strftime('%Y-%m-%d %H:%M:%S', localtime())
print(type(strftime2), strftime2) def demo3():
"""
演示ctime函数的用法
"""
ctime1 = ctime() # 直接从本地时间的时间戳转换为时间的字符串表示形式
print(type(ctime1), ctime1)
ctime2 = ctime(1500000000) # 直接从时间戳转换为时间的字符串表示形式
print(type(ctime2), ctime2) def demo4():
"""
演示strptime函数的用法
"""
strptime1 = strptime('2017-09-21 15:07:38', '%Y-%m-%d %H:%M:%S')
print(type(strptime1), strptime1)
print(strptime1.tm_yday) def demo5():
"""
演示gmtime和localtime函数的用法
"""
gmtime1 = gmtime(1500000000) # 把时间戳转化为struct_time类的对象(本初子午线时间)
print(type(gmtime1), gmtime1)
gmtime1 = gmtime() # 把当前时间的时间戳转化为struct_time类的对象(本初子午线时间),
print(type(gmtime1), gmtime1)
localtime1 = localtime(1500000000) # 把时间戳转化为struct_time类的对象(本地时间)
print(type(localtime1), localtime1)
print(type(localtime()), localtime()) # 把当前时间的时间戳转化为struct_time类的对象(本地时间) def demo6():
"""
演示mktime函数的用法
"""
mktime1 = mktime(tuple1) # 把元组转化为时间戳
print(type(mktime1), mktime1)
def demo7():
"""
演示time函数的用法
"""
print(time())
print(mktime(localtime()))
demo1()
demo2()
demo3()
demo4()
demo5()
demo6()
demo7()
''' 
strptime和strftime使用的格式字符:
  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)
  • %a 本地简化星期名称
  • %A 本地完整星期名称
  • %b 本地简化的月份名称
  • %B 本地完整的月份名称
  • %c 本地相应的日期表示和时间表示
  • %j 年内的一天(001-366)
  • %p 本地A.M.或P.M.的等价符
  • %U 一年中的星期数(00-53)星期天为星期的开始
  • %w 星期(0-6),星期天为星期的开始
  • %W 一年中的星期数(00-53)星期一为星期的开始
  • %x 本地相应的日期表示
  • %X 本地相应的时间表示
  • %Z 当前时区的名称
  • %% %号本身

输出:(部分输出会因程序运行时的时间而改变)

<class 'time.struct_time'> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=23, tm_hour=14, tm_min=45, tm_sec=10, tm_wday=0, tm_yday=0, tm_isdst=0)
<class 'str'> 2017-09-21 19:16:19 Thursday PM Thu +0800 Sep September 07 Thu Sep 21 19:16:19 2017
<class 'str'> 2017-09-23
<class 'str'> 2017-09-21 19:16:19
<class 'str'> Thu Sep 21 19:16:19 2017
<class 'str'> Fri Jul 14 10:40:00 2017
<class 'time.struct_time'> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=15, tm_min=7, tm_sec=38, tm_wday=3, tm_yday=264, tm_isdst=-1)
264
<class 'time.struct_time'> time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
<class 'time.struct_time'> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=11, tm_min=16, tm_sec=19, tm_wday=3, tm_yday=264, tm_isdst=0)
<class 'time.struct_time'> time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
<class 'time.struct_time'> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=19, tm_min=16, tm_sec=19, tm_wday=3, tm_yday=264, tm_isdst=0)
<class 'float'> 1506149110.0

1505993665.347068
1505993665.0

python之time模块的更多相关文章

  1. python之platform模块

    python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...

  2. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  3. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  4. 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)

    一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...

  5. linux下python调用c模块

    在C调用Python模块时需要初始化Python解释器,导入模块等,但Python调用C模块却比较简单,下面还是以helloWorld.c 和 main.py 做一说明:   (1)编写C代码,hel ...

  6. Python学习之模块进程函数详解

    今天在看<Beginning Linux Programming>中的进程相关部分,讲到Linux几个进程相关的系统函数: system , exec , fork ,wait . Pyt ...

  7. python基础——第三方模块

    python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的.  如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了.  如果你正在使用Window ...

  8. python基础——使用模块

    python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...

  9. python 中time模块使用

    在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主 ...

  10. Python之logging模块

    一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...

随机推荐

  1. Android开发之SoundPool使用详解

    使用SoundPool播放音效 如果应用程序经常播放密集.急促而又短暂的音效(如游戏音效)那么使用MediaPlayer显得有些不太适合了.因为MediaPlayer存在如下缺点: 1) 延时时间较长 ...

  2. 80C51 K1控制D1发光

    #include "reg52.h" typedef unsigned char u8; typedef unsigned int u16; sbit led = P2^0; sb ...

  3. c语言 进程控制---创建进程 vfork()函数

    #include "stdio.h" #include "unistd.h" #include "sys/types.h" int gvar ...

  4. UWP 手绘视频创作工具 “来画Pro” 技术分享系列

    开篇先来说一下我和来画的故事,以及写这篇文章的初衷. 今年年初时,我还在北京,在 Face++,做着人脸识别技术的 Windows 和 Android 端,做着人工智能终将实现世间所有美好的梦.这时的 ...

  5. JavaScrpt笔记之第二天

    JavaScript正则表达式regex 1.郑则表达式是有一个字符序列形成的搜索模式. *语法:/正则表达式主题/修饰符(可选) *正则表达式修饰符:i部分大小写:g全局搜索:m多行匹配 *[abc ...

  6. 第一篇--认识Jmeter

    Jmeter是Apache组织开发的基于Java的压力测试工具,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文件.Java 小服务程序.CGI 脚 ...

  7. JavaScript 30 - 2 学习笔记

    学习JavaScirpt30的笔记! 有意思! 2------->   CSS clock 效果是这样的.... 这是改良过后的 版本.... 话不多说,直接来看代码. 首先是html部分 &l ...

  8. 关于 Go 中 Map 类型和 Slice 类型的传递

    关于 Go 中 Map 类型和 Slice 类型的传递 Map 类型 先看例子 m1: func main() { m := make(map[int]int) mdMap(m) fmt.Printl ...

  9. javascript如何用递归写一个简单的树形结构

    现在有一个数据,需要你渲染出对应的列表出来: var data = [ {"id":1}, {"id":2}, {"id":3}, {&qu ...

  10. 在Windows上使用Ubuntu共享的打印机

    Ubuntu下使用cups共享打印机, 是一种简单易用的方法.CUPS(Common UNIX Printing System,通用Unix打印系统)是Fedora Core3中支持的打印系统,它主要 ...