🕟 Date and time processing language

Overview

Date Time Expression

PyPI Travis (.com) PyPI - Python Version License: WTFPL

dte is a WIP date-time processing language with focus on broad interpretation.

If you don't think it's intuitive, it's most likely unfinished.

It is strongly inspired by pdd.

demo

How to use & What to know

pip install dte

Conventions

relevant xkcd

  • When there is margin for ambiguity, expressions are always interpreted with highest units appearing before, complying with ISO-8601, e.g.: 2021-06-13, 2023 August 27 or 2019 Jul 20
  • Unix timestamps are both interpreted and output in seconds by default, but this is configurable
  • When specifying time, just remember that M is for month and m is for minute

Configuration File

dte tries to read a config.json file under config directory (~/.config/dte/ on Linux). In it you can set the following options:

" "clock": "<24|12>", "datetime_output_format": " >", "comparison_tolerance_seconds": , "basedate_output_format": "%Y-%b", "decimal_places": } ">
{
  "timestamp_unit": "
       
        "
  "clock": "<24|12>",
  "datetime_output_format": "
        
         
          >",
  "comparison_tolerance_seconds": 
          
           ,
  "basedate_output_format": "%Y-%b",
  "decimal_places": 
           
             } 
           
          
         
        
       

Dependencies

  • dateutil handles month and year's complex operations
  • ply is a pure-Python implementation of the popular compiler construction tools lex and yacc
  • appdirs for reading config file in a cross-platform manner

To do

  • format(timepoint, fmt) (in keyword) units given current time field
  • add custom/OS locale support (?)
  • add tab-completion for:
    • months
    • units given current datetime field or RHS of in keyword
  • "days until winter"
  • run tests across a variety of locales
  • add show function
    • show clock for time
    • show cal for date and basedate

Examples

The following examples are generated based on tests run, so many results will be relative to the day it was tested. Every expression on the left side is valid syntax.

(T-1d).weekday returns Saturday

(n + 180d)-180d == n returns True

(n + 181d)-180d != n returns True

(t + 180d)-180d == t returns True

-1d.weekday returns Saturday

08h30 returns 8:30:00

1 in unix returns 1

n - 1234 returns 18945 days, 14:48:22.242742

10h30 + 14h returns 1 day, 0:30:00

2021 feb 14 12:00:00 returns 2021-02-14 12:00:00

seconds until 2021 feb 14 12:00:00 returns -23587736.48

1-1-1 23:23:23 returns 0001-01-01 23:23:23

1-1-1 23:23m returns 0001-01-01 23:23:00

1-1-1 23h:23 returns 0001-01-01 23:23:00

1-1-1 23h:23m returns 0001-01-01 23:23:00

1-1-1 23m:23 returns 0001-01-01 00:23:23

1-1-1 23m:23s returns 0001-01-01 00:23:23

1-1-1 23m:23S returns 0001-01-01 00:23:23

1-1-1 23:23S returns 0001-01-01 00:23:23

11h:20 AM returns 11:20:00

11m:20 PM returns 00:11:20

11h:20 am returns 11:20:00

11h:20m pm returns 23:20:00

11:20s PM returns 00:11:20

2014 Jan 13 returns 2014-01-13

2014 January 13 returns 2014-01-13

1996.04.28 returns 1996-04-28

22h:22 returns 22:22:00

22:22:22 returns 22:22:22

22h:22m:22 returns 22:22:22

22h:22m:22s returns 22:22:22

22:22m:22s returns 22:22:22

22h:22:22s returns 22:22:22

22:22:22s returns 22:22:22

2020-Jan-27 returns 2020-01-27

6 pm returns 18:00:00

6 pm + 1h returns 19:00:00

6pm returns 18:00:00

22m:22 + 4h returns 4:22:22

1-1-1-1-1-1 returns 0:00:00

1610494238 returns 2021-01-12 20:30:38

1610494238+4h.weekday returns Wednesday

1610494238.weekday returns Tuesday

12h:00 AM != 12h:00 PM returns True

2014 Jan 13==2014 January 13 returns True

1957-12-26 - t returns -23334 days, 0:00:00

1957-12-26 22:22:22 - t returns -23334 days, 22:22:22

1958-05-14 - 1958-05-16 returns -2 days, 0:00:00

1d in hours returns 24.00

1d in minutes returns 1440.00

1d in seconds returns 86400.00

1d returns 1 day, 0:00:00

1d+0h22m returns 1 day, 0:22:00

1d1m in hours returns 24.02

1970 Jan 1 - 3h in unix returns 0

1w returns 7 days, 0:00:00

2020 Jan 27 + 1y == 2021 Jan 27 returns True

2 < 1 returns False

12h:00 pm != 12h:00 am returns True

22h+2m returns 22:02:00

22h22m returns 22:22:00

1y2M returns 1 year, 2 months

0y2M returns 2 months

-1y2M returns -1 year, -2 months

1M1d returns 1 month, 1 day, 0:00:00

2h2m returns 2:02:00

3h+3M returns 3 months, 3:00:00

3M returns 3 months

T-1.5d returns 2021-11-12 12:00:00

T-10d returns 2021-11-04

T.day returns 14

T.weekday returns Sunday

YD.day returns 13

n returns 2021-11-14 12:09:01.677980

next Sunday returns 2021-11-21

2000-10-10 00:16 returns 2000-10-10 00:16:00

2000-10-10 16:00 returns 2000-10-10 16:00:00

seconds until 3000 Apr 10 returns 30875341858.00

seconds since 3000 Apr 10 returns -30875341857.92

next Sunday == last sunday returns False

next Sunday != last sunday returns True

last Sunday == next sunday returns False

last Sunday != next sunday returns True

last sunday in 2021 returns 2021-12-26

first sunday in 2021 returns 2021-01-03

1st sunday in 2021 returns 2021-01-03

2012-12-13-3y.weekday returns Sunday

t - next Sunday returns -7 days, 0:00:00

wait .001s returns ``

weekday 0 returns Wednesday

Jan 2014 returns 2014-Jan

first friday in April 2014 returns 2014-04-04

1st friday in April 2014 returns 2014-04-04

first sun in April 2021 returns 2021-04-04

1st sun in April 2021 returns 2021-04-04

yd-5h returns 2021-11-12 19:00:00

1957-12-26 22:22:22 in unix returns -379118258

5m+5m returns 0:10:00

1h in seconds returns 3600.00

1 hour in seconds returns 3600.00

2s2s returns 0:00:04

1996 August 28 9 AM returns 1996-08-28 09:00:00

seconds until tomorrow returns 42656.03

seconds until 11 pm returns 39055.95

next month returns 2021-12-01

first friday in next month returns 2021-12-03

1st friday in next month returns 2021-12-03

first friday in april returns 2022-04-01

1st friday in april returns 2022-04-01

2014 01 returns 2014-Jan

6pm+1h returns 19:00:00

days until 2030-12-25 returns 3327.49

last fri in 2014 December returns 2014-12-26

last fri in 2014 Dec returns 2014-12-26

last fri in Dec 2014 returns 2014-12-26

yesterday==thursday returns False

yesterday==thu returns False

weekday tm returns Monday

(weekday t+100d) returns Tuesday

(weekday t+100d)==100d.weekday returns True

weekday t+100d returns Tuesday

monday+1d returns 2021-11-16

next mon + 1d returns 2021-11-16

next mon + 1d == next tue returns True

days until next mon returns 0.49

days until mon returns 0.49

today==mon returns False

seconds in 24h returns 86400.00

Jan 2014 + 1M returns 2014-02-01

2014 Jan + 1M returns 2014-02-01

-1d + 2020-10-10 returns 2020-10-09

2nd sunday in 2021 returns 2021-01-10

3rd sunday in 2021 returns 2021-01-17

4th sunday in 2021 returns 2021-01-24

5th sunday in 2021 returns 2021-01-31

4th wed in august returns 2021-08-25

august returns 2021-Aug

t 1am returns 2021-11-14 01:00:00

t 1:00 returns 2021-11-14 01:00:00

t 1:00 == t 1am returns True

(2020-10-10+1d) 3pm returns 2020-10-11 15:00:00

1am t returns 2021-11-14 01:00:00

1am t == t 1am returns True

t+1d 08h30 returns 2021-11-15 08:30:00

april+1M returns 2022-05-01

last sun in 2021 returns 2021-12-26

days until Jan 2030 returns 2969.49

2020-01-29 + (1 year + 1 month) returns 2021-02-28

friday day 13 in 2015.weekday returns Friday Friday Friday

friday day 13 in August 2021 returns 2021-08-13

friday day 13 in August 2021.weekday returns Friday

friday day < 8 in Jan 2015 returns 2015-01-02

friday day < 13 in 2014 sep returns 2014-09-05 2014-09-12

day 13 friday in 2021 returns 2021-08-13

1970 january 1st returns 1970-01-01

jan 1 + 99999M returns 8334-04-01

You might also like...
E-Ink Magic Calendar that automatically syncs to Google Calendar and runs off a battery powered Raspberry Pi Zero
E-Ink Magic Calendar that automatically syncs to Google Calendar and runs off a battery powered Raspberry Pi Zero

E-Ink Magic Calendar that automatically syncs to Google Calendar and runs off a battery powered Raspberry Pi Zero

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

This application is the basic of automated online-class-joiner(for YıldızEdu) within the right time. Gets the ZOOM link by scheduled date and time.
This application is the basic of automated online-class-joiner(for YıldızEdu) within the right time. Gets the ZOOM link by scheduled date and time.

This application is the basic of automated online-class-joiner(for YıldızEdu) within the right time. Gets the ZOOM link by scheduled date and time.

Provide partial dates and retain the date precision through processing

Prefix date parser This is a helper class to parse dates with varied degrees of precision. For example, a data source might state a date as 2001, 2001

Advance Image Downloader/Extractor (Job) is a Python-Flask web-based app, which will help the user download the any kind of Images at any date and time over the internet. These images will get downloaded as a job and then let user know that the images have been downloaded by sending them a link over an email. A ULauncher/Albert extension that supports currency, units and date time conversion, as well as a calculator that supports complex numbers and functions.
A ULauncher/Albert extension that supports currency, units and date time conversion, as well as a calculator that supports complex numbers and functions.

Ulauncher/Albert Calculate Anything Ulauncher/Albert Calculate Anything is an extension for Ulauncher and Albert to calculate things like currency, ti

 Integrate bus data from a variety of sources (batch processing and real time processing).
Integrate bus data from a variety of sources (batch processing and real time processing).

Purpose: This is integrate bus data from a variety of sources such as: csv, json api, sensor data ... into Relational Database (batch processing and r

A faster and highly-compatible implementation of the Python programming language. The code here is out of date, please follow our blog

Pyston is a faster and highly-compatible implementation of the Python programming language. Version 2 is currently closed source, but you can find the

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

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

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

Date Time Userbot With Python
Date Time Userbot With Python

DATE_TIME_USERBOT An Telegram Bot By @Pythone_3 Config Vars API_ID : Telegram API_ID, get it from my.telegram.org/apps API_HASH : Telegram API_ID, get

LegalNLP - Natural Language Processing Methods for the Brazilian Legal Language

LegalNLP - Natural Language Processing Methods for the Brazilian Legal Language ⚖️ The library of Natural Language Processing for Brazilian legal lang

A design of MIDI language for music generation task, specifically for Natural Language Processing (NLP) models.

MIDI Language Introduction Reference Paper: Pop Music Transformer: Beat-based Modeling and Generation of Expressive Pop Piano Compositions: code This

Kaldi-compatible feature extraction with PyTorch, supporting CUDA, batch processing, chunk processing, and autograd

Kaldi-compatible feature extraction with PyTorch, supporting CUDA, batch processing, chunk processing, and autograd

Code and data form the paper BERT Got a Date: Introducing Transformers to Temporal Tagging

BERT Got a Date: Introducing Transformers to Temporal Tagging Satya Almasian*, Dennis Aumiller*, and Michael Gertz Heidelberg University Contact us vi

A simple python script that, given a location and a date, uses the Nasa Earth API to show a photo taken by the Landsat 8 satellite. The script must be executed on the command-line.
A simple python script that, given a location and a date, uses the Nasa Earth API to show a photo taken by the Landsat 8 satellite. The script must be executed on the command-line.

What does it do? Given a location and a date, it uses the Nasa Earth API to show a photo taken by the Landsat 8 satellite. The script must be executed

A Django api to display items and their current up-to-date prices from different online retailers in one platform.

A Django api to display items and their current up-to-date prices from different online retailers in one platform. Utilizing scrapy to periodically scrape the latest prices from different online retailers. Store in a PostgreSQL database and make available via an API.

 A curated list of the latest breakthroughs in AI by release date with a clear video explanation, link to a more in-depth article, and code.
A curated list of the latest breakthroughs in AI by release date with a clear video explanation, link to a more in-depth article, and code.

A curated list of the latest breakthroughs in AI by release date with a clear video explanation, link to a more in-depth article, and code

Comments
  • "seconds since 1970 january 1st" query crashes the app

    Hi, When I type "seconds since 1970 january 1st", the app crashes with the following stack trace:

    seconds since 1970 january 1st
    st is not loaded
    Traceback (most recent call last):
      File "/usr/local/Caskroom/miniconda/base/lib/python3.8/cmd.py", line 214, in onecmd
        func = getattr(self, 'do_' + cmd)
    AttributeError: 'CmdParse' object has no attribute 'do_seconds'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/Caskroom/miniconda/base/bin/dte", line 794, in p_expression_expression_point
        p[0] = datetime.combine(p[1], p[2])
    TypeError: combine() argument 2 must be datetime.time, not None
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/Caskroom/miniconda/base/bin/dte", line 1357, in <module>
        interactive()
      File "/usr/local/Caskroom/miniconda/base/bin/dte", line 1346, in interactive
        CmdParse().cmdloop()
      File "/usr/local/Caskroom/miniconda/base/lib/python3.8/cmd.py", line 138, in cmdloop
        stop = self.onecmd(line)
      File "/usr/local/Caskroom/miniconda/base/lib/python3.8/cmd.py", line 216, in onecmd
        return self.default(line)
      File "/usr/local/Caskroom/miniconda/base/bin/dte", line 1338, in default
        yacc.parse(line)
      File "/usr/local/Caskroom/miniconda/base/lib/python3.8/site-packages/ply/yacc.py", line 333, in parse
        return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
      File "/usr/local/Caskroom/miniconda/base/lib/python3.8/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
        p.callable(pslice)
      File "/usr/local/Caskroom/miniconda/base/bin/dte", line 796, in p_expression_expression_point
        p[0] = datetime.combine(p[2], p[1])
    TypeError: combine() argument 1 must be datetime.date, not None`
    ```
    bug 
    opened by ahmedahmedov 1
  • can't do last day in month

    can't do last day in month

    dte last sun in 2021
    2021-12-26
    
    dte last day in Jan 2022
    yacc: Syntax error at line 1, token=NAME
    ...
    
    dte last day 2022-01
    yacc: Syntax error at line 1, token=NAME
    2022-Jan
    
    dte 2022-02-01 - 1 day 
    2022-01-31
    
    dte 2022-Feb - 1 day 
    error:...
    
    dte 2022-02 - 1 day 
    2022-01-31
    
    opened by honzajde 0
  • Months and years can't be converted

    Months and years can't be converted

    1M in days
    Traceback (most recent call last):
      File "/usr/local/lib/python3.8/cmd.py", line 214, in onecmd
        func = getattr(self, 'do_' + cmd)
    AttributeError: 'CmdParse' object has no attribute 'do_1M'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/yggdrasil/.local/bin/dte", line 1364, in <module>
        interactive()
      File "/home/yggdrasil/.local/bin/dte", line 1353, in interactive
        CmdParse().cmdloop()
      File "/usr/local/lib/python3.8/cmd.py", line 138, in cmdloop
        stop = self.onecmd(line)
      File "/usr/local/lib/python3.8/cmd.py", line 216, in onecmd
        return self.default(line)
      File "/home/yggdrasil/.local/bin/dte", line 1345, in default
        yacc.parse(line)
      File "/usr/home/yggdrasil/.local/pipx/venvs/dte/lib/python3.8/site-packages/ply/yacc.py", line 333, in parse
        return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
      File "/usr/home/yggdrasil/.local/pipx/venvs/dte/lib/python3.8/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
        p.callable(pslice)
      File "/home/yggdrasil/.local/bin/dte", line 1283, in p_statement_expression_in_unit
        p[0] = delta_to_unit(p[1], p[3].lower())
      File "/home/yggdrasil/.local/bin/dte", line 1196, in delta_to_unit
        total_seconds = delta.total_seconds()
    AttributeError: 'relativedelta' object has no attribute 'total_seconds'
    
    1s in years
    Traceback (most recent call last):
      File "/usr/local/lib/python3.8/cmd.py", line 214, in onecmd
        func = getattr(self, 'do_' + cmd)
    AttributeError: 'CmdParse' object has no attribute 'do_1s'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/yggdrasil/.local/bin/dte", line 1364, in <module>
        interactive()
      File "/home/yggdrasil/.local/bin/dte", line 1353, in interactive
        CmdParse().cmdloop()
      File "/usr/local/lib/python3.8/cmd.py", line 138, in cmdloop
        stop = self.onecmd(line)
      File "/usr/local/lib/python3.8/cmd.py", line 216, in onecmd
        return self.default(line)
      File "/home/yggdrasil/.local/bin/dte", line 1345, in default
        yacc.parse(line)
      File "/usr/home/yggdrasil/.local/pipx/venvs/dte/lib/python3.8/site-package
    s/ply/yacc.py", line 333, in parse
        return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
      File "/usr/home/yggdrasil/.local/pipx/venvs/dte/lib/python3.8/site-package
    s/ply/yacc.py", line 1120, in parseopt_notrack
        p.callable(pslice)
      File "/home/yggdrasil/.local/bin/dte", line 1283, in p_statement_expressio
    n_in_unit
        p[0] = delta_to_unit(p[1], p[3].lower())
      File "/home/yggdrasil/.local/bin/dte", line 1208, in delta_to_unit
        raise Exception('Invalid conversion')
    Exception: Invalid conversion
    
    help wanted 
    opened by yggdr 1
Owner
Marcelo
Marcelo
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
⌚️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
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
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
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
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
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
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
pytz Python historical timezone library and database

pytz Brings the IANA tz database into Python. This library allows accurate and cross platform timezone calculations. pytz contains generated code, and

Stub 236 Jan 3, 2023
Generate and work with holidays in Python

python-holidays A fast, efficient Python library for generating country, province and state specific sets of holidays on the fly. It aims to make dete

Maurizio Montel 881 Dec 29, 2022