Git Product home page Git Product logo

pytime's Introduction

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

pytime's People

Contributors

dinkopehar avatar felipevolpone avatar fy0 avatar marksmayo avatar shinux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytime's Issues

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}

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.

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?

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.