{"id":15583469,"url":"https://github.com/davidmarquis/pyperiods","last_synced_at":"2025-03-29T08:42:52.212Z","repository":{"id":62582448,"uuid":"75435481","full_name":"davidmarquis/pyperiods","owner":"davidmarquis","description":"A Python library for representing and manipulating periods of time such as years and months.","archived":false,"fork":false,"pushed_at":"2020-08-03T18:09:49.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-05T11:06:13.362Z","etag":null,"topics":["manipulating-periods","months","python-library","years"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidmarquis.png","metadata":{"files":{"readme":"README","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-02T22:21:48.000Z","updated_at":"2020-08-03T17:56:03.000Z","dependencies_parsed_at":"2022-11-03T20:17:55.356Z","dependency_job_id":null,"html_url":"https://github.com/davidmarquis/pyperiods","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmarquis%2Fpyperiods","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmarquis%2Fpyperiods/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmarquis%2Fpyperiods/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmarquis%2Fpyperiods/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidmarquis","download_url":"https://codeload.github.com/davidmarquis/pyperiods/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246162105,"owners_count":20733352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["manipulating-periods","months","python-library","years"],"created_at":"2024-10-02T20:08:31.842Z","updated_at":"2025-03-29T08:42:52.193Z","avatar_url":"https://github.com/davidmarquis.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pyperiods\n\nA Python library for representing and manipulating specific periods in time such as years and year/month.\n\n## How to Install?\n\n`pyperiods` is available on [Pypi](https://pypi.python.org/pypi/pyperiods):\n\n```\n   pip install pyperiods\n```\n\n## Some features\n\n - Both specific years (ex: 2012) and specific months (ex: January 2012) can be represented, manipulated and compared.\n - Simple add/subtract operations are supported (`month + 1` = next month)\n - Utilities for iterations (i.e.: iterate through all previous 12 months)\n - A Django ModelField is provided for storing months/years in Django models\n - A Django REST Framework serializer is provided for convenience as well\n - Compatible with **Python 3** only\n\n## Concepts\n\nThe main concept of the library is a **Period**, an abstract base class which can represent either a specific year (ex: 2012), or a specific month and its year (ex: January 2012).\n\nSpecific years are represented by instances of the **`YearPeriod`** class while specific months are represented by instances of the **`MonthPeriod`** class.\n\nPeriods are comparable and can be sorted. All periods have a string representation which corresponds to either the year only as 4 digits (ex: `2012`) or the year and the month as 6 digits (ex: `201201`)\n\n\n## Examples\n\nThe basics:\n\n``` python\n\u003e\u003e\u003e from pyperiods.months import MonthPeriod\n\u003e\u003e\u003e start = MonthPeriod(year=2016, month=5)\n\u003e\u003e\u003e start.year\n2016\n\u003e\u003e\u003e start.month\n5\n\u003e\u003e\u003e next_month = start + 1\n\u003e\u003e\u003e str(next_month)\n'201606'\n\u003e\u003e\u003e next_month.format_long()\n'June 2016'\n\u003e\u003e\u003e next_month.first_day()\ndatetime.date(2016, 5, 1)\n\u003e\u003e\u003e next_month.last_day()\ndatetime.date(2016, 5, 30)\n\n\u003e\u003e\u003e from pyperiods.years import YearPeriod\n\u003e\u003e\u003e current_year = YearPeriod()\n\u003e\u003e\u003e str(current_year)\n'2016'\n\u003e\u003e\u003e list(current_year.months)\n[201601, 201602, 201603, 201604, 201605, 201606, 201607, 201608, 201609, 201610, 201611, 201612]\n\n```\n\n### Basic arithmetic\n\n``` python\n\u003e\u003e\u003e MonthPeriod('201611') + 2\n201701\n\u003e\u003e\u003e MonthPeriod('201611') - 12\n201511\n```\n\n### Sorting \u0026 Comparison\n\n``` python\n\u003e\u003e\u003e periods = [\n        MonthPeriod('201607'),\n        MonthPeriod('201601'),\n        MonthPeriod('201501'),\n        MonthPeriod('201507'),\n    ]\n\u003e\u003e\u003e list(periods.sort())\n[201501, 201507, 201601, 201607]\n\u003e\u003e\u003e MonthPeriod('201502') \u003c MonthPeriod('201601')\nTrue\n\u003e\u003e\u003e MonthPeriod('201502') == MonthPeriod('201503') - 1\nTrue\n```\n\n### Iteration\n\n``` python\n\u003e\u003e\u003e list(MonthPeriod('201605').range(stop=6))\n[201605, 201606, 201607, 201608, 201609, 201610, 201611]\n\u003e\u003e\u003e list(MonthPeriod('201605').range(start=-2))\n[201603, 201604, 201605]\n\u003e\u003e\u003e list(MonthPeriod('201605').range(start=-2, stop=2))\n[201603, 201604, 201605, 201606, 201607]\n```\n\n### Creation\n\n``` python\n\u003e\u003e\u003e MonthPeriod('201605')\n201605\n\u003e\u003e\u003e MonthPeriod(year=2016)\n201601\n\u003e\u003e\u003e MonthPeriod(year=2016, month=2)\n201602\n\u003e\u003e\u003e YearPeriod(2016)\n2016\n\n\u003e\u003e\u003e from pyperiods.factory import period_from_string\n\u003e\u003e\u003e type(period_from_string('2016'))\n\u003cclass 'pyperiods.years.YearPeriod'\u003e\n\u003e\u003e\u003e type(period_from_string('201612'))\n\u003cclass 'pyperiods.months.MonthPeriod'\u003e\n```\n\n## APIs\n\nSee all unit tests in the `tests` folder for details on supported APIs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidmarquis%2Fpyperiods","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidmarquis%2Fpyperiods","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidmarquis%2Fpyperiods/lists"}