Giter Site home page Giter Site logo

times's Introduction

NOTE:
This library will not be maintained any further. You probably want to use the excellent Arrow library.

“There should be one—and preferably only one—obvious way to do it.”

In fact, version 0.7 of times has been rewritten to be implemented on top of Arrow, so it still provides the Times interface, but you'll already be using Arrow. You can probably easily replace your times function calls by Arrow objects.


Times

Build status: Build Status Coverage Status

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

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)

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.

Date Strings

If you want to accepting datetime representations in string form (for example, from JSON APIs), you can convert them to universal datetimes easily:

>>> import time, times
>>> print times.to_universal('2012-02-03 11:59:03-0500')   # auto-detects source timezone

Times utilizes the string parsing routines available in dateutil. Note that the source timezone is auto-detected from the string. If the string contains a timezone offset, you are not allowed to explicitly specify one.

If the string does not contain any timezone offset, you must specify the source timezone explicitly:

>>> print times.to_universal('2012-02-03 11:59:03', 'Europe/Amsterdam')

This is the inverse of times.format().

POSIX timestamps

If you prefer working with UNIX (POSIX) timestamps, you can convert them to safe datetime representations easily:

>>> import time, times
>>> print times.to_universal(time.time())
2012-02-03 11:59:03.588419

Note that to_universal auto-detects that you give it a UNIX timestamp.

To get the UNIX timestamp representation of a universal datetime, use:

>>> print times.to_unix(universal_time)

Current time

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 explicitly format your universal 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). 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

arturdryomov avatar dbader avatar jparise avatar mvanveen avatar nvie avatar selwin avatar svetlyak40wt 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.