推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
sinux
0.09D
V2EX  ›  Python

分享一个并没有什么卵用的 Python 库: PyTime

  •  3
     
  •   sinux ·
    PRO
    ·
    shinux · May 15, 2015 · 6634 views
    This topic created in 4195 days ago, the information mentioned may be changed or developed.

    (其实就是厚着脸皮来求Star的, 噗)

    仓库地址:https://github.com/shnode/PyTime

    安装:

    pip install pytime
    

    主要作用就是通过字符串的方法操纵时间戳,以及不规则的datetime string的规则化。

    >>>from pytime import pytime
    >>>
    >>>pytime.before('2015.5.17', '2years 3mon 3d 2hr' )
    datetime.datetime(2013, 2, 13, 22, 0)
    >>>
    >>>pytime.after(pytime.tomorrow(), '23month3dy29minu')
    datetime.datetime(2017, 4, 19, 0, 29)
    

    随包附赠了西方节日和常用的时间段,x时间之前,x时间之后等方便写脚本的时候计算时间。

    在test并没有完全覆盖(明明是基本不覆盖,摔)以及代码还很粗糙的情况下,我还是上了包管理...

    总之欢迎star ,fork , 提意见,pull request, 推荐工作,搞基,介绍女朋友之类啦╮(╯▽╰)╭

    Supplement 1  ·  May 16, 2015
    测试已经覆盖了!增加了某一天的上个星期,下个月之类的东西啦,仍然是字符串和时间戳都接受的,更方便写脚本和(伪)自动化运维啦。
    Supplement 2  ·  May 16, 2015
    被Python Trending 捕获到并发到官推上啦:)


    Supplement 3  ·  May 16, 2015
    已经进入 Awesome-python 的Date and Time区域。(https://github.com/vinta/awesome-python#date-and-time )
    33 replies  •  2015-05-17 18:46:12 +08:00
    R4rvZ6agNVWr56V0
    R4rvZ6agNVWr56V0
    May 15, 2015❤️1
    1
    顶 标题
    viko16
    viko16
    May 15, 2015❤️1
    2
    顶 自带吐槽
    redf
    redf
    May 15, 2015❤️1
    3
    不错
    kqz901002
    kqz901002
    May 15, 2015❤️1
    4
    顶头像
    redf
    redf
    May 15, 2015
    5
    哦,对了,看下我的帖子...
    HanSonJ
    HanSonJ
    May 15, 2015❤️1
    6
    看标题就有种预感,进来一看果然,我也写了这个一个,不过用的是golang

    https://github.com/Hanccc/timeutil
    rrfeng
    rrfeng
    May 15, 2015❤️1
    7
    bash 下的 date 就很棒可以照着完成以下……
    LeoQ
    LeoQ
    May 15, 2015❤️1
    8
    已star

    @rrfeng 同意
    HowardMei
    HowardMei
    May 15, 2015❤️1
    9
    你在哪儿?要是在新加坡,我给你介绍女朋友,搞Java的萌妹子 xD
    Mirana
    Mirana
    May 15, 2015❤️1
    10
    把舌头捋直再来
    staticor
    staticor
    May 16, 2015❤️1
    11
    分享另一个 datetime的 https://github.com/crsmithdev/arrow
    jas0ndyq
    jas0ndyq
    May 16, 2015 via iPhone
    12
    不错
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    13
    @staticor arrow 和 dateutil 用的很多了,还有 pytimeparse我也看了,代码质量和时间的精度(尤其是在time的秒的阶段)自然都是水准很高的,而且pytimeparse全程使用正则,我是正则和字符串匹配混用的,哈哈,还是感谢支持:)
    msg7086
    msg7086
    May 16, 2015
    14
    Rails用户表示,下面这种写法已经能满足大多数需要了……
    (2.years + 3.months + 3.days + 2.hours).from_now #=> 2017-08-18 22:46:11 -0700
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    15
    @msg7086 Rails萌萌哒,然而Python没有这种方法...days以下可以用timedelta赋值之后加减,months和years要用正常 的加减之后,再replace时间戳里地数值,并且有年和月的进退位不是平滑的。
    msg7086
    msg7086
    May 16, 2015
    16
    @sinux 本来想说能不能在python里也实现这样的功能。但是一想,python里的整数好像不是对象?
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    17
    @msg7086 是对象的:)
    msg7086
    msg7086
    May 16, 2015
    18
    @sinux 呃,那为啥没人搞1.days.ago?是因为类不能修改?还是说有哲学意味在里面?
    LPeJuN6lLsS9
    LPeJuN6lLsS9
    May 16, 2015
    19
    @msg7086 抱歉插句话……这样的话可能要MyInt(1).days.ago了,你说的用法是ruby的int自带还是rails提供的呢?如果是后者,py应该也可以按照类似方法修改int
    eepaul
    eepaul
    May 16, 2015
    20
    顶标题,湖南人?
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    21
    @eepaul 哈哈不是啊,何以见得
    eepaul
    eepaul
    May 16, 2015
    22
    @sinux 没什么卵用 用得很好
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    23
    @eepaul ...其实也并不是没有什么用,主要看有没有相关的需求。
    wolfan
    wolfan
    May 16, 2015 via Android
    24
    我想找个可以合并音频文件的py库不知有么?
    msg7086
    msg7086
    May 16, 2015
    25
    @hantsuki rails提供,不过直接插入了数值类 (或者说Numeric)
    类似
    class Numeric:
    def hours:
    ...
    这样。
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    27
    @wolfan 看到你说的是波形叠加...这个真不知道
    iloveayu
    iloveayu
    May 16, 2015
    28
    这头像配这标题,莫名其妙戳中笑点。23333
    sinux
    sinux
    OP
    PRO
    May 16, 2015
    29
    @iloveayu ....
    knktc
    knktc
    May 17, 2015
    30
    我要写issue的话就是提一个:可否出个中国版的,增加中国节假日,应用场景是某些计划任务的灵活调整(这个估计需要联网获取节假日信息了,有点脑洞了)
    mingyun
    mingyun
    May 17, 2015❤️1
    31
    star
    sinux
    sinux
    OP
    PRO
    May 17, 2015
    32
    @knktc 尝试写了农历,但是一直不准确,就放弃了。
    alexapollo
    alexapollo
    May 17, 2015
    33
    嗯,看起来我也该写个easytime的库了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   511 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 20:57 · PVG 04:57 · LAX 12:57 · JFK 15:57
    ♥ Do have faith in what you're doing.