Data models, locale, and formatting¶
Immutable calendar models, the locale holder, digit-script helpers, and the text-field date formatter.
Calendar models¶
- class SimpleDate(year, month, day_of_month=1)[source]¶
Bases:
objectRepresents a simple date with year, month, and day of the month.
Ordered chronologically: comparisons and sorting follow (year, month, day_of_month), so
sorted(),min(),max(), and</>work as expected.
- class SimpleTime(hour, minute, second, nanosecond)[source]¶
Bases:
objectRepresents a 24-hour format time of day (hour, minute, second, nanosecond). Strictly adjusted to the Asia/Kathmandu time zone.
- class NepaliMonthCalendar(year, month, total_days_in_month, first_day_of_month, last_day_of_month)[source]¶
Bases:
objectRepresents a calendar month in the Nepali calendar system.
This data class provides information about a specific month in a Nepali calendar year, including the total number of days, the day of the week for the first and last days of the month, and the number of days from the start of the week to the first day of the month.
- Parameters:
- first_day_of_month¶
The day of the week (1-7, where 1 is Sunday) for the first day of the month.
- Type:
- last_day_of_month¶
The day of the week (1-7, where 1 is Sunday) for the last day of the month.
- Type:
- days_from_start_of_week_to_first_of_month¶
The number of days from the start of the week (Sunday) to the first day of the month.
- Type:
- class CustomCalendar(year, month, day_of_month, era, first_day_of_month, last_day_of_month, total_days_in_month, day_of_week_in_month=-1, day_of_week=-1, day_of_year=-1, week_of_month=-1, week_of_year=-1)[source]¶
Bases:
objectRepresents a date in a custom calendar system with detailed information.
This data class holds information about a specific date, including its year, month, day, era (AD or BS), and various other properties related to the day and week within the month and year.
- Parameters:
- day_of_week_in_month¶
The number of times the day of the week occurs in the month (e.g., 5 for the fifth Friday of the month). Defaults to -1 if not applicable.
- Type:
- day_of_week¶
The day of the week (1-7, e.g., 1 for Sunday). Defaults to -1 if not applicable.
- Type:
- property calendar_system: CalendarSystem¶
The CalendarSystem this calendar is expressed in, or BIKRAM_SAMBAT when era holds an unrecognised value.
- class CustomDateTime(custom_calendar, simple_time)[source]¶
Bases:
objectA data holder representing a CustomCalendar and SimpleTime.
Combines a CustomCalendar instance (representing the calendar) and a SimpleTime instance (representing the time).
- Parameters:
custom_calendar (CustomCalendar)
simple_time (SimpleTime)
- custom_calendar¶
The custom calendar represented by CustomCalendar.
- Type:
- simple_time¶
The corresponding time of day represented by SimpleTime.
- Type:
Calendar systems¶
The two calendar systems this library speaks.
Mirrors dev.shivathapaa.nepalidatepickerkmp.data.CalendarSystem.
- class CalendarSystem(value)[source]¶
Bases:
EnumA calendar system, identified by the
eraaCustomCalendarcarries.This is the named form of the
erainteger onCustomCalendarandMonthCalendar(1 = AD, 2 = BS), so the two representations never disagree:erais the single mapping.A calendar carries the system it was read in, so
NepaliDateConverter.get_english_calendar(...)answersGREGORIANandget_nepali_calendar(...)answersBIKRAM_SAMBAT.- BIKRAM_SAMBAT = 2¶
Bikram Sambat, the official calendar of Nepal.
era2.
- GREGORIAN = 1¶
Gregorian, referred to as the English or AD calendar throughout this library.
era1.
- classmethod from_era(era)[source]¶
The system carrying
era, orNonewhenerais neither 1 nor 2.Returns
Nonerather than raising becauseerareaches this from parsed and restored values that callers are expected to fall back on, not to crash over.- Parameters:
era (int)
- Return type:
CalendarSystem | None
A month’s grid geometry, tagged with the calendar system it is expressed in.
Mirrors dev.shivathapaa.nepalidatepickerkmp.data.MonthCalendar.
- class MonthCalendar(calendar_system, year, month, total_days_in_month, first_day_of_month, last_day_of_month)[source]¶
Bases:
objectOne month of either calendar, carrying the system it belongs to.
NepaliMonthCalendardescribes a Bikram Sambat month only. This says the same things about a month of either calendar, so a caller laying out a grid does not have to know which system produced it.- Parameters:
- calendar_system¶
The system
yearandmonthare expressed in.- Type:
- property days_from_start_of_week_to_first_of_month: int¶
Leading blank cells before day 1 when the grid starts on Sunday.
- index_in(years)[source]¶
The position of this month within
years, counting 12 months per year.yearsmust be a range in the samecalendar_system.
- to_nepali_month_calendar()[source]¶
Narrow this month to the Bikram Sambat-only
NepaliMonthCalendar.The year and month are copied verbatim, so calling this on a
CalendarSystem.GREGORIANmonth produces aNepaliMonthCalendarholding Gregorian numbers. Convert the month to Bikram Sambat first when that matters.- Return type:
Locale¶
- class NepaliDateLocale(language=NepaliCalendarUtilsLang.ENGLISH, date_format=NepaliDateFormatStyle.LONG, week_day_name=NameFormat.FULL, month_name=NameFormat.FULL, digit_script=None)[source]¶
Bases:
objectLocale settings for Nepali date display and formatting.
- Parameters:
language (NepaliCalendarUtilsLang)
date_format (NepaliDateFormatStyle)
week_day_name (NameFormat)
month_name (NameFormat)
digit_script (DigitScript | None)
- language¶
Language for date-related text. Defaults to English.
- date_format¶
Style of date formatting. Defaults to LONG.
- week_day_name¶
Format for weekday names. Defaults to FULL.
- month_name¶
Format for month names. Defaults to FULL.
- digit_script¶
Explicit numeral script for digits.
None(the default) means “follow the language”. Set this to render Nepali month names with Latin digits, or English month names with Devanagari digits.- Type:
- property resolved_digit_script: DigitScript¶
Concrete digit script to render numerals with.
Returns
digit_scriptwhen set explicitly, otherwise the language’s default (Devanagari for Nepali, Latin for English).
Digit scripts¶
Numeral-script utilities, decoupled from language.
Mirrors dev.shivathapaa.nepalidatepickerkmp.data.DigitScript from the Kotlin
core. A DigitScript holds the ten code points for digits 0-9 in a given
script, so any locale that shares the Devanagari digits (Nepali, Hindi, Marathi,
Maithili, Bhojpuri, Newari) can reuse the same rendering.
- class DigitScript(value)[source]¶
Bases:
EnumNumeral script used when rendering digits in localized dates / times.
Each member’s value is the ten code points for digits 0..9 in that script. Use
localize()to convert Latin-digit text to the chosen script, andto_latin_digits()to go the other way.- LATIN = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')¶
ASCII
0123456789. Default forNepaliCalendarUtilsLang.ENGLISH.
- DEVANAGARI = ('०', '१', '२', '३', '४', '५', '६', '७', '८', '९')¶
Devanagari
०१२३४५६७८९(U+0966..U+096F). Default forNEPALI.
- default_digit_script(lang)[source]¶
Default
DigitScriptfor a givenNepaliCalendarUtilsLang.Pass an explicit
digit_scripttoNepaliDateLocaleto override (e.g. show Nepali month names with Latin digits).- Return type:
- latin_digit_or_none(char)[source]¶
Reverse lookup for a single character.
If
charis a digit in any supported non-Latin script (Devanagari today), return the matching ASCII'0'..'9'. If it is already an ASCII digit, return it unchanged. Otherwise returnNone.
- to_latin_digits(text)[source]¶
Inverse of
DigitScript.localize().Convert digits in any supported non-Latin script back to ASCII 0-9. Non-digit characters pass through unchanged.
Date formatter¶
Parse and format SimpleDate for short numeric text-field input.
Mirrors dev.shivathapaa.nepalidatepickerkmp.data.NepaliDateFormatter.
Use this when you have a raw YYYY/MM/DD-style string and need a
SimpleDate (or vice versa). For locale-aware long-form output
(“Asar 21, 2082”), use NepaliDateConverter.format_nepali_date(...) instead.
Supported patterns are limited on purpose - a free-form formatter DSL is out of scope; the constrained surface keeps masking and validation predictable.
- class DatePattern(value)[source]¶
Bases:
EnumSupported text-field input/output patterns.
Each member’s value is
(literal, delimiter, year_first).- YYYY_SLASH_MM_SLASH_DD = ('YYYY/MM/DD', '/', True)¶
YYYY/MM/DD- e.g.2082/02/14.
- YYYY_DASH_MM_DASH_DD = ('YYYY-MM-DD', '-', True)¶
YYYY-MM-DD- ISO-like, e.g.2082-02-14.
- DD_SLASH_MM_SLASH_YYYY = ('DD/MM/YYYY', '/', False)¶
DD/MM/YYYY- day-first, e.g.14/02/2082.
- DD_DASH_MM_DASH_YYYY = ('DD-MM-YYYY', '-', False)¶
DD-MM-YYYY- day-first dashed, e.g.14-02-2082.
- class NepaliDateFormatter[source]¶
Bases:
objectFormatter/parser primitive for short numeric date strings.
- Pattern[source]¶
Alias so callers can write
NepaliDateFormatter.Pattern.YYYY_SLASH_MM_SLASH_DD.alias of
DatePattern
- static format(date, pattern, script=DigitScript.LATIN)[source]¶
Format
dateas apattern.literal-shaped string with digits inscript.No range or selectable-date checks - pass any
SimpleDate; the result will reflect it.- Parameters:
date (SimpleDate)
pattern (DatePattern)
script (DigitScript)
- Return type:
- static parse(text, pattern)[source]¶
Parse
textaspattern. Accepts Latin and Devanagari digits.Returns
Nonewhen the length is wrong, a token is not numeric, a delimiter does not match, month is not in 1..12, or day is not in 1..32 (32 is allowed because some Bikram Sambat months have 32 days; tighter validation against the actual month length is the caller’s job).- Parameters:
text (str)
pattern (DatePattern)
- Return type:
SimpleDate | None
Time formatter¶
Parse and format SimpleTime as the library’s canonical time-of-day wire string.
Mirrors dev.shivathapaa.nepalidatepickerkmp.data.NepaliTimeFormatter.
HH:mm:ss with an optional nine-digit fractional part. This is the shape a SimpleTime
takes on the wire everywhere the sibling Kotlin library is published, so a payload written
here reads on a Kotlin, Android, Swift or JavaScript client without any of them depending on
kotlinx-serialization.
Times are always read as Asia/Kathmandu, the zone every SimpleTime in this library is
anchored to. No zone offset is written or accepted.
For display output (12-hour clocks, Devanagari digits, locale-aware wording) use
NepaliDateConverter.get_formatted_time_in_english, get_formatted_time_in_nepali, or
NepaliDateConverter.format_time_by_unicode_pattern. This formatter is for persistence and
transport, and is fixed to Latin digits and 24-hour form on purpose.
- class NepaliTimeFormatter[source]¶
Bases:
objectFormatter and parser for the canonical time-of-day wire string.
- static format(time)[source]¶
Format
timeasHH:mm:ss, appending.nnnnnnnnnonly whenSimpleTime.nanosecondis non-zero, so whole-second payloads stay compact.Examples:
"09:30:00","23:59:59.123456789","00:00:00.000000007".No range check, pass any
SimpleTimeand the result reflects it. A value outside the rangesparse()accepts will not round-trip.- Parameters:
time (SimpleTime)
- Return type:
- static parse(text)[source]¶
Parse
textasHH:mm:ssorHH:mm:ss.nnnnnnnnn, returningNonewhen it is not a time this formatter would have produced.Returns
Nonewhen:the clock part does not hold exactly three
:-separated fields,any field or the fractional part is not decimal digits,
hour is outside
0..23, or minute or second is outside0..59,the fractional part is outside
0..999999999.
The fractional part is read as a plain count of nanoseconds, so
".7"means seven nanoseconds, not seven tenths of a second. Round-tripping throughformat()returns the zero-padded nine-digit form.Field widths are not enforced, so
"9:30:00"parses as09:30:00. Any Unicode decimal digit is accepted, so Devanagari input such as"०९:३०:००"reads the same as its Latin form, and the two may be mixed.format()always writes Latin digits. Leading and trailing whitespace is not trimmed, callstr.strip()first when the input may carry any.- Parameters:
text (str)
- Return type:
SimpleTime | None