# timespan
Utilities for working with popular timespan formats.
## .Net Timespans
Functions are provided for converting between .NET 7.0 TimeSpan objects
(with format specifiers) and Python's `datetime.timedelta` objects.
### `timespan.from_string(str) -> timedelta`
Converts a TimeSpan string in the current locale to a
`datetime.timedelta` object, e.g.
>>> import timespan
>>> timespan.c(3, 17, 25, 30, 500)
'3.17:25:30.5000000'
>>> timespan.from_string('3.17:25:30.5000000')
datetime.timedelta(days=3, seconds=62730, microseconds=500000)
#### Parameters
- `timespan_string`: TimeSpan string of any format and locale.
#### Return Value
A timedelta object.
### `timespan.to_string(specifier: str, *args: tuple) -> str`
Converts date\time information (variable-length tuple) to a TimeSpan
string in the current locale.
#### Parameters
- `specifier`: format specifier. Options are 'c', 'g' and 'G'.
- `args`: variable-length tuple (size 1, 3, 4 or 5) specifying
components of date and time.
#### Return Value
A TimeSpan string
### Notes
See the switch case of `_args_to_seconds()` in
[src/timespan/dotnet.py](src/timespan/dotnet.py) for full coverage of
all input types.
## Asterisk Timespans
Asterisk style timespans allow you to check if a timestamp falls within
a specified list of boundaries. For example, you might want to program
your phone system to only accept calls Mon-Fri from 9 a.m. to 5 p.m.
except on holidays like Christmas.
Timespans are specified in the form of `times|daysofweek|days|months`.
If your timespan starts with `!`, it'll only match if the timestamps
falls outside the given range.
Basic example:
import timespan
from datetime import datetime
business_hours = [
'9:00-17:00|mon-fri|*|*', # is between 9 a.m. to 5 p.m. on Mon to Fri
'!*|*|1|jan', # not new years
'!*|*|25|dec', # not christmas
'!*|thu|22-28|nov', # not thanksgiving
]
if timespan.match(business_hours, datetime.now()):
print "we're open for business!"
else:
print "sorry, we're closed :("
For more examples, see the documentation or source code.
Raw data
{
"_id": null,
"home_page": "https://github.com/jart/timespan",
"name": "timespan",
"maintainer": "",
"docs_url": null,
"requires_python": ">=2.7",
"maintainer_email": "",
"keywords": "",
"author": "Justine Tunney",
"author_email": "jtunney@gmail.com",
"download_url": "",
"platform": null,
"description": "# timespan\n\nUtilities for working with popular timespan formats.\n\n## .Net Timespans\n\nFunctions are provided for converting between .NET 7.0 TimeSpan objects\n(with format specifiers) and Python's `datetime.timedelta` objects.\n\n### `timespan.from_string(str) -> timedelta`\n\nConverts a TimeSpan string in the current locale to a\n`datetime.timedelta` object, e.g.\n\n >>> import timespan\n >>> timespan.c(3, 17, 25, 30, 500)\n '3.17:25:30.5000000'\n >>> timespan.from_string('3.17:25:30.5000000')\n datetime.timedelta(days=3, seconds=62730, microseconds=500000)\n\n#### Parameters\n\n- `timespan_string`: TimeSpan string of any format and locale.\n\n#### Return Value\n\nA timedelta object.\n\n### `timespan.to_string(specifier: str, *args: tuple) -> str`\n\nConverts date\\time information (variable-length tuple) to a TimeSpan\nstring in the current locale.\n\n#### Parameters\n\n- `specifier`: format specifier. Options are 'c', 'g' and 'G'.\n- `args`: variable-length tuple (size 1, 3, 4 or 5) specifying\n components of date and time.\n\n#### Return Value\n\nA TimeSpan string\n\n### Notes\n\nSee the switch case of `_args_to_seconds()` in\n[src/timespan/dotnet.py](src/timespan/dotnet.py) for full coverage of\nall input types.\n\n## Asterisk Timespans\n\nAsterisk style timespans allow you to check if a timestamp falls within\na specified list of boundaries. For example, you might want to program\nyour phone system to only accept calls Mon-Fri from 9 a.m. to 5 p.m.\nexcept on holidays like Christmas.\n\nTimespans are specified in the form of `times|daysofweek|days|months`.\nIf your timespan starts with `!`, it'll only match if the timestamps\nfalls outside the given range.\n\nBasic example:\n\n import timespan\n from datetime import datetime\n\n business_hours = [\n '9:00-17:00|mon-fri|*|*', # is between 9 a.m. to 5 p.m. on Mon to Fri\n '!*|*|1|jan', # not new years\n '!*|*|25|dec', # not christmas\n '!*|thu|22-28|nov', # not thanksgiving\n ]\n\n if timespan.match(business_hours, datetime.now()):\n print \"we're open for business!\"\n else:\n print \"sorry, we're closed :(\"\n\nFor more examples, see the documentation or source code.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Utilities for Asterisk and Microsoft .Net timespans",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/jart/timespan"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "678eeabdcfc51c2c54055134f7f34cffe2e426c95812dca18be0be6c4a902e4e",
"md5": "146f2667eb7d4cbcc8ef80d39b0243fd",
"sha256": "167dda4cf7667edb3c13c888287d1c077716f8817984e85cefdb149378061d17"
},
"downloads": -1,
"filename": "timespan-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "146f2667eb7d4cbcc8ef80d39b0243fd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=2.7",
"size": 9869,
"upload_time": "2023-07-13T06:51:09",
"upload_time_iso_8601": "2023-07-13T06:51:09.324650Z",
"url": "https://files.pythonhosted.org/packages/67/8e/eabdcfc51c2c54055134f7f34cffe2e426c95812dca18be0be6c4a902e4e/timespan-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-13 06:51:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jart",
"github_project": "timespan",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "timespan"
}