local-crontab
local-crontab is a Python wheel and command line utility designed to convert a crontab, defined in a local timezone, into a set of UTC crontabs. This conversion often results in multiple UTC crontabs due to the complexities of Daylight Saving Time (DST). The project is based on an earlier `local-crontab` by UnitedIncome, incorporating bug fixes and extending functionality to convert specific hour and day parts. As of version 0.3.0, released in July 2021, the library appears to be in a maintenance status with infrequent updates.
Common errors
-
ModuleNotFoundError: No module named 'local_crontab'
cause The `local-crontab` package was installed, but the import statement uses an incorrect module name or the package is not in Python's `sys.path`.fixEnsure the package is installed via `pip install local-crontab`. The correct import statement is `from local_crontab import Converter` (note the underscore in `local_crontab`). -
TypeError: Converter() takes 2 positional arguments but 3 were given
cause The `Converter` class constructor was called with an incorrect number of arguments.fixThe `Converter` class expects exactly two arguments: the crontab string and the local timezone string, e.g., `Converter('0 10 * * *', 'America/New_York')`. -
pytz.exceptions.UnknownTimeZoneError: 'Invalid/Unknown' is not a valid timezone
cause An invalid or unrecognized timezone string was provided to the `Converter` class.fixEnsure the timezone string passed to `Converter` is a valid `pytz` timezone name (e.g., 'America/New_York', 'Europe/London'). Consult the `pytz` documentation for a list of supported timezones.
Warnings
- gotcha The library typically generates multiple UTC crontab entries for a single local crontab. This is by design, accounting for Daylight Saving Time (DST) changes throughout the year. Users expecting a single, fixed UTC equivalent might find this behavior unexpected.
- gotcha While the library allows converting crontab 'hour and day parts' to a single UTC equivalent, maintaining year-round accuracy through DST changes requires an *external* 'dst check' program. This external tool is not part of the `local-crontab` library.
- gotcha This `local-crontab` package is a fork and bug-fixed version of an older project by 'UnitedIncome'. While improvements are included, users familiar with or migrating from the original might encounter subtle behavioral differences or API changes not explicitly documented as breaking changes.
Install
-
pip install local-crontab
Imports
- Converter
from local_crontab import Converter
Quickstart
from local_crontab import Converter
# Example 1: Convert a crontab from 'America/New_York' to UTC
# This will typically produce two UTC crontabs due to DST changes.
converter_ny = Converter('0 10 * * *', 'America/New_York')
utc_crontabs_ny = converter_ny.convert()
print(f"Original (NY): 0 10 * * *")
print(f"UTC conversions for 'America/New_York': {utc_crontabs_ny}")
# Expected output similar to: ['0 15 * * *', '0 14 * * *'] (order may vary)
# Example 2: Convert a crontab for 'Europe/Berlin'
# Also likely to produce two UTC crontabs due to DST.
converter_berlin = Converter('30 8 * * *', 'Europe/Berlin')
utc_crontabs_berlin = converter_berlin.convert()
print(f"\nOriginal (Berlin): 30 8 * * *")
print(f"UTC conversions for 'Europe/Berlin': {utc_crontabs_berlin}")
# Expected output similar to: ['30 7 * * *', '30 6 * * *'] (order may vary)