Delorean: Time Travel Made Easy

Overview

http://delorean.readthedocs.org/en/latest/_static/delorean.png

Delorean: Time Travel Made Easy

Delorean is a library for clearing up the inconvenient truths that arise dealing with datetimes in Python. Understanding that timing is a delicate enough of a problem delorean hopes to provide a cleaner less troublesome solution to shifting, manipulating, and generating datetimes.

Delorean stands on the shoulders of giants pytz and dateutil

Delorean will provide natural language improvements for manipulating time, as well as datetime abstractions for ease of use. The overall goal is to improve datetime manipulations, with a little bit of software and philosophy.

Pretty much make you a badass time traveller.

Getting Started

Here is the world without a flux capacitor at your side:

from datetime import datetime
import pytz

est = pytz.timezone('US/Eastern')
d = datetime.now(pytz.utc)
d = est.normalize(d.astimezone(est))
return d

Now lets warm up the delorean:

from delorean import Delorean

d = Delorean()
d = d.shift('US/Eastern')
return d

Look at you looking all fly. This was just a test drive: check out out what else delorean can help with in the documentation.

Comments
  • Delorean object from aware datetimes

    Delorean object from aware datetimes

    There seems to be no way to create a Delorean object from an aware datetime object.

    Is there a reason for this?

    I tend to work with fully aware datetime objects in UTC, but with them being passed around and used everywhere, it's not particularly easy to create a Delorean object from them, without having to do:

    dt = Delorean(datetime=dt_utc.replace(tzinfo=None), timezone='UTC')
    

    Which is a bit tedious for an awesome library like Delorean!

    I might be a little naive though (excuse the pun!) so I wanted to ask!

    opened by mwaterfall 7
  • Documentation mismatch for parsing strings in latest version

    Documentation mismatch for parsing strings in latest version

    Theres a bug when parsing date strings in the latest release.

    In a new virtual env:

    $ pip install delorean
    $ pip freeze
    Babel==2.2.0
    Delorean==0.6.0
    humanize==0.5.1
    python-dateutil==2.5.2
    pytz==2016.3
    six==1.10.0
    tzlocal==1.2.2
    wheel==0.24.0
    
    >>> import delorean
    >>> delorean.parse("2013-05-06")
    Delorean(datetime=datetime.datetime(2013, 6, 5, 0, 0), timezone='UTC')
    

    but the docs say that the output should be Delorean(datetime=datetime.datetime(2013, 5, 6), timezone='UTC') (which is the correct thing to do according to ISO 8601).

    Installing python-dateutil==2.5.1 fixes the problem, but i'm not sure if the bug is in delorean or on their end.

    opened by ParthGandhi 6
  • Delorean support for timedelta arithmetic

    Delorean support for timedelta arithmetic

    The timedelta class in the datetime package is very useful for calendar/time arithmetic. It would be great if Deloreans had the same behavior as datetime objects:

    from datetime import timedelta, datetime
    from delorean import Delorean
    
    dd1 = Delorean(datetime(2013, 2, 15), "US/Eastern")
    dd2 = Delorean(datetime(2013, 2, 16), "US/Eastern")
    
    one_day = dd2 - dd1
    assert one_day == timedelta(days=1)
    assert (dd1 + one_day) == dd2
    
    opened by fawce 6
  • Parsing string then converting to midnight and epoch.

    Parsing string then converting to midnight and epoch.

    I'm trying to parse a string row[15] using the parse function, and then convert the result to midnight, and then convert that to epoch, however it keeps coming back with:

    TypeError: 'datetime.datetime' object is not callable

    What am I doing wrong, and is it possible to do the above? Ideally i'd like to be able to get it to show milliseconds too...

    opened by markorapaic 5
  • Python 3.4 support?

    Python 3.4 support?

    Hi,

    Is there a reason why the 0.4.3 version does not have the 'Programming Language :: Python :: 3.3' and 'Programming Language :: Python :: 3.4'? It is included in the travis ci tests. For Python 3.4:

    >| python -V
    Python 3.4.2
    

    I cloned the repo and did:

    >| python setup.py develop
    >| python tests/test_data.py
    

    The result was:

    ...tests/test_data.py:431: DeprecationWarning: Please use assertEqual instead.
     self.assertEquals(dt1, dt2)
    .........................................................
    ----------------------------------------------------------------------
    Ran 60 tests in 0.016s
    
    OK
    

    Thanks a lot! (Love this package).

    opened by motiteux 4
  • PR for monkey patching

    PR for monkey patching

    Hello,

    I've been using a similar in-house module for time travel testing in Python, with the exception that mine supports monkey patching but not timezones. Monkey patching for me is absolutely vital, because many of the libraries used (e.g. Django) cannot be passed a context of custom time/datetime at runtime. For example;

    period1 = datetime.datetime(2011, 1, 1)
    with timetravel(period1):
        Model.objects.get_or_create() # this is created at 2013-01-01 00:00:00
        time.sleep(10)
        Model.objects.get_or_create() # this is created at 2013-01-01 00:00:10
        time.sleep(10)
    
    Model.objects.get_or_create() # this is created at 2013-12-10 00:00:20
    

    Although monkey patching does have a few surprising side effects when using automatic h:m:s calculation (i.e. accidental backwards time travel), it is exceptionally useful for testing time sensitive logic (such as subscription renewals etc).

    It would make more sense for me to add monkey patching support onto an existing library with good maturity and py3 support, as our in-house one lacks maturity in this area.

    Would you be happy for me to send a PR with a monkey patcher? Something like this;

    from delorean import monkey_patch
    monkey_patch()
    
    with Delorean(datetime=dt, global=True):
       # builtins are monkey patched
    
    with Delorean(datetime=dt) as dt:
       # builtins are not monkey patched
    

    Let me know your thoughts

    opened by foxx 4
  • Adds a copy() method for returning a copy of the Delorean object with a new timezone

    Adds a copy() method for returning a copy of the Delorean object with a new timezone

    shift() currently mutates the original Delorean object, so this is just a simple way to get a copy of the current object with an optional new timezone.

    opened by sleekslush 4
  • Fix ISO date parsing

    Fix ISO date parsing

    Thank you for cool library.

    In some of versions between 2.5 and 2.6 python-dateutil broke ISO date parsing when dayfirst=True. Unfortunately, delorean uses True for this parameter by default. So, I've fixed it:

    1. Try to parse string as ISO date before everything else.
    2. You can disable it via isofirst=False.
    3. Added test for this case.
    4. python-dateutil version constraint now >=2.7.0, because dateutil.parser.isoparse added only in this version.

    Close #101

    opened by orsinium 3
  • Delorean.shift does not take datetime.timezone.utc

    Delorean.shift does not take datetime.timezone.utc

    To reproduce:

    import delorean
    import datetime
    
    utc = datetime.timezone.utc
    dt = datetime.datetime(2017, 3, 5, 3, 1, 46, 425154, tzinfo=utc)
    
    d = delorean.Delorean(dt).shift(utc)
    

    From these lines, Delorean doesn't support anything outside of pytz.timezone ability to parse, which breaks on zone.upper() at the beginning of this method.

    Should I always use "UTC" instead of datetime's provided datetime.timezone.utc? I was hoping to reuse the standard library's version across different datetime libraries.

    opened by numberoverzero 3
  • PyPi / repo code is different

    PyPi / repo code is different

    I installed delorean using pip, which reports Delorean==0.2.1, which appears to be the same as the repo's version.py (0,2,1).

    However, the PyPi version of dates.py is missing several of the functions found in the repo (e.g. move_datetime_hour, move_datetime_minute, move_datetime_second).

    I'm assuming this means the repo was updated at some point, but PyPi wasn't and the repo's version.py didn't get bumped up.

    opened by crolfe 3
  • Allow conversion of non-pytz timezone aware datetimes to Delorean

    Allow conversion of non-pytz timezone aware datetimes to Delorean

    If a user is trying to convert a datetime initialized with a pytz tzinfo to Delorean the following happens:

    a = datetime(2013,8,13,7,3,26,56569, tzinfo=pytz.UTC)
    >>> delorean.Delorean(a)
    Delorean(datetime=2013-08-13 07:03:26.056569+00:00, timezone=UTC)
    

    But if a user is trying to convert a datetime initialized with a non-pytz tzinfo to Delorean the following happens:

    >>> b = datetime(2013,8,13,7,3,26,56569, tzinfo=colander.iso8601.UTC)
    delorean.Delorean(b)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/dev/.virtualenvs/whatstolose/local/lib/python2.7/site-packages/delorean/dates.py", line 169, in __init__
        zone = datetime.tzinfo.zone
    AttributeError: 'Utc' object has no attribute 'zone'
    

    Here are the docs for the tzinfo objects and the methods it should have: http://docs.python.org/release/2.5.2/lib/datetime-tzinfo.html

    The change I've made should now allow delorean to work with any TZ aware datetime.

    opened by benhohner 3
  • docs: Fix a few typos

    docs: Fix a few typos

    There are small typos in:

    • delorean/dates.py
    • delorean/interface.py
    • docs/install.rst
    • docs/quickstart.rst

    Fixes:

    • Should read specifically rather than specifcally.
    • Should read posed rather than possed.
    • Should read delorean rather than delorian.
    • Should read convenient rather than convient.
    • Should read associated rather than assoicated.
    • Should read associated rather than assocaited.

    Semi-automated pull request generated by https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md

    opened by timgates42 0
  • PytzUsageWarning

    PytzUsageWarning "normalize method is no longer necessary" when using delorean.parse()

    Environment:

    • Python 3.9.2
    • pytz 2021.3

    When using delorean.parse() this warning message is produced:

    delorean/dates.py:170: PytzUsageWarning: The normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
    
    opened by prehensilecode 0
  • zoneinfo.ZoneInfo AttributeError while parsing within the Django context

    zoneinfo.ZoneInfo AttributeError while parsing within the Django context

    I'm running into an error when using Delorean within a Django app context. This only happens when the Django app is loaded. For example, in the interpreter setup by django:

    $ python manage.py shell
    Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
    [Clang 12.0.5 (clang-1205.0.22.9)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> t = '2020-11-30T05:17:05.000000Z'
    >>> import delorean
    >>> delorean.parse(t).datetime
    

    This results in:

    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File ".../lib/python3.9/site-packages/delorean/interface.py", line 87, in parse
        do = Delorean(dt, timezone=tz)
      File ".../lib/python3.9/site-packages/delorean/dates.py", line 202, in __init__
        self._dt = localize(datetime, self._tzinfo)
      File ".../lib/python3.9/site-packages/delorean/dates.py", line 157, in localize
        return tz.localize(dt)
    AttributeError: 'zoneinfo.ZoneInfo' object has no attribute 'localize'
    

    The same procedure done outside of django results in a successful parsing of the datetime.

    $ python
    Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
    [Clang 12.0.5 (clang-1205.0.22.9)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> t = '2020-11-30T05:17:05.000000Z'
    >>> import delorean
    >>> delorean.parse(t).datetime
    datetime.datetime(2020, 11, 30, 5, 17, 5, tzinfo=<UTC>)
    

    Note, it's the same environment, just not within the django shell setup.

    AFAICT, the only difference is that Django is using a zoneinfo timezone instance, while pure python is finding any using a pytz timezone instance.

    opened by mmulich 1
  • mistake in parse example

    mistake in parse example

    example shows:

    >>> parse("2013-05-06")
    Delorean(datetime=datetime.datetime(2013, 5, 6), timezone='UTC')
    

    executing the example, I get:

    >>> parse("2013-05-06")
    Delorean(datetime=datetime.datetime(2013, 6, 5, 0, 0), timezone='UTC')
    

    Documentation shows dayfirst=True as default, so there's nothing wrong with the code. Just the example needs fixing.

    opened by smachanm 1
  • Publish New Version

    Publish New Version

    @myusuf3 Any chance of publishing a new version of Delorean to pip that includes the fix provided here: https://github.com/myusuf3/delorean/pull/103

    Cheers

    opened by Evesy 5
Releases(1.0.0)
A Python library for dealing with dates

moment A Python library for dealing with dates/times. Inspired by Moment.js and Kenneth Reitz's Requests library. Ideas were also taken from the Times

Zach Williams 709 Dec 09, 2022
Delorean: Time Travel Made Easy

Delorean: Time Travel Made Easy Delorean is a library for clearing up the inconvenient truths that arise dealing with datetimes in Python. Understandi

Mahdi Yusuf 1.8k Dec 20, 2022
Better dates & times for Python

Arrow: Better dates & times for Python Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating, formatt

Arrow 8.2k Jan 05, 2023
⌚️Internet Time reference and (eventually) converter site, for planning things with your internet friends who aren't (yet) obsessed with Internet Time 😉

Internet-Ti.me Internet Time reference and (eventually) converter site, for planning things with your internet friends who aren't (yet) obsessed with

Jessica Stokes 17 Nov 02, 2022
UNIX time from NTP or short UtfN is a simple CLI tool to set the time from an NTP-Server.

UNIX ⌚ from NTP UNIX time from NTP or short UtfN is a simple CLI tool to set the time from an NTP-Server. Sets time and date using the date command pr

Alexander 1 Jan 02, 2022
🏹 Better dates & times for Python

Arrow: Better dates & times for Python Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating, formatt

Arrow 8.2k Jan 09, 2023
Python datetimes made easy

Pendulum Python datetimes made easy. Supports Python 2.7 and 3.4+. import pendulum now_in_paris = pendulum.now('Europe/Paris') now_in_par

Sébastien Eustace 5.3k Jan 06, 2023
Cross Platform Application for Calculating Render Time

mdsanima-rt-go Cross Platform Application for Calculating Render Time. Testing This is a base application build on Windows Android and Linux. All buil

MDSANIMA DEV 2 Mar 29, 2022
TimeTagger is a web-based time-tracking solution that can be run locally or on a server

TimeTagger is a web-based time-tracking solution that can be run locally or on a server. In the latter case, you'll want to add authentication, and also be aware of the license restrictions.

Almar Klein 626 Jan 06, 2023
Parse human-readable date/time strings

parsedatetime Parse human-readable date/time strings. Python 2.6 or greater is required for parsedatetime version 1.0 or greater. While we still test

Mike Taylor 651 Dec 23, 2022
A Python 3 library for parsing human-written times and dates

Chronyk A small Python 3 library containing some handy tools for handling time, especially when it comes to interfacing with those pesky humans. Featu

Felix Wiegand 339 Dec 19, 2022
Friendly Python Dates

When.py: Friendly Dates and Times Production: Development: User-friendly functions to help perform common date and time actions. Usage To get the syst

Andy Dirnberger 191 Oct 14, 2022
A datetime parser in Python by Ari24-cb24 and NekoFantic

datetimeparser A datetime parser in Python by Ari24-cb24 and NekoFantic V 1.0 Erinnerung für den Parser Auf falsche Eingaben überprüfen Liste an Event

AriDevelopment 13 Dec 30, 2022
Useful extensions to the standard Python datetime features

dateutil - powerful extensions to datetime The dateutil module provides powerful extensions to the standard datetime module, available in Python. Inst

2k Dec 29, 2022
A Python module that tries to figure out what your local timezone is

tzlocal This Python module returns a tzinfo object with the local timezone information under Unix and Windows. It requires either Python 3.9+ or the b

Lennart Regebro 159 Dec 16, 2022
Datetimes for Humans™

Maya: Datetimes for Humans™ Datetimes are very frustrating to work with in Python, especially when dealing with different locales on different systems

Timo Furrer 3.4k Dec 28, 2022
PyTime is an easy-use Python module which aims to operate date/time/datetime by string.

PyTime PyTime is an easy-use Python module which aims to operate date/time/datetime by string. PyTime allows you using nonregular datetime string to g

Sinux 148 Dec 09, 2022
The Terasic DECA board as a mandelbrot acceleerator

deca-mandelbrot The Terasic DECA board as a mandelbrot accelerator. This is a hobby project to explore parallel computation/pipelining on a FPGA. curr

Hans Baier 11 Aug 29, 2022
A simple in-process python scheduler library, designed to be integrated seamlessly with the `datetime` standard library.

scheduler A simple in-process python scheduler library, designed to be integrated seamlessly with the datetime standard library. Due to the support of

30 Dec 30, 2022
Croniter provides iteration for the datetime object with a cron like format

Introduction Contents Introduction Travis badge Usage About DST About second repeats Testing if a date matches a crontab Gaps between date matches Ite

kiorky 152 Dec 30, 2022