{"record":{"id":"082f8b795aac90d0","repo":"TheAlgorithms/Python","slug":"invalid-unit-invalid-unit-is-not-in-join-t","errorCode":null,"errorMessage":"Invalid unit {invalid_unit} is not in {', '.join(time_chart)}.","messagePattern":"Invalid unit (.+?) is not in (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/time_conversions.py","lineNumber":71,"sourceCode":"    >>> convert_time(1, \"cool\", \"century\")  # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid unit cool is not in seconds, minutes, hours, days, weeks, ...\n    >>> convert_time(1, \"seconds\", \"hot\")  # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid unit hot is not in seconds, minutes, hours, days, weeks, ...\n    \"\"\"\n    if not isinstance(time_value, (int, float)) or time_value < 0:\n        msg = \"'time_value' must be a non-negative number.\"\n        raise ValueError(msg)\n\n    unit_from = unit_from.lower()\n    unit_to = unit_to.lower()\n    if unit_from not in time_chart or unit_to not in time_chart:\n        invalid_unit = unit_from if unit_from not in time_chart else unit_to\n        msg = f\"Invalid unit {invalid_unit} is not in {', '.join(time_chart)}.\"\n        raise ValueError(msg)\n\n    return round(\n        time_value * time_chart[unit_from] * time_chart_inverse[unit_to],\n        3,\n    )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n    print(f\"{convert_time(3600,'seconds', 'hours') = :,}\")\n    print(f\"{convert_time(360, 'days', 'months') = :,}\")\n    print(f\"{convert_time(360, 'months', 'years') = :,}\")\n    print(f\"{convert_time(1, 'years', 'seconds') = :,}\")\n","sourceCodeStart":53,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/time_conversions.py#L53-L87","documentation":"Raised by convert_time() in conversions/time_conversions.py when unit_from or unit_to (after .lower()) is not a key of time_chart: seconds, minutes, hours, days, weeks, months, years, decades, centuries. The invalid one is identified in the message; from-unit is reported if both are bad. Matching is case-insensitive because inputs are lowercased first.","triggerScenarios":"Calling convert_time(1, 'cool', 'century') — note the singular/plural mismatch: 'century' fails, 'centuries' passes; also 'sec', 'min', 'hr', 'ms' abbreviations all fail.","commonSituations":"Using abbreviated unit names from other libraries (pandas 'T'/'min', dateutil 'H'); singular/plural drift between UI labels and this API; non-English or symbol units ('s', 'h').","solutions":["Use the exact plural keys: seconds, minutes, hours, days, weeks, months, years, decades, centuries.","Build an abbreviation map: {'sec': 'seconds', 'min': 'minutes', 'hr': 'hours', 's': 'seconds', 'h': 'hours'}.","Derive dropdown options from the library's chart (time_chart keys) instead of hand-writing them."],"exampleFix":"# before\nconvert_time(1, 'sec', 'min')  # ValueError: Invalid unit sec is not in seconds, minutes, ...\n\n# after\nALIASES = {'sec': 'seconds', 'min': 'minutes', 'hr': 'hours', 'day': 'days', 'yr': 'years'}\nconvert_time(1, ALIASES['sec'], ALIASES['min'])","handlingStrategy":"validation","validationCode":"from conversions.time_conversions import time_chart\n\nunit_from = ALIASES.get(unit_from.lower(), unit_from.lower())\nunit_to = ALIASES.get(unit_to.lower(), unit_to.lower())\nif unit_from not in time_chart or unit_to not in time_chart:\n    raise ValueError(f'unsupported time unit(s) {unit_from!r}, {unit_to!r}')\nconvert_time(value, unit_from, unit_to)","typeGuard":"from conversions.time_conversions import time_chart\n\ndef is_time_unit(u: object) -> bool:\n    return isinstance(u, str) and u.lower() in time_chart","tryCatchPattern":"try:\n    convert_time(v, f, t)\nexcept ValueError as e:\n    if 'Invalid unit' in str(e):\n        raise UserInputError(f'unknown time unit in {f!r}->{t!r}') from e\n    raise","preventionTips":["Units are plural and lowercase: seconds, minutes, hours, days, weeks, months, years, decades, centuries","Alias abbreviations (s, sec, min, hr) at your boundary","Remember 'century' is invalid; use 'centuries'"],"tags":["python","validation","units","time","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}