Giter VIP home page Giter VIP logo

timeframe's Introduction

TimeFrame

linter tests codecov License Stars Downloads Open Issues Open PRs CodeClimate Maintainability CodeClimate Issues CodeClimate Tech Debt Contributors Version Python Wheel Repo Size Code Size

Introduction

This package makes the following calculations on datetime:

  • Adding two time frames, resulting in one bigger time frame or two disjoint one.
  • Multiplying two time frames, resuling in either an overlapped time frame or an empty one, depending on the two time frames.
  • Substracting two time frames, resuling in one or several time frames.

Install

Installing the package is as simple as running the following command inside your terminal:

pip install timeframe

Examples

NOTE: You can always take a look at the test cases in the tests directory to get a sense of how to use the package, but consider the below examples first, because it's fairly easy to use.

You need to import datetime as well as TimeFrame:

from datetime import datetime
from timeframe import TimeFrame

Inclusion

New API

>>> tf1 = TimeFrame(datetime(2021, 1, 1), datetime(2021, 1, 2))
>>> tf2 = TimeFrame(datetime(2021, 1, 1, 12), datetime(2021, 1, 1, 13))
>>> tf2 in tf1
True

Deprecated

This implies whether or not one time frame includes another; it can also be used to check if a datetime is inside one TimeFrame.

When you want to check if a datetime is inside a TimeFrame:

tf1 = TimeFrame(datetime(2021, 1, 26, 19), datetime(2021, 1, 26, 20))
tf1.includes(datetime(2021, 1, 26, 19, 30))
# output: True

When You want to check if an instance of TimeFrame is inside another one:

tf2 = TimeFrame(datetime(2021, 1, 26, 19, 30), datetime(2021, 1, 26, 19, 40))
tf1.includes(tf2)
# output: True
tf3 = TimeFrame(datetime(2021, 1, 26, 19, 45), datetime(2021, 1, 26, 21, 30))
tf1.includes(tf3)
# output: False

Duration

TimeFrame has a property named duration which can be used to retrieve the total amount of seconds that TimeFrame has:

tf1.duration
# output: 3600.0
tf2.duration
# output: 600.0
tf3.duration
# output: 6300.0

Comparison

You can always compare two TimeFrame to see if one is greater than the other or not. This comparison is based on the end of one TimeFrame and the start of the other.

tf1 > tf2
# output: False
tf3 > tf2
# output: True

You can also compare equality using either greater-equal sign, or a simple equal.

tf1 == tf2
# output: False
tf3 >= tf2
# output: True

Overlap

When you want to know how much two time frames have in common, use multiply sign:

tf1 * tf2
# output: 2021-01-26T19:30:00#2021-01-26T19:40:00
tf2 * tf3
# output: Empty TimeFrame

You can also check their duration as well:

(tf1 * tf2).duration
# output: 600.0
(tf2 * tf3).duration
# output: 0.0

Summation (union)

The summation sign is used to get the union of two time frames:

tf1 + tf2
# output: 2021-01-26T19:00:00#2021-01-26T20:00:00
(tf1 + tf2).duration
# output: 3600.0
tf1 + tf3
# output: 2021-01-26T19:00:00#2021-01-26T21:30:00
(tf1 + tf3).duration
# output: 9000.0

Minus

You can also substract one time frame from the other, which will ultimately result in either two disjoint time frames, or one unified time frame, depending on the time frames.

tf1 - tf2
# output:
# 2021-01-26T19:00:00#2021-01-26T19:29:59.999999
# 2021-01-26T19:40:00.000001#2021-01-26T20:00:00
(tf1 - tf2).duration
# output: 2999.999998

Substracting two disjoint time frames will return the first time frame as a result.

tf2 - tf3
# output: 2021-01-26T19:30:00#2021-01-26T19:40:00
(tf2 - tf3).duration
# output: 600.0
(tf2 - tf3) == tf2
# output: True

Acknowledgment

Thank you for showing interest in this package. Feel free to contact me if you feel like it. ๐Ÿฅ‚

Contribution

Any contribution of any size is greatly appreciated. Feel free to open a PR or issue in the github page at any time. ๐Ÿค—

Stargazers over time

Star History Chart

timeframe's People

Contributors

dependabot[bot] avatar meysam81 avatar pre-commit-ci[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

topknopp

timeframe's Issues

TypeError when BatchTimeFrame is subtracted a Timeframe from within

        tf_long = TimeFrame(datetime(2022, 3, 1, 12), datetime(2022, 3, 1, 13))
        tf_a = BatchTimeFrame([tf_long])

        tf_sub_a = TimeFrame(datetime(2022, 3, 1, 12, 30), datetime(2022, 3, 1, 12, 45))
        tf_b = BatchTimeFrame([tf_sub_a])

        print(tf_a - tf_b)  #TypeError: Every iterable element should be a BaseTimeFrame

ValueError when subtracting a timeframe that includes the other but has equal start

Python console:

from timeframe import TimeFrame
from datetime import datetime
tf1 = TimeFrame(datetime(2022, 2, 22), datetime(2022, 2, 23))
tf2 = TimeFrame(datetime(2022, 2, 22), datetime(2022, 2, 24))
tf2 - tf1
# 2022-02-23T00:00:00.000001#2022-02-24T00:00:00  (fine)
tf1 - tf2
# -> Exception:
Traceback (most recent call last):
  File "F:\Programme\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "F:\Programme\Python\Python310\lib\site-packages\timeframe\timeframe.py", line 359, in __sub__
    return TimeFrame(lower, upper)
  File "F:\Programme\Python\Python310\lib\site-packages\timeframe\timeframe.py", line 195, in __init__
    raise ValueError(
ValueError: start should be lower or equal than end: 2022-02-24 00:00:00.000001 & 2022-02-23 00:00:00

PS: Great package otherwise! :) ๐Ÿฅ‡

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.