• Stars
    star
    345
  • Rank 122,750 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 10 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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'

More Repositories

1

AGM

Authentic Gameplay Modification for A3 | THIS PROJECT IS NO LONGER BEING WORKED ON. ALL ISSUES AND PULL REQUESTS WILL BE IGNORED.
C++
125
star
2

armake

A C implementation of Arma modding tools (PAA conversion, binarization/rapification, PBO packing). (WIP)
C
97
star
3

SudokuSolver

A script that tries to extract a sudoku from an image and solve it.
Python
77
star
4

rustbucket

Access your car's diagnostics with a BeagleBone Blue and Rust.
Rust
50
star
5

armake2

Successor to armake written in Rust
Rust
49
star
6

pymoji

Emoji-cheat-sheet converter for Python
Python
9
star
7

Arma3Spotter

A webapp for calculating firing solutions using Arma 3 and AGM
Java
8
star
8

p3drender

Render P3Ds using py3d, armake and Blender
Python
8
star
9

AGM_Compatibility

Compatibility PBOs for AGM
C++
7
star
10

bucksaw

A tool for looking inside your Betaflight/INAV logs
Rust
7
star
11

vim-sqf

SQF Syntax Highlighting For Vim
Vim Script
6
star
12

crowbar.rs

Quick-and-dirty tool for debinarizing ODOL P3Ds.
Rust
4
star
13

dotfiles

dotfiles? dotfiles.
Vim Script
4
star
14

p3dtxt

A tool for converting MLOD P3Ds to a text-based representation
Rust
3
star
15

puush-linux

A Python implementation of the file-upload/screenshot tool Puush for use on Linux
Python
3
star
16

ext

Extract archives without worrying about parameters or subdirectories
Python
2
star
17

py3d

Python library for reading and writing Arma's P3D files (MLOD format)
Python
2
star
18

PyGHI

A Python implementation of stephencelis' GHI
Python
1
star
19

docker-arma3

Shell
1
star
20

ArmaUtils

Various tools for Arma content creation
Python
1
star