PyTime is an easy-use Python module which aims to operate date/time/datetime by string.

Overview

PyTime

Build Status PyPI version Supported Python versions

PyTime is an easy-use Python module which aims to operate date/time/datetime by string.

PyTime allows you using nonregular datetime string to generate and calculate datetime at most situation.

It Also provides you some simple useful methods for getting datetime you want.

Install

pip install pytime

Sample Usage

Calculate datetime all by string, accept unit for short or overall length, support out of order or in capital unit:

>>>from pytime import pytime
>>>
>>>pytime.before('2015.5.17', '2years 3mon 23week 3d 2hr')     # 2years 3months 23weeks 3days 2hours before 2015.5.17
datetime.datetime(2012, 9, 5, 22, 0)
>>>
>>>pytime.after(pytime.tomorrow('15.5.17'), '23month3dy29minu')   # 23months 3days 29minutes after 2015-5-17's next day
datetime.datetime(2017, 4, 21, 0, 29)

Parse nonregular datetime string to datetime:

>>>pytime.parse('April 3rd 2015')
datetime.date(2015, 4, 3)
>>>pytime.parse('Oct, 1st. 2015')
datetime.date(2015, 10, 1)
>>>pytime.parse('NOV21th2015')
datetime.date(2015, 11, 21)
>>>
>>>pytime.parse('2015517')
datetime.date(2015, 5, 17)
>>>
>>>pytime.parse('5/17/15')
datetime.date(2015, 5, 17)
>>>
>>>pytime.parse('92-11-2')
datetime.date(1992, 11, 2)
>>>
>>>pytime.parse(1432310400)          # support datetimestamp for all function
datetime.datetime(2015, 5, 23, 0, 0)

Calculate week and month in a easy way. Normally we want to use these binate date in script, from Monday to next Monday which means 00:00:00-23:59:59, and Month from 1st to next 1st in the same way, but if you want clean date interval, you could set pytime.next_week(clean=True) to get the clean date for next week etc.

>>>pytime.this_week()
(datetime.date(2015, 5, 11), datetime.date(2015, 5, 18))

>>>pytime.next_week('2015-6-14')                         # 2015-6-14's next week for script
(datetime.date(2015, 6, 15), datetime.date(2015, 6, 23))

>>>pytime.last_week(pytime.mother(2013), True)           # 2013 Mother's Day's last week
(datetime.date(2013, 5, 13), datetime.date(2013, 5, 20))

>>>pytime.next_month('2015-10-1')
(datetime.date(2015, 11, 1), datetime.date(2015, 12, 1))

Pytime support calculate timedelta by string even between date and datetime, merely set the date to that day's midnight:

>>>pytime.count('2015-5-17 23:23:23', pytime.tomorrow())
datetime.timedelta(1, 84203)

Get common festivals for designated year:

>>>pytime.father()              # Father's Day
datetime.date(2015, 6, 21)
>>>
>>>pytime.mother(2016)          # 2016 Mother's Day
datetime.date(2016, 5, 8)
>>>
>>>pytime.easter(1999)          # 1999 Easter
datetime.date(1999, 4, 4)
>>>pytime.vatertag(2020)        # Fater's Day in Germany
datetime.date(2020, 5, 21)

Get days between two date.

>>>pytime.days_range('2015-5-17', '2015-5-23')
[datetime.date(2015, 5, 23),
 datetime.date(2015, 5, 22),
 datetime.date(2015, 5, 21),
 datetime.date(2015, 5, 20),
 datetime.date(2015, 5, 19),
 datetime.date(2015, 5, 18),
 datetime.date(2015, 5, 17)]

...

and other useful methods.

Contributors

License

MIT

You might also like...
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

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

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.

Cross Platform Application for Calculating Render Time
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

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

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

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

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

python parser for human readable dates
python parser for human readable dates

Python parser for human readable dates Key Features • How To Use • Installation • Common use cases • You may also like... • License Key Features Suppo

Comments
  • Want to add improvements and fix some errors

    Want to add improvements and fix some errors

    Hi @shinux . First of all, very nice module. Lot of handy methods to use for date and time manipulation. I know last commit was years ago, but I would like to fix some things because I find this module useful and really easy to read. Also, it would be my first contribution to open source.

    Here is what I found digging through PyTime, first some minor mistakes:

    1. At the end of filter.py, in method from_str, there is typo at last line, raise CanNotFormatError('Not well formated. Expecting something like May,21st.2015'). formated should be formatted
    2. At the end of filter.py, at if __name__ == '__main__':, there is PEP 8 violation. PEP 8: expected 2 blank lines after class or function definition, found 1. Missing one more blank line
    3. In function daysrange, parameter wipe is not documented.

    Now second list may or may not be problems, you decide.

    1. In test_normal.py, in method test_function, this7 and this8 fail. On my machine, I setup this so it can show test as your tested, +2h offset I guess. I think you wanted to get from pytime.yesterday(1432310400 + gmt8offset) -> datetime.datetime(2015, 5, 22, 0, 0). And you get it, but you are comparing it to datetime.datetime(2015, 5, 22) which returns false, because tuple comparison fails here. Similiar is for this8.
    2. Maybe better aproach would be to rename daysrange to days_range, newyear to new_year, christeve to christ_eve and lastday to last_day. That is because words should be split with underscore by conventions of python using snake case, and you already use that convention for this_week, last_week, next_month and so on.
    3. In filter.py, in method __parse_not_only_str, function word usage in for loop is too broad term to be used. Maybe rename it to baseParser_function. Found some problems with pycharm too. link here

    There are some other things, but I would like to change some of those mentioned above if you would allow me to make pull request. Thank you for your time.

    opened by dinko-pehar 8
  • pytime.father() wrong for non-US countries

    pytime.father() wrong for non-US countries

    pytime.father(2020) gives me 2020-06-21 pytime.father(2021) gives me 2021-06-20

    But for germany that day is on the 2020-05-21 / 2021-05-13.

    Is there any way to get the fatherday for other countries?

    enhancement 
    opened by Marmeladenbrot 4
  • Parsing date like May,21st.2015

    Parsing date like May,21st.2015

    I created an different method called from_str to parse this type of date. Also, this method is linked in the main function of BaseParser.

    You can see the change that I did in the old "parse_diff" method to call all methods that could try parse the value (from_str and parse_diff). This could be simpler if the method parse_diff doesn't exists, but I didn't removed cause I don't where you could use (I checked the tests and no one call it).

    Anyway, if it's something wrong please tell me. Tks

    enhancement 
    opened by felipevolpone 4
  • Confusion about month abbreviations

    Confusion about month abbreviations

    Thank you very much for checking my issue. I found that August was abbreviated as Agu instead of Aug when I used it, which made me very confused. I want to ask why this is? example found in filter.py file: NAMED_MONTHS = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Agu': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}

    opened by weiqingcai 1
Owner
Sinux
Through the shadow.
Sinux
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

null 30 Dec 30, 2022
Make Python datetime formatting human readable

Make Python datetime formatting human readable

James Timmins 0 Oct 3, 2021
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
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
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
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
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