Skip to content

Commit

Permalink
feat: add fiscal year calculation methods and update README with usag…
Browse files Browse the repository at this point in the history
…e examples
  • Loading branch information
4mritGiri committed Jan 4, 2025
1 parent 91e8331 commit 9c60b9b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,77 @@ Sun Mon Tue Wed Thu Fri Sat

---

## **Fiscal Year Calculations**

The **npdatetime** library provides methods to easily calculate the current fiscal year and convert dates to their corresponding fiscal year. Fiscal years in Nepal start from **Shrawan (month 4)** and end at **Ashadh (month 12)**.

### **Getting the Current Fiscal Year**
You can get the current fiscal year using the `current_fiscal_year()` method. It returns the fiscal year as a tuple in the format `(start_year, end_year)`.

```python
import npdatetime

# Get current fiscal year
npdatetime.datetime.current_fiscal_year()
# Output: (2081, 2082)
```

Alternatively, you can use it directly from the `datetime` class:

```python
from npdatetime import datetime

# Get current fiscal year
datetime.current_fiscal_year()
# Output: (2081, 2082)
```

### **Getting Fiscal Year for a Specific Date**
You can also calculate the fiscal year for any specific Nepali date using the `get_fiscal_year_by_date()` function. This takes a `datetime` object as input and returns the fiscal year for that date.

```python
from npdatetime import get_fiscal_year_by_date
from npdatetime import datetime

# Example date
date_obj = datetime(2079, 12, 1)

# Get fiscal year for a specific date
get_fiscal_year_by_date(date_obj)
# Output: (2079, 2080)

# You can also directly pass a Nepali datetime object to the function
get_fiscal_year_by_date(datetime(2080, 4, 1))
# Output: (2080, 2081)
```

### **Examples**

Here are a few examples of how you can use the `get_fiscal_year_by_date()` method to calculate the fiscal year for various dates:

```python
# For 2079-12-01
get_fiscal_year_by_date(datetime(2079, 12, 1))
# Output: (2079, 2080)

# For 2080-03-01
get_fiscal_year_by_date(datetime(2080, 3, 1))
# Output: (2079, 2080)

# For 2080-04-01
get_fiscal_year_by_date(datetime(2080, 4, 1))
# Output: (2080, 2081)

# For 2080-06-01
get_fiscal_year_by_date(datetime(2080, 6, 1))
# Output: (2080, 2081)
```

### **Note**
- The fiscal year is based on the Nepali date system, which is different from the Gregorian calendar. In Nepal, the fiscal year runs from **Shrawan (month 4)** to **Ashadh (month 12)**.

---

## **Documentation**

Comprehensive usage examples and detailed documentation can be found on the [official website](https://4mritGiri.github.io/npdatetime/).
Expand Down
10 changes: 9 additions & 1 deletion npdatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import math as _math
import datetime as _actual_datetime

from npdatetime.utils import get_fiscal_year_by_date

from .config import CALENDAR_PATH, MINDATE, MAXDATE, REFERENCE_DATE_AD

MINYEAR = MINDATE['year']
Expand Down Expand Up @@ -376,7 +378,13 @@ def from_datetime_date(cls, from_date):
if not isinstance(from_date, _actual_datetime.date):
raise TypeError("Unsupported type {}.".format(type(from_date)))
return cls(MINYEAR, 1, 1) + (from_date - _actual_datetime.date(**REFERENCE_DATE_AD))


@classmethod
def current_fiscal_year(cls):
"""Return the current fiscal year"""
current_date = cls.today()
return get_fiscal_year_by_date(current_date)

def to_datetime_date(self):
"""Convert npdatetime.date to datetime.date (B.S date to A.D).
Expand Down
9 changes: 9 additions & 0 deletions npdatetime/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

def get_fiscal_year_by_date(date_obj):
"""Return fiscal year by the given Nepali datetime object"""
# Fiscal year starts in Shrawan (month 4)
if date_obj.month < 4: # Months 1, 2, 3 are part of the previous fiscal year
fiscal_year = (date_obj.year - 1, date_obj.year)
else:
fiscal_year = (date_obj.year, date_obj.year + 1)
return fiscal_year

0 comments on commit 9c60b9b

Please sign in to comment.