time
Defines the BSTime class for representing time-of-day.
This module provides a timezone-aware BSTime object, which is a subclass of the standard datetime.time. It supports all the functionality of the parent class but adds BS-specific formatting capabilities, such as rendering time components in Nepali numerals.
Examples
from bikram_sambat import time, tz
# Create a time
t = time(10, 30, 45)
# Create a timezone-aware time
t_aware = time(10, 30, 45, tzinfo=tz.nepal)
# Formatting
print(t.strftime("%H:%M:%S"))
# >> 10:30:45
print(t_aware.strftime("%I:%M:%S %p %Z"))
# >> 10:30:45 AM Asia/Kathmandu
print(t_aware.strftime("%i:%l:%s %P"))
# >> १०:३०:४५ पूर्वाह्न
- class bikram_sambat.bs_time.BSTime(hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0, tzinfo: tzinfo | None = None, *, fold: int = 0)[source]
Bases:
timeRepresents a time of day, independent of any particular day.
BSTime is a subclass of the standard datetime.time class and is fully compatible with it. It extends the base class with enhanced formatting options via the strftime method, which supports Nepali numerals (e.g., %h) and Nepali AM/PM designators (e.g., %P).
The constructor accepts all the same arguments as datetime.time, including tzinfo for creating timezone-aware time objects.
- Parameters:
hour (int) – The hour (0-23). Defaults to 0.
minute (int) – The minute (0-59). Defaults to 0.
second (int) – The second (0-59). Defaults to 0.
microsecond (int) – The microsecond (0-999999). Defaults to 0.
tzinfo (_dt.tzinfo, optional) – The timezone object. Defaults to None.
fold (int) – Used to disambiguate wall times during a repeated hour (e.g., during a DST transition). 0 or 1. Defaults to 0.
- strftime(format: str) str[source]
Formats the time according to a format string with BS-specific directives.
This method supports all standard strftime directives for time, plus custom directives for Nepali numerals and AM/PM indicators.
Example
>>> t = BSTime(15, 30, tzinfo=pytz.timezone("Asia/Kathmandu")) >>> t.strftime("%H:%M %P") '15:30 अपराह्न' >>> t.strftime("%I:%M %p in Nepali is %i:%l") '03:30 PM in Nepali is ०३:३०'
- Parameters:
format (str) – The strftime-style format string.
- Returns:
The formatted time string.
- Return type:
str
- Raises:
InvalidTypeError – If the format is not a string.
ValueError – If the format string contains an unsupported directive.
- classmethod fromstrftime(time_string: str, format: str) BSTime[source]
Parses a string into a BSTime object according to a format.
This class method provides a flexible way to create BSTime instances from strings, including those with Nepali numerals or names.
Example
>>> BSTime.fromstrftime("15:30 अपराह्न", "%H:%M %P") bikram_sambat.time.BSTime(15, 30)
- Parameters:
time_string (str) – The string to parse.
format (str) – The strftime format that the time_string follows.
- Returns:
A new BSTime instance.
- Return type:
- utcoffset(dt=None) timedelta | None[source]
Returns the UTC offset if the time is timezone-aware.
- Returns:
The UTC offset as a timedelta object, or None if the instance is naive.
- Return type:
Optional[_dt.timedelta]
- tzname(dt=None) str | None[source]
Returns the timezone name if the time is timezone-aware.
- Returns:
The timezone name as a string, or None if the instance is naive.
- Return type:
Optional[str]
- dst()
Return self.tzinfo.dst(self).
- fold
- classmethod fromisoformat()
string -> time from a string in ISO 8601 format
- hour
- isoformat()
Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].
The optional argument timespec specifies the number of additional terms of the time to include. Valid options are ‘auto’, ‘hours’, ‘minutes’, ‘seconds’, ‘milliseconds’ and ‘microseconds’.
- max = bikram_sambat.bs_time.BSTime(23, 59, 59, 999999)
- microsecond
- min = bikram_sambat.bs_time.BSTime(0, 0)
- minute
- replace()
Return time with new specified fields.
- resolution = datetime.timedelta(microseconds=1)
- second
- tzinfo