Skip to main content

A comprehensive, modern, and easy-to-use Python library for the Bikram Sambat (BS) calendar system.

Project description

Bikram Sambat (बि.सं.) for Python

PyPI Version Python Versions CI/CD Status Coverage Status License

Bikram Sambat (BS), also known as Vikram Samvat, is a historical Hindu calendar system used in Nepal and several states in India. It is approximately 56.7 years ahead of the Gregorian (AD) calendar.

This Python library provides a comprehensive and easy-to-use interface for working with Bikram Sambat dates and times. It offers date, time, and datetime objects that are analogous to Python's built-in datetime module, but specifically tailored for the BS calendar system. Key features include accurate AD-BS conversions, timezone awareness, BS-specific formatting (including Nepali numerals and month/weekday names), and robust parsing capabilities.

🛠️ Installation

Install the bikram-sambat package using pip:

pip install bikram-sambat

Requirements:

  • Python 3.8 or higher
  • The pytz library (automatically installed as a dependency)

✨ Features

  • BS-Specific Date, Time, and DateTime Objects:
    • bikram_sambat.date: Represents a Bikram Sambat date (year, month, day).
    • bikram_sambat.time: Represents a Bikram Sambat time (hour, minute, second, microsecond), with timezone support.
    • bikram_sambat.datetime: Represents a Bikram Sambat date and time, with timezone support.
  • Accurate AD ⇔ BS Conversions:
    • Reliably convert between Gregorian (AD) and Bikram Sambat (BS) dates and datetimes.
    • Handles the complex mapping of days between the two calendar systems.
  • Comprehensive Formatting (strftime):
    • Format dates and times into custom string representations.
    • Supports standard strftime directives.
    • Includes BS-specific directives for:
      • Nepali numerals (e.g., २०८१ for year, १५ for day).
      • Full and abbreviated month names in English (e.g., "Baishakh", "Bai").
      • Full and abbreviated month names in Nepali (e.g., "वैशाख").
      • Full and abbreviated weekday names in English and Nepali.
  • Robust Parsing (fromstrftime):
    • Parse BS date/time strings from various formats into corresponding objects.
    • Handles Nepali numerals and month/weekday names in input strings.
  • Timezone Awareness:
    • Full support for timezone-aware time and datetime objects using pytz.
    • Easy access to common timezones like Nepal Standard Time (Asia/Kathmandu) and UTC.
    • Convert BS datetimes between different timezones.
  • Arithmetic Operations:
    • Perform addition and subtraction with timedelta objects.
    • Calculate differences between BS dates/datetimes.
  • Standard Python Interface:
    • Familiar API, largely compatible with Python's built-in datetime module, making it easy to learn and use.
    • Inherits from datetime.date, datetime.time, and datetime.datetime where appropriate.
  • Nepali Calendar Data:
    • Includes data for BS calendar month lengths for a wide range of years (typically 1901 BS to 2199 BS).
  • Bikram Sambat Calendar Data:
    • Includes pre-compiled data for Bikram Sambat month lengths for a wide range of years (currently 1901 BS to 2199 BS).
    • Disclaimer: The calendar data (days in each BS month) is based on aggregated information from various public sources and existing calendar implementations. See the "Calendar Data and Accuracy" section for more details.

📅 Calendar Data and Accuracy

The Bikram Sambat calendar is a lunisolar calendar, and the exact number of days in each month can vary from year to year. This library relies on pre-compiled data for the number of days in each Bikram Sambat month across a range of years (currently 1901 BS to 2199 BS).

This data has been aggregated and cross-referenced from several publicly available sources and existing calendar implementations, including:

  1. NepDate by TheCrossLegCoder (C#)
  2. nepali-datetime by amitgaru2 (Python)
  3. Traditional Nepali calendar software.

While efforts have been made to ensure accuracy, discrepancies can exist between different calendar sources for certain years or months due to the traditional methods of BS calendar determination. The conversions and date operations within this library are accurate based on the embedded calendar data. For official or legal purposes, please consult an officially published Nepali calendar (Patro).

🚀 Quick Start

Here's a quick glimpse of how to use bikram-sambat:

from bikram_sambat import date, time, datetime, timedelta
from bikram_sambat.timezone import nepal, utc

# --- Bikram Sambat Dates ---
# Create a BS date
bs_date = date(2080, 5, 15)  # BS: 2080 Bhadra 15
print(f"BS Date: {bs_date}")
# >> BS Date: 2080-05-15

print(f"Year: {bs_date.year}, Month: {bs_date.month}, Day: {bs_date.day}")
# >> Year: 2080, Month: 5, Day: 15

# Today's BS date
today_bs = date.today()
print(f"Today (BS): {today_bs}")
# >> Today (BS): 2082-04-06

# Convert BS to AD
ad_date = bs_date.togregorian()
print(f"Equivalent AD Date: {ad_date}")
# >> Equivalent AD Date: 2023-09-01

# Convert AD to BS
from datetime import date as ad_py_date
ad_another_date = ad_py_date(2024, 1, 1)
bs_converted_date = date.fromgregorian(ad_another_date)
print(f"AD {ad_another_date} is BS {bs_converted_date}")
# >> AD 2024-01-01 is BS 2080-09-16

# Formatting
print(bs_date.strftime("%Y %B %d, %A (%K %N %D, %G)"))
# >> 2080 Bhadra 15, Friday (२०८० भदौ १५, शुक्रबार)

# --- Bikram Sambat Times ---
# Create a BS time (naive)
bs_time = time(14, 30, 45)
print(f"BS Time: {bs_time}")
# >> BS Time: 14:30:45


# Create a timezone-aware BS time
bs_time_nepal = time(10, 15, 0, tzinfo=nepal)
print(f"BS Time (Nepal): {bs_time_nepal} {bs_time_nepal}")
# >> BS Time (Nepal): 10:15:00 10:15:00


# Formatting time
print(bs_time_nepal.strftime("%I:%M:%S %p %P [%Z]"))
# >> 10:15:00 AM पहिले [Asia/Kathmandu]


# --- Bikram Sambat Datetimes ---
# Create a naive BS datetime
bs_dt_naive = datetime(2081, 1, 1, 10, 0, 0) # BS: 2081 Baishakh 1, 10:00 AM
print(f"BS Datetime (naive): {bs_dt_naive}")
# >> BS Datetime (naive): 2081-01-01T10:00:00


# Create a timezone-aware BS datetime
bs_dt_aware = datetime(2081, 1, 1, 10, 0, 0, tzinfo=nepal)
print(f"BS Datetime (aware): {bs_dt_aware}")
# >> BS Datetime (aware): 2081-01-01T10:00:00+0545


# Current BS datetime (naive)
now_bs_naive = datetime.now()
print(f"Now (BS, naive): {now_bs_naive}")
# >> Now (BS, naive): 2082-04-06T03:41:28.962229


# Current BS datetime in a specific timezone
now_bs_nepal = datetime.now(nepal)
print(f"Now (BS, Nepal): {now_bs_nepal}")
# >> Now (BS, Nepal): 2082-04-06T09:26:28.962327+0545


# Convert to another timezone
now_bs_utc = now_bs_nepal.astimezone(utc)
print(f"Now (BS, UTC): {now_bs_utc}")
# >> Now (BS, UTC): 2082-04-06T03:41:28.962327+0000


# Convert BS datetime to AD datetime
ad_dt = bs_dt_aware.togregorian()
print(f"Equivalent AD Datetime: {ad_dt}")
# >> Equivalent AD Datetime: 2024-04-13 10:00:00+05:45

# --- Arithmetic ---
delta = timedelta(days=10, hours=5)
future_dt = now_bs_nepal + delta
print(f"10 days, 5 hours from now (BS): {future_dt}")
# >> 10 days, 5 hours from now (BS): 2082-04-16T14:26:28.962327+0545


diff = future_dt - now_bs_nepal
print(f"Difference: {diff}")
# >> Difference: 10 days, 5:00:00

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.

🤝 Contributing

Contributions are welcome!.

📚 Full Documentation

Read the full documentattion here: https://bikram-sambat.readthedocs.io/en/latest/

🙌 Support

If you find this project useful, consider starring ⭐ the repo or sharing it. For feature requests or issues, please open a ticket at the Issue Tracker

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bikram_sambat-0.1.1.tar.gz (79.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bikram_sambat-0.1.1-py3-none-any.whl (79.3 kB view details)

Uploaded Python 3

File details

Details for the file bikram_sambat-0.1.1.tar.gz.

File metadata

  • Download URL: bikram_sambat-0.1.1.tar.gz
  • Upload date:
  • Size: 79.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for bikram_sambat-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3b7dbd2401553fbe1dc17d7c62dece283d333054c08f7b4bb562a6ab89e9a4f3
MD5 845f719fa7279af89b2efa50790569a3
BLAKE2b-256 cb85bc1d6a0618926e23a705e8985fb230bcbf3eb651d7563ddbe7c7fcf03216

See more details on using hashes here.

File details

Details for the file bikram_sambat-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bikram_sambat-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 79.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for bikram_sambat-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd7b11d7df594e1e82d8b5beb9e14caedd370ceed261c9d16d166eebfbfa2787
MD5 63512b6e2f0896ecca314d13503dd292
BLAKE2b-256 1bf1ed43665071509eb0f7018afb0ab05d8bf17893ea335fe0f835f80c6a611e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page