Git Product home page Git Product logo

ordinal-date-util's Introduction

ordinal-date-util

A utility to return the date of the first, second, third, fourth, or fifth day (monday, tues, ... etc.) of a given month and year.

This could be used within a scheduling library or otherwise place where a user might need to find dates for phrases like "find the date of first monday of next month"

Abstracting the 'human readable' concept to a more general usage, the API is built to enable users to ask the more general question of getting the date from an ordinal request. For example we might ask for the date of the first Monday in December 2020.

To enable this I created the primary api as follows...

def get_date_of(ordinal=None, day=None, month=None, year=None, date=None) -> Optional[datetime.date]:

Relating this back to the example sentence we wrote above, we can read the function as...

get the date of (*ordinal* *day* in *month* of *year*)

This returns either the date you asked for, or None if that request can't be found during a given month. For example, a request for the seventh Monday in January, 2020 would return a None object because there is no seventh Monday in that month and year.

A successful request with result in a datatime.date object such as follows...

>>>>from date_util import *
>>>>get_date_of(ordinal.FIRST, calendar.MONDAY, month.JANUARY, 2020)
datetime.date(2020, 1, 6)

NOTE:

There are 3 parameters in the header that are need to be considered with care.

These are the date, and the year / month. You should consider these to be exclusive. Either pass a date, or pass both a month and year.

If you pass the date field, you should pass in either a datetime.date object or a datetime.datetime object. This date should be from the month you are interested in. The day you set in the either of those inputs will be ignored.

Additionally, you should note that if you happen to pass the both the month / year and the data parameters, the month / year will override the date parameter and the date parameter will be ignored.

As an example, if you would like to get the second tuesday in January of 2020, you can call the function as follows.

date = get_date_of(ordinal.THIRD, calendar.MONDAY, 0, 0)

or

date = get_date_of(ordinal=ordinal.FIRST, day=calendar.MONDAY, month=month.JANUARY, year=2019)

or

date = get_date_of(ordinal=ordinal.FIRST, day=calendar.MONDAY, date=datetime.date(year=2019, month=month.JANUARY, day=1))

Check the tests for use.

ordinal-date-util's People

Contributors

zksnark avatar

Watchers

 avatar

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.