Giter Club home page Giter Club logo

clock's Introduction

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 clock

Usage

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
<DstTzInfo 'Asia/Harbin' LMT+8:27:00 STD>

Better

If you feel anything wrong, feedbacks or pull requests are welcomed.

clock's People

Contributors

fengsp avatar mozillazg avatar

Watchers

 avatar  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.