Coder Social home page Coder Social logo

mvanveen / times Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nvie/times

1.0 3.0 0.0 99 KB

Times and time zones in Python with a focus on best practices.

Home Page: http://pypi.python.org/pypi/times

License: Other

Shell 1.80% Python 98.20%

times's Introduction

Times

Times is a small, minimalistic, Python library for dealing with time conversions to and from timezones, for once and for all.

It is designed to be simple and clear, but also opinionated about good and bad practices.

Armin Ronacher wrote about timezone best practices in his blog post Dealing with Timezones in Python. The tl;dr summary is that everything sucks about our mechanisms to represent absolute moments in time, but the least worst one of all is UTC.

Rationale

Python's datetime library and the pytz library are powerful, but because they don't prescribe a standard practice of working with dates, everybody is free to pick his or her own way.

times tries to make working with times and timezones a little less of a clusterfuck and hopefully set a standard of some sort.

It still uses datetime and pytz under the covers, but as long as you never use any timezone related stuff outside times, you should be safe.

Accepting time

Never work with local times. Whenever you must accept local time input (e.g. from a user), convert it to universal time immediately:

>>> times.to_universal(local_time, 'Europe/Amsterdam')
datetime.datetime(2012, 2, 1, 10, 31, 45, 781262)

You can approach the conversion from the other side, too, as to_universal is conveniently aliased to from_local:

>>> times.from_local(local_time, 'Europe/Amsterdam')
datetime.datetime(2012, 2, 1, 10, 31, 45, 781262)

The second argument can be a pytz.timezone instance, or a timezone string.

If the local_time variable already holds timezone info, you must leave out the source timezone from the call.

To enforce best practices, times will never implicitly convert times for you, even if that would technically be possible.

When you want to record the current time, you can use this convenience method:

>>> import times
>>> print times.now()
datetime.datetime(2012, 2, 1, 11, 51, 27, 621491)

Presenting times

To present times to the end user of your software, you should format your absolute time to your user's local timezone.

>>> import times
>>> now = times.now()
>>> print times.format(now, 'CET')
2012-02-01 21:32:10+0100

As with the to_universal function, the second argument may be either a timezone instance or a timezone string.

Note: It is possible to convert universal times to local times, using to_local (or the alias from_universal). However, you probably shouldn't do it, unless you want to strftime() the resulting local date multiple times. In any other case, you are advised to use times.format() directly instead.

times's People

Contributors

nvie avatar

Stargazers

Michael Van Veen avatar

Watchers

Michael Van Veen avatar James Cloos 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.