Skip to main content

Extends datetime.time, allowing time arithmetic

Project description

Python’s “datetime” module has many uses, but it has a difficulty: you can’t do any arithmetic with “datetime.time”. Only with “datetime.datetime”. Now, there are good reasons for this: “What time will it be, 24 hours from now” has a lot of corner cases, including daylight savings time, leap seconds, historical timezone changes, and so on. But sometimes you really do need the simple case. Sometimes the perfect is the enemy of the good. And that’s why you have this module. This module will allow you to cocoon yourself in the comforting illusion that, in 24 hours, it will be the same time as it is right now.

And what freedom this illusion gives you! You can add a “datetime.timedelta” object to an “nptime” object, and it works! It will cycle around 24 hours, like modular arithmetic. You can ask “what time comes 1 day and 36 minutes after 12:24 pm?” and it will let you know: 1:00 pm. How lovely!

>>> from nptime import nptime
>>> from datetime import timedelta, date, datetime
>>> afternoon = nptime(12, 24) + timedelta(days=1, minutes=36)
>>> afternoon
nptime(13, 0)
>>> str(afternoon)
'13:00:00'

Maybe we’re talking about the afternoon of Guido van Rossum’s birthday. Lucky, we can combine “nptime” objects with “date” objects:

>>> datetime.combine(date(1956, 1, 31), afternoon)
datetime.datetime(1956, 1, 31, 13, 0, 0)

You can also ask “How long is it between 9:00 AM and 5:00 PM? It sure feels like a million years!”

>>> workday = nptime(hour=17) - nptime(hour=9)
>>> workday
datetime.timedelta(0, 28800)
>>> print workday
8:00:00

Nope, only 8 hours, how strange. Anyway, please use this module. It will be convenient. But don’t use it when talking about concrete time, time in a particular place, or anything like that. In fact, it doesn’t even notice “datetime.tzinfo” objects right now. Good luck.

Note: You can find the newest version at https://github.com/tgs/nptime .

You can find online documentation at http://tgs.github.com/nptime/ . The package is available for download from PyPi: easy_install nptime. This software is licensed under the 3-clause BSD License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nptime-0.4.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distributions

nptime-0.4.0-py33-none-any.whl (6.3 kB view details)

Uploaded Python 3.3

nptime-0.4.0-py27-none-any.whl (6.4 kB view details)

Uploaded Python 2.7

File details

Details for the file nptime-0.4.0.tar.gz.

File metadata

  • Download URL: nptime-0.4.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nptime-0.4.0.tar.gz
Algorithm Hash digest
SHA256 158b8dd99eea6feb5c785432a2b05749807d5e4365c8f779f2363fed26294f63
MD5 b78f36b35935a6f23dcb241b9eab32ea
BLAKE2b-256 fccbf3ed8f8ac7997cd8ddec92aa21dfd5121a3896135b7c52f694435f7a6df8

See more details on using hashes here.

File details

Details for the file nptime-0.4.0-py33-none-any.whl.

File metadata

File hashes

Hashes for nptime-0.4.0-py33-none-any.whl
Algorithm Hash digest
SHA256 2c11e149b34e992e552b71d77ee3686e498180555456dbbc2e4ae8120361f2a2
MD5 8011ac7fc433ba0484ea94cc603e0de8
BLAKE2b-256 a5fc396522430f28fc4d79081db468fa1ecfe229fc8fed946675e3cff88ecfbc

See more details on using hashes here.

File details

Details for the file nptime-0.4.0-py27-none-any.whl.

File metadata

File hashes

Hashes for nptime-0.4.0-py27-none-any.whl
Algorithm Hash digest
SHA256 fbe4a386aafc9aedddf56e8128457abb2c7bd2bc5d2fe048d4cde48d3387683e
MD5 0d130be82b08edd8e1380a804d04e224
BLAKE2b-256 6f9f97aa76fdff879bcaaa0ba327ca94254c539536e3b89aa27e7215c88e841f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page