time-helper


Nametime-helper JSON
Version 0.1.5 PyPI version JSON
download
home_pagehttps://github.com/felixnext/python-time-helper
SummaryVarious Helper Tools to handle different time data
upload_time2022-11-24 11:42:40
maintainer
docs_urlNone
authorFelix Geilert
requires_python
licenseMIT License
keywords time handling
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Time Helper

Simple helper library to handle different time related tasks in python. This ties into pandas and numpy as well as pytz.

The general idea is to have a bunch of one-stop functions that allow you to easily handle datetime related tasks.

## Getting Started

```bash
pip install time-helper
```

Then in python code:

```python
from time_helper import make_aware

make_aware("2022-03-10")
# > datetime.datetime(2022, 3, 10, 0, 0, tzinfo=backports.zoneinfo.ZoneInfo(key='CET'))
```

## Library Logic

The library is build to extend around various datetime objects (such as python internal datetime, date, as well as np.datetime).
It provides a bunch of helper functions that are grouped into various categories:

### Convert

This gets datetimes in and out of the library. This includes a range of functions for converting strings and different datetime types into canonical py-datetime types:

```python
import time_helper as th

# convert a unix datetime
dt = th.unix_to_datetime(1649491287)
dt = th.any_to_datetime(1649491287)

# convert string to datetime
dt = th.any_to_datetime("2022-03-19")
dt = th.any_to_datetime("2022-03-19 20:15")

# convert a date to datetime
from datetime import date
dt = th.any_to_datetime(date(2022, 3, 10))
```

It also allows to easily switch between aware and unaware datetime:

```python
dt = th.any_to_datetime("2022-03-10")
aware_dt = th.make_aware(dt)
aware_dt = th.make_aware(dt, "UTC")
unaware_dt = th.make_unaware(dt)
unaware_dt = th.make_unaware(dt, "UTC")
```

Note that for `make_unaware` you can still provide a datetime. In that case the datetime is first converted into the regarding timezone before the
timezone information is removed from the object. You can also explicitly convert the timezone with `localize_datetime`.

### Operations & Ranges

The library also defines a range of operations to make working with timezones easier.
These include handling modifications of a single datetime:

```python
day = th.round_time(dt, "D")
# results in: datetime.datetime(2022, 3, 10, 0, 0)
day = th.round_time(dt, "D", max_out=True)
# results in: datetime.datetime(2022, 3, 10, 23, 59, 59, 999999)
has_tz = th.has_timezone(aware_dt)
has_tz = th.has_timezone(unaware_dt)

# compute a diff (between aware and unaware datetimes)
diff = th.time_diff(
    th.round_time(aware_dt, "M", max_out=True),
    unaware_dt
)
# results in: datetime.timedelta(seconds=3659, microseconds=999999)
```

It also supports a bunch of range operations:

```python
# converts the time into a interval (float) value for the defined range
# default i
dt = datetime(2022, 3, 12, 12, 0)
pos = th.time_to_interval(dt, offset=0)
# results in: 0.0 (as it is noon)
pos = th.time_to_interval(dt, offset=0, zero_center=False)
# results in: 0.5 (as half day is gone and center is no longer zeroed)

# create interval tuples
ivs = th.create_intervals(dt, dt + timedelta(days=2), interval=1)
ivs = th.create_intervals(dt, dt + timedelta(days=2), interval=timedelta(minutes=30))
```

### Wrapper

This library also provides a wrapper class to make all functions more accessible and first class citizens of the system.

> **Note:** This part of the library is still under construction.

## Notes

There is still a lot to improve on this library, please feel free to create PRs or contact me if you wish to contribute!

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/felixnext/python-time-helper",
    "name": "time-helper",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "time handling",
    "author": "Felix Geilert",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/b7/ad/0a3e54aab73114912c5817d12cd838f9be94c4e23d5f7bd83c83eb947601/time-helper-0.1.5.tar.gz",
    "platform": null,
    "description": "# Time Helper\n\nSimple helper library to handle different time related tasks in python. This ties into pandas and numpy as well as pytz.\n\nThe general idea is to have a bunch of one-stop functions that allow you to easily handle datetime related tasks.\n\n## Getting Started\n\n```bash\npip install time-helper\n```\n\nThen in python code:\n\n```python\nfrom time_helper import make_aware\n\nmake_aware(\"2022-03-10\")\n# > datetime.datetime(2022, 3, 10, 0, 0, tzinfo=backports.zoneinfo.ZoneInfo(key='CET'))\n```\n\n## Library Logic\n\nThe library is build to extend around various datetime objects (such as python internal datetime, date, as well as np.datetime).\nIt provides a bunch of helper functions that are grouped into various categories:\n\n### Convert\n\nThis gets datetimes in and out of the library. This includes a range of functions for converting strings and different datetime types into canonical py-datetime types:\n\n```python\nimport time_helper as th\n\n# convert a unix datetime\ndt = th.unix_to_datetime(1649491287)\ndt = th.any_to_datetime(1649491287)\n\n# convert string to datetime\ndt = th.any_to_datetime(\"2022-03-19\")\ndt = th.any_to_datetime(\"2022-03-19 20:15\")\n\n# convert a date to datetime\nfrom datetime import date\ndt = th.any_to_datetime(date(2022, 3, 10))\n```\n\nIt also allows to easily switch between aware and unaware datetime:\n\n```python\ndt = th.any_to_datetime(\"2022-03-10\")\naware_dt = th.make_aware(dt)\naware_dt = th.make_aware(dt, \"UTC\")\nunaware_dt = th.make_unaware(dt)\nunaware_dt = th.make_unaware(dt, \"UTC\")\n```\n\nNote that for `make_unaware` you can still provide a datetime. In that case the datetime is first converted into the regarding timezone before the\ntimezone information is removed from the object. You can also explicitly convert the timezone with `localize_datetime`.\n\n### Operations & Ranges\n\nThe library also defines a range of operations to make working with timezones easier.\nThese include handling modifications of a single datetime:\n\n```python\nday = th.round_time(dt, \"D\")\n# results in: datetime.datetime(2022, 3, 10, 0, 0)\nday = th.round_time(dt, \"D\", max_out=True)\n# results in: datetime.datetime(2022, 3, 10, 23, 59, 59, 999999)\nhas_tz = th.has_timezone(aware_dt)\nhas_tz = th.has_timezone(unaware_dt)\n\n# compute a diff (between aware and unaware datetimes)\ndiff = th.time_diff(\n    th.round_time(aware_dt, \"M\", max_out=True),\n    unaware_dt\n)\n# results in: datetime.timedelta(seconds=3659, microseconds=999999)\n```\n\nIt also supports a bunch of range operations:\n\n```python\n# converts the time into a interval (float) value for the defined range\n# default i\ndt = datetime(2022, 3, 12, 12, 0)\npos = th.time_to_interval(dt, offset=0)\n# results in: 0.0 (as it is noon)\npos = th.time_to_interval(dt, offset=0, zero_center=False)\n# results in: 0.5 (as half day is gone and center is no longer zeroed)\n\n# create interval tuples\nivs = th.create_intervals(dt, dt + timedelta(days=2), interval=1)\nivs = th.create_intervals(dt, dt + timedelta(days=2), interval=timedelta(minutes=30))\n```\n\n### Wrapper\n\nThis library also provides a wrapper class to make all functions more accessible and first class citizens of the system.\n\n> **Note:** This part of the library is still under construction.\n\n## Notes\n\nThere is still a lot to improve on this library, please feel free to create PRs or contact me if you wish to contribute!\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Various Helper Tools to handle different time data",
    "version": "0.1.5",
    "project_urls": {
        "Download": "https://github.com/felixnext/python-time-helper/releases/tag/v0.1.0-alpha",
        "Homepage": "https://github.com/felixnext/python-time-helper"
    },
    "split_keywords": [
        "time",
        "handling"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "36ad4bb4b971bab3729fe8ac8dcdab65e8ca134c959331bebae8dda9cc228218",
                "md5": "3bf8606a5e3a85333a90e2414a331bd3",
                "sha256": "ce29d5048e403493ea42728d863b32b9570d460b1b50cdabb6280664578601fd"
            },
            "downloads": -1,
            "filename": "time_helper-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3bf8606a5e3a85333a90e2414a331bd3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11535,
            "upload_time": "2022-11-24T11:42:38",
            "upload_time_iso_8601": "2022-11-24T11:42:38.873833Z",
            "url": "https://files.pythonhosted.org/packages/36/ad/4bb4b971bab3729fe8ac8dcdab65e8ca134c959331bebae8dda9cc228218/time_helper-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b7ad0a3e54aab73114912c5817d12cd838f9be94c4e23d5f7bd83c83eb947601",
                "md5": "36d095192791a82a0793ac338cb76795",
                "sha256": "2d93b6e0ff54e8c854f37b71c9a7b15f5dc5d6852068b85b29a19d0b9edb98b4"
            },
            "downloads": -1,
            "filename": "time-helper-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "36d095192791a82a0793ac338cb76795",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 11416,
            "upload_time": "2022-11-24T11:42:40",
            "upload_time_iso_8601": "2022-11-24T11:42:40.169831Z",
            "url": "https://files.pythonhosted.org/packages/b7/ad/0a3e54aab73114912c5817d12cd838f9be94c4e23d5f7bd83c83eb947601/time-helper-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-11-24 11:42:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "felixnext",
    "github_project": "python-time-helper",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "time-helper"
}
        
Elapsed time: 0.70971s