Giter Site home page Giter Site logo

fawkesley / python-utcdatetime Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 2.0 37 KB

Simple UTC datetimes for Python using RFC3339 subset of ISO8601.

License: MIT License

Shell 5.09% Python 93.57% Makefile 1.34%
python timezone utc strptime utc-datetime utc-timezone parse

python-utcdatetime's Introduction

Build Status Coverage Status Latest Version Supported Python versions Development Status Supported Python implementations

utcdatetime: datetime class that makes working in UTC easier

If you work primarily with UTC datetimes you might find this utcdatetime makes your life a little bit easier.

Construct a UTC datetime

>>> import utcdatetime
>>> my_utcdatetime = utcdatetime.utcdatetime(2011, 6, 1, 16, 45)

standard library equivalent:

>>> import datetime
>>> import pytz
>>> my_datetime = datetime.datetime(2011, 6, 1, 16, 45, tzinfo=pytz.UTC)

Format in Z-style ISO 8601

>>> str(my_utcdatetime)
'2011-06-01T16:45:00Z'

standard library equivalent:

>>> my_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
'2011-06-01T16:45:00Z'

Parse from ISO 8601 Z, ±HH, ±HHMM, ±HH:MM style string

>>> import utcdatetime
>>> utcdatetime.utcdatetime.from_string('2011-06-01T16:45:00Z')
utcdatetime.utcdatetime(2011, 6, 1, 16, 45)

>>> utcdatetime.utcdatetime.from_string('2011-06-01T16:45:00+00:00')
utcdatetime.utcdatetime(2011, 6, 1, 16, 45)

>>> utcdatetime.utcdatetime.from_string('2011-06-01T18:45:00+02:00')
utcdatetime.utcdatetime(2011, 6, 1, 16, 45)

Note that non-UTC timezones are automatically converted to UTC.

Doing this in the standard library is really nasty for a few reasons:

Suppose you use datetime.datetime.strptime(my_string, '%Y-%m-%dT%H:%M:%S%z')

  • The %z argument to strptime only accepts ±HHMM, not ±HH:MM or ±HH
  • The %z argument also doesn't know about Z.
  • The datetime you get back then needs to be converted using .astimezone(UTC)
  • ... but you don't have a UTC class so you'll need to use pytz !
  • And what about microseconds?

So you'll have to work out the format first, then run a different strptime accordingly.

Get now() that actually has a timezone

>>> import utcdatetime
>>> utc_now = utcdatetime.utcdatetime.now()
>>> utc_now
utcdatetime.utcdatetime(2011, 1, 5, 18, 45, 36)

>>> str(utc_now)
`2011-01-05T18:45:36Z'

Convert a datetime into a utcdatetime

Not a problem, as long as it's got a timezone:

>>> import datetime
>>> import utcdatetime

>>> europe = pytz.timezone('Europe/London')
>>> my_datetime = europe.localize(datetime.datetime(2010, 6, 10, 18, 45))
>>> utcdatetime.utcdatetime.from_datetime(my_datetime)
utcdatetime.utcdatetime(2010, 6, 10, 17, 45)  # Note British Summer Time -> UTC

Convert a utcdatetime into a Python datetime

>>> import utcdatetime
>>> import pytz

>>> my_datetime = utcdatetime.utcdatetime(2010, 6, 10, 17, 45)  # Note British summer time
>>> my_datetime.astimezone(pytz.timezone('Europe/London'))
datetime.datetime(2010, 6, 10, 18, 45, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)

Motivation

I used to work all day with UTC datetimes in Python and I found some baffling things:

  • Why do I need to import pytz just to get a UTC timezone class?
  • Why does datetime.utcnow() return a native datetime with no UTC timezone?!
  • Why does datetime.isoformat() use the +00:00 format rather than Z for a UTC datetime?
  • Why do I have to keep writing %Y-%m-%dT%H:%M:%SZ every time I parse or format a UTC datetime?
  • Why do I have to handle all these timezone syntaxes: Z, ±HH:MM, ±HHMM, ±HH ?!

This (opinionated) library gives you a UTC datetime with a sensible formatter and a parser which handles sane ISO 8601 datetimes.

python-utcdatetime's People

Contributors

jnrowe avatar movermeyer avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

python-utcdatetime's Issues

Make it hashable

import utcdatetime
set(utcdatetime.utcdatetime.now())

... should be allowed...

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.