Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fengsp/clock
A minimalist datetime library for Python
https://github.com/fengsp/clock
Last synced: about 2 months ago
JSON representation
A minimalist datetime library for Python
- Host: GitHub
- URL: https://github.com/fengsp/clock
- Owner: fengsp
- License: other
- Created: 2014-05-14T08:58:59.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-17T03:26:23.000Z (almost 9 years ago)
- Last Synced: 2024-09-30T11:16:28.234Z (3 months ago)
- Language: Python
- Size: 4.88 KB
- Stars: 10
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES
- License: LICENSE
Awesome Lists containing this project
README
Welcome to Clock
================Clock is a nice datetime practice.
Guideline
---------**Always handle time in UTC.**
If you feel confused, read `Dealing with Timezones in Python
`_.Installation
------------::
$ pip install clockUsage
-----.. code:: python
import clock
# Internally always use UTC, convert everything else to UTC
# before manipulate or save datetime
## get current UTC datetime
clock.utc()## get one timestamp in UTC
utc_dt = clock.utc(1400639252.963287)
clock.utc(1400639252)
clock.utc('1400639252.963287')
clock.utc('1400639252')
## get one local datetime in UTC
clock.utc(clock.datetime(2014, 5, 12, 11, 11, 11), clock.localtimezone)
clock.utc(clock.datetime(2014, 5, 12, 11, 11, 11), 'US/Pacific')
## cannot use explicit timezone with timezone-aware datetime both
clock.utc(clock.datetime.now().replace(tzinfo=clock.localtimezone))
# Interfacing with the user, convert to local time
local_dt = clock.local(utc_dt, clock.localtimezone)
clock.local(utc_dt, 'US/Pacific')
## convert to POSIX timestamp
clock.to_timestamp(utc_dt)
## then format it and show it to user
>>> local_dt.strftime('%Y-%m-%d %H:%M:%S')
'2014-05-21 10:27:32'
## actually clock comes with one shortcut for this, pass UTC datetime
## in and get local datetime format
>>> clock.format(utc_dt, clock.localtimezone, '%Y-%m-%d %H:%M:%S')
'2014-05-21 10:27:32'# utcnow and timedelta
>>> clock.now()
datetime.datetime(2014, 5, 21, 3, 27, 26, 580031)
>>> clock.timedelta(days=1)
datetime.timedelta(1)
# get local timezone
>>> clock.localtimezone
Better
------If you feel anything wrong, feedbacks or pull requests are welcome.