• This repository has been archived on 25/Aug/2020
  • Stars
    star
    394
  • Rank 109,295 (Top 3 %)
  • Language
    Python
  • License
    Other
  • Created almost 13 years ago
  • Updated over 10 years ago

Reviews

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

Repository Details

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

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.

More Repositories

1

gitflow

Git extensions to provide high-level repository operations for Vincent Driessen's branching model.
Shell
26,364
star
2

git-toolbelt

A suite of useful Git commands that aid with scripting or every day command line usage
Shell
1,175
star
3

vim-flake8

Flake8 plugin for Vim
Vim Script
1,053
star
4

vimrc

My personal Neovim configuration, with a lot of love put into it.
Vim Script
548
star
5

decoders

Elegant validation library for type-safe input data (for TypeScript and Flow)
JavaScript
351
star
6

cookiecutter-python-cli

Python
186
star
7

itertools

TypeScript port of Python's awesome itertools stdlib.
TypeScript
139
star
8

vim-rst-tables

Easily create and reformat your RST (reStructuredText) tables as you change cell content.
Python
121
star
9

dotfiles

Shell
94
star
10

shFlags

Git mirror of the shFlags project by Kate Ward
Shell
89
star
11

vim-pyunit

Plugin enabling advanced unit test support inside your favorite editor.
Python
74
star
12

lemons.js

🍋 Common algebraïc data types for JS
JavaScript
59
star
13

SimpleAES

AES-256 encryption and decryption in Python for mere mortals.
Python
55
star
14

vim-togglemouse

Toggles the mouse focus between Vim and your terminal emulator, allowing terminal emulator mouse commands, like copy/paste.
Vim Script
52
star
15

python-fu

Python command line tools, for increased fu.
Python
46
star
16

vim-pep8

This project is superseded by vim-flake8!
Vim Script
38
star
17

sr

A simple mass search & replace tool
Rust
30
star
18

osx-install

Personal OSX install scripts / notes
Shell
26
star
19

vim_bridge

A Python-to-Vim bridge decorator that allows transparent calls to Python functions in native Vim scripts.
Python
23
star
20

pluck

General-purpose function to pluck fields from an iterable's values.
Python
21
star
21

dictmerge

Merge dicts without mutating them.
Python
16
star
22

git-it

Git issue tracker
Python
14
star
23

python-drainers

Event-based draining of process output
Python
13
star
24

new_workers

Python
13
star
25

vim-pyflakes

This project is superseded by vim-flake8!
Vim Script
11
star
26

rule-of-law

A sanity checker to verify assumptions about data consistency
JavaScript
8
star
27

debrief.js

Object serialization and annotation, for use in human-friendly error messages
JavaScript
8
star
28

even-more-itertools

Even more itertools than the stdlib and the more-itertools project provide.
Python
8
star
29

BKrypt

Thin wrapper around the bcrypt library.
Python
6
star
30

scripts

A collection of simple and small but useful UNIX shell scripts.
Shell
6
star
31

nox-ideas

Ideas for a new kind of programming language
6
star
32

vim-ini

Syntax highlighting for INI files in Vim.
Vim Script
6
star
33

Convenience

Convenience Cocoa classes, extending default functionality for a bunch of Cocoa objects.
Objective-C
5
star
34

VDOrderedEntityMigrationPolicy

Custom migration policy class for migrating Core Data entities with ordered relationships (i.e. entities that inherit from BWOrderedManagedObject).
Objective-C
4
star
35

ast-generator

TypeScript
3
star
36

syncfrom

Super-duper easy rsync wrappers for your home setup
Shell
3
star
37

Xcode-Extensions

Custom made Xcode extensions
Objective-C
3
star
38

homebrew-tap

Personal taps for Homebrew
Ruby
2
star
39

vim-nox

Nox syntax highlighting for Vim
Vim Script
1
star
40

nvie

1
star
41

clean-project

Repo to demonstrate a bug in Bun
TypeScript
1
star