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)
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 9, 2022
darts is a Python library for easy manipulation and forecasting of time series.

A python library for easy manipulation and forecasting of time series.

Unit8 5.2k Jan 1, 2023
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 2, 2022
⌚️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 2, 2022
A simple digital clock made with the help of python

Digital-Clock ⏰ Description ?? ✔️ A simple digital clock made with the help of python. The code is easy to understand and implement. With this reposit

Mohit 0 Dec 10, 2021
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 6, 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
ISO 8601 date/time parser

ISO 8601 date/time parser This module implements ISO 8601 date, time and duration parsing. The implementation follows ISO8601:2004 standard, and imple

null 118 Dec 20, 2022
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
Let your Python tests travel through time

FreezeGun: Let your Python tests travel through time FreezeGun is a library that allows your Python tests to travel through time by mocking the dateti

Steve Pulec 3.5k Dec 29, 2022
Let your Python tests travel through time

FreezeGun: Let your Python tests travel through time FreezeGun is a library that allows your Python tests to travel through time by mocking the dateti

Steve Pulec 3.5k Jan 9, 2023
Travel through time in your tests.

time-machine Travel through time in your tests. A quick example: import datetime as dt

Adam Johnson 373 Dec 27, 2022
Out-of-Town Recommendation with Travel Intention Modeling (AAAI2021)

TrainOR_AAAI21 This is the official implementation of our AAAI'21 paper: Haoran Xin, Xinjiang Lu, Tong Xu, Hao Liu, Jingjing Gu, Dejing Dou, Hui Xiong

Jack Xin 13 Oct 19, 2022
Application to list countries in order of travel from the United States.

Application to list countries in order of travel from the United States.

Broden Wanner 1 Nov 3, 2021
A set of scripts for a two-step procedure to measure the value of access to destinations across several modes of travel within a geographic area.

A set of scripts for a two-step procedure to measure the value of access to destinations across several modes of travel within a geographic area.

Institute for Transportation and Development Policy 2 Oct 16, 2022
Users can read others' travel journeys in addition to being able to upload and delete posts detailing their own experiences

Users can read others' travel journeys in addition to being able to upload and delete posts detailing their own experiences! Posts are organized by country and destination within that country.

Christopher Zeas 1 Feb 3, 2022
Kats is a toolkit to analyze time series data, a lightweight, easy-to-use, and generalizable framework to perform time series analysis.

Kats, a kit to analyze time series data, a lightweight, easy-to-use, generalizable, and extendable framework to perform time series analysis, from understanding the key statistics and characteristics, detecting change points and anomalies, to forecasting future trends.

Facebook Research 4.1k Dec 29, 2022
Django package to log request values such as device, IP address, user CPU time, system CPU time, No of queries, SQL time, no of cache calls, missing, setting data cache calls for a particular URL with a basic UI.

django-web-profiler's documentation: Introduction: django-web-profiler is a django profiling tool which logs, stores debug toolbar statistics and also

MicroPyramid 77 Oct 29, 2022
Your own movie streaming service. Easy to install, easy to use. Download, manage and watch your favorite movies conveniently from your browser or phone. Install it on your server, access it anywhere and enjoy.

Vigilio Your own movie streaming service. Easy to install, easy to use. Download, manage and watch your favorite movies conveniently from your browser

Tugcan Olgun 141 Jan 6, 2023
Easy-apply-bot - A LinkedIn Easy Apply bot to help with my job search.

easy-apply-bot A LinkedIn Easy Apply bot to help with my job search. Getting Started First, clone the repository somewhere onto your computer, or down

Matthew Alunni 5 Dec 9, 2022