• python 第三方库 时间 arrow


    arrow简介

    arrow是一个提供了更易懂和友好的方法来创建、操作、格式化和转化日期、时间和时间戳的python库。可以完全替代datetime,支持python2和3。

    安装方法

    官方文档

    用法

    1. 获取当前时间

      # utc 时间
      In [16]: arrow.utcnow()
      Out[16]: <Arrow [2020-04-29T12:16:38.437675+00:00]>
      
      # 本地时间
      In [17]: arrow.now()
      Out[17]: <Arrow [2020-04-29T20:16:53.059585+08:00]>
          
      In [29]: arrow.now("Asia/Shanghai")
      Out[29]: <Arrow [2020-04-29T20:28:09.429144+08:00]>
          
      In [61]: local.ctime()
      Out[61]: 'Wed Apr 29 21:33:39 2020'
      
    2. 获取时间戳

      In [18]: arrow.now().timestamp
      Out[18]: 1588162957
      
      In [19]: arrow.utcnow().timestamp
      Out[19]: 1588162963
      
    3. 切换时区

      In [55]: local = arrow.now().to("Asia/Shanghai")
      
      In [56]: local
      Out[56]: <Arrow [2020-04-29T21:32:07.196767+08:00]>
      
      In [57]: local.year
      Out[57]: 2020
      
    4. 时间戳转化为Arrow对象

      In [37]: arrow.get(1367900664)
      Out[37]: <Arrow [2013-05-07T04:24:24+00:00]>
      
      In [38]: arrow.get(1367900664.123456)
      Out[38]: <Arrow [2013-05-07T04:24:24.123456+00:00]>
      
    5. 格式化时间

      In [20]: arrow.now().format()
      Out[20]: u'2020-04-29 20:23:56+08:00'
      
      In [21]: arrow.now().format("YYYY-MM-DD HH:mm")
      Out[21]: u'2020-04-29 20:24'
      
    6. 转化为Arrow对象

      In [40]: arrow.get("2020-04-29 20:23", "YYYY-MM-DD HH:mm")
      Out[40]: <Arrow [2020-04-29T20:23:00+00:00]>
      
      In [41]: arrow.get("2020-04-29 20:23")
      Out[41]: <Arrow [2020-04-29T20:23:00+00:00]>
          
      In [42]: arrow.get(2020,4,29)
      Out[42]: <Arrow [2020-04-29T00:00:00+00:00]>
      
      In [43]: arrow.get(2020,4,29,20,30)
      Out[43]: <Arrow [2020-04-29T20:30:00+00:00]>
      
      
    7. 时间推移

      t = arrow.now()
      <Arrow [2017-02-01T17:19:19.933507+08:00]>
      
      t.shift(days=-1)  # 前一天
      <Arrow [2017-01-31T17:19:19.933507+08:00]>
      
      t.shift(weeks=-1)  # 前一周
      <Arrow [2017-01-25T17:19:19.933507+08:00]>
      
      t.shift(months=-2) # 前两个月
      <Arrow [2016-12-01T17:19:19.933507+08:00]>
      
      t.shift(years=1)  # 明年
      <Arrow [2018-02-01T17:19:19.933507+08:00]>
      
    8. 时间替换

      In [50]: a = arrow.now()
      
      In [51]: a
      Out[51]: <Arrow [2020-04-29T20:39:12.615185+08:00]>
      
      In [52]: a.replace(hour=4)
      Out[52]: <Arrow [2020-04-29T04:39:12.615185+08:00]>
      
    9. 对象的属性

      >>> a = arrow.now()
      >>> a
      <Arrow [2016-07-04T20:17:09.633154+08:00]>
      >>> a.timestamp
      1467634629
      >>> a.year
      2016
      >>> a.month
      7
      >>> a.day
      4
      >>> a.hour
      20
      >>> a.minute
      17
      >>> a.second
      9
      >>> a.microsecond
      633154
      >>> a.week
      27
      

    Token

    Token Output
    Year YYYY 2000, 2001, 2002 … 2012, 2013
    YY 00, 01, 02 … 12, 13
    Month MMMM January, February, March …
    MMM Jan, Feb, Mar …
    MM 01, 02, 03 … 11, 12
    M 1, 2, 3 … 11, 12
    Day of Year DDDD 001, 002, 003 … 364, 365
    DDD 1, 2, 3 … 4, 5
    Day of Month DD 01, 02, 03 … 30, 31
    D 1, 2, 3 … 30, 31
    Day of Week dddd Monday, Tuesday, Wednesday …
    ddd Mon, Tue, Wed …
    d 1, 2, 3 … 6, 7
    Hour HH 00, 01, 02 … 23, 24
    H 0, 1, 2 … 23, 24
    hh 01, 02, 03 … 11, 12
    h 1, 2, 3 … 11, 12
    AM / PM A AM, PM
    a am, pm
    Minute mm 00, 01, 02 … 58, 59
    m 0, 1, 2 … 58, 59
    Second ss 00, 01, 02 … 58, 59
    s 0, 1, 2 … 58, 59
    Sub-second SSS 000, 001, 002 … 998, 999
    SS 00, 01, 02 … 98, 99
    S 0, 1, 2 … 8, 9
    Timezone ZZ -07:00, -06:00 … +06:00, +07:00
    Z -0700, -0600 … +0600, +0700
    Timestamp X 1381685817
  • 相关阅读:
    Day 31 Event事件/进程池和线程池/高性能爬取梨视频/协程
    scp命令
    [Influxdb]记录
    REMOTE HOST IDENTIFICATION HAS CHANGED!
    【转载】Java异常控制机制和异常处理原则
    html转成PDF,替换内容
    SOAP请求-示例
    InputStream转成String
    [集合操作]List对象数组获取元素值非空对象及根据对象元素值排序取最大&取对象数组的对象元素集合&条件去重&条件分组
    maven deploy jar包到远程仓库400
  • 原文地址:https://www.cnblogs.com/hiyang/p/12805008.html
Copyright © 2020-2023  润新知