GithubHelp home page GithubHelp logo

mredar / chronyk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from koffeinflummi/chronyk

0.0 1.0 0.0 299 KB

A Python 3 library for parsing human-written times and dates

License: MIT License

Python 100.00%

chronyk's Introduction

Chronyk

Build Status Code Coverage PyPi License

A small Python 3 library containing some handy tools for handling time, especially when it comes to interfacing with those pesky humans.

Features

  • Parsing human-written strings ("10 minutes ago", "10. April 2015", "2014-02-15"...)
  • Relative time string creation ("in 2 hours", "5 hours ago")
  • Various input formats
  • Easy to use

Installation

$ pip install chronyk

Usage

Basic:

>>> from chronyk import Chronyk
>>> t = Chronyk(1410531179.0)
>>> t = Chronyk("May 2nd, 2016 12:51 am")
>>> t = Chronyk("yesterday")
>>> t = Chronyk("21. 8. 1976 23:18")
>>> t = Chronyk("2 days and 30 hours ago")
>>> t.ctime()
'Tue Sep  9 05:59:39 2014'
>>> t.timestamp()
1410235179.0
>>> t.timestring()
'2014-09-09 05:59:39'
>>> t.timestring("%Y-%m-%d")
'2014-09-09'
>>> t.relativestring()
'3 days ago'
>>> t.date()
datetime.date(2014, 9, 9)
>>> t.datetime()
datetime.datetime(2014, 9, 9, 5, 59, 39)

Input validation:

import sys
import chronyk

timestr = input("Please enter the date you were born: ")
try:
    date = chronyk.Chronyk(timestr, allowfuture=False)
except chronyk.DateRangeError:
    print("Yeah, right.")
    sys.exit(1)
except ValueError:
    print("Failed to parse birthdate.")
    sys.exit(1)
else:
    print("You were born {}".format(date.relativestring()))

Timezones:

By default, the Chronyk constructor uses local time, and every method by default uses whatever was passed to the constructor as well.

Almost all methods, however, have a timezone keyargument that you can use to define your local offset from UTC in seconds (positive for west, negative for east).

If you want to use a certain timezone for more than one method, you can also change the timezone instance attribute itself:

>>> t = Chronyk("4 hours ago", timezone=0) # using UTC
>>> t.ctime()
'Tue Sep  9 10:53:42 2014'
>>> t.timezone = -3600 # changes to CET (UTC+1)
>>> t.relativeTime()
'5 hours ago'
>>> t.ctime()
'Tue Sep  9 09:53:42 2014'

This uses the local relative time and returns a time string relative to current UTC:

>>> t = Chronyk("4 hours ago")
>>> t.relativestring(timezone=0)
'3 hours ago'

This uses a UTC timestamp and returns a time string relative to local time:

>>> t = Chronyk(1410524713.69, timezone=0)
>>> t.relativestring(timezone=chronyk.LOCALTZ)
'2 hours ago'

chronyk's People

Contributors

hayd avatar koffeinflummi avatar walkerdb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.