Files
cowork-local/core/holiday_calendar.py
T
f9f6bc01fd
CI / test (push) Canceled after 0s
Feature/delta team/epic r04 (#7)
## Summary

epic r04 - begin refactor

## Change Type

- [x] Cowork feature
- [ ] Bug fix
- [ ] Core AI contribution
- [ ] Test / hardening
- [ ] Performance
- [ ] Documentation

## Related Work

Cowork Task:

Core Repo: http://34.143.229.138/gitea-admin/fsg-ai-core-assets

Core AI Issue:

Core Task:

Related PR:

## Scope

What is intentionally included?

What is intentionally NOT included?

## Validation

- [ ] Unit tests
- [ ] Integration tests
- [ ] Manual verification
- [ ] Regression check

Commands / evidence:

## Security Impact

Permission / credential / network / customer data impact:

## Compatibility

- [ ] No breaking change
- [ ] Breaking change documented

## Reviewer Notes

Anything Cowork reviewers should pay attention to.

---------

Co-authored-by: Anh Tran Nguyen Minh <anhtnm1@fpt.com>
Co-authored-by: Huong Le Thi Thien <huongltt35@fpt.com>
Co-authored-by: Nam Pham Dinh Thanh <nampdt@fpt.com>
Co-authored-by: Vu Dam Tuan <vudt15@fpt.com>
Co-authored-by: Hiep Ha Van <hiephv3@fpt.com>
Co-authored-by: Lam Hoang Van <lamhv7@fpt.com>
Reviewed-on: #7
Co-authored-by: Duy Le Huu <duylh19@fpt.com>
2026-08-31 05:15:13 +00:00

59 lines
2.2 KiB
Python

"""Public-holiday lookup by country for Schedule Task's "skip holidays".
Prefers the ``holidays`` PyPI package (200+ countries, correct lunar-calendar
dates for Tết etc.). If it isn't installed or the country code is unknown,
falls back to a small built-in fixed-date table (VN/JP national days only) so
the feature degrades gracefully instead of crashing offline installs.
"""
from __future__ import annotations
from datetime import date
from typing import Dict, Optional, Set, Tuple
# Country codes offered in the task editor (any ISO code typed in still works
# when the `holidays` package is installed).
COMMON_COUNTRIES = ("VN", "JP", "US", "KR", "CN", "SG", "DE", "FR", "GB", "IN")
# Fixed-date fallback (month, day) — used only when the holidays package is
# unavailable. Lunar holidays (Tết, Hùng Kings…) can't be fixed dates, so the
# fallback intentionally covers solar-calendar national days only.
_FALLBACK: Dict[str, Set[Tuple[int, int]]] = {
"VN": {(1, 1), (4, 30), (5, 1), (9, 2)},
"JP": {(1, 1), (2, 11), (2, 23), (4, 29), (5, 3), (5, 4), (5, 5),
(8, 11), (11, 3), (11, 23)},
}
_cache: Dict[Tuple[str, int], object] = {}
def _package_calendar(country: str, year: int):
"""A holidays-package calendar for (country, year), cached; None when the
package is missing or the country code is unknown to it."""
key = (country, year)
if key in _cache:
return _cache[key]
cal = None
try:
import holidays as _holidays
cal = _holidays.country_holidays(country, years=[year, year + 1])
except Exception: # noqa: BLE001 — package missing / unknown country code
cal = None
_cache[key] = cal
return cal
def is_holiday(d: date, country: Optional[str]) -> bool:
"""Ngày này có phải ngày nghỉ của một quốc gia không.
Không đặt quốc gia thì luôn trả ``False`` — không suy đoán lịch nghỉ thay
người dùng.
"""
country = (country or "").strip().upper()
if not country:
return False
cal = _package_calendar(country, d.year)
if cal is not None:
return d in cal
return (d.month, d.day) in _FALLBACK.get(country, set())