Skip to content

py-nepali is a python package containing features that will be useful for Nepali projects.

License

Notifications You must be signed in to change notification settings

opensource-nepal/py-nepali

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nepali

nepalidatetime compatible with python's datetime feature. Converting nepali date to english, parsing nepali datetime, nepali timezone, and timedelta support in nepali datetime

Example

import datetime
from nepali.datetime import nepalidate, parser

# parse
nepali_datetime = parser.parse('2079-02-15')
print(nepali_datetime) # 2079-02-15 00:00:00

# convert
date = datetime.date(2017, 3, 15)
nepali_date = nepalidate.from_date(date)
print(nepali_date) # 2073-12-02

Requirements

Python >= 3

Installation

pip install nepali

Features

  1. Nepali datetime support
  2. Parse nepali datetime
  3. Nepali Characters (Months, Days, etc)
  4. Number to nepali numbers and nepali numbers to english.

nepalidate

Represents nepali date, converts English date to nepali date and nepali date to english date

from nepali.datetime import nepalidate  

Creating new object

# object with current date
np_date = nepalidate(year, month, day)

# object with today's date
np_date = nepalidate.today()

# parse date
np_date = nepalidate.strptime('2078-01-12', format='%Y-%m-%d')

Object from python's datetime.date

import datetime

date = datetime.date.today()
np_date = nepalidate.from_date(date)

Get python's datetime.date object

np_date.to_date()

Get python's datetime.datetime object

np_date.to_datetime()

nepalidatetime

Represents nepali date time

from nepali.datetime import nepalidatetime  

Creating new object

# object with specific datetime
np_datetime = nepalidatetime(year, month, day[, hour[, minute[, second]]]) # arguments must be nepali

# object with current datetime
np_datetime = nepalidatetime.now()

# parse datetime
np_datetime = nepalidatetime.strptime('2078-01-12 13:12', format='%Y-%m-%d %H:%M')

Object from python's datetime.datetime

import datetime

dt = datetime.datetime.now()
np_datetime = nepalidatetime.from_datetime(dt)

Get nepalidate object

np_datetime.date()

Get python's datetime.time object

np_datetime.time()

Get python's datetime.datetime object

np_datetime.to_datetime()

Date String Format
Equivalent to python's datetime strftime format

npDateTime = nepalidatetime.now()
print(npDateTime.strftime('%a %A %w %d %b %B %m %y %Y %H %I %p %M %S'))
print(npDateTime.strftime_en('%a %A %w %d %b %B %m %y %Y %H %I %p %M %S'))
OUTPUT: 
बिही बिहीबार ४ २३ असार असार ०३ ७९ २०७९ १२ १२ मध्यान्ह ४० १९
Thu Thursday 4 23 Ashad Ashad 03 79 2079 12 12 AM 40 19

timedelta operations

ndt = nepalidatetime.now()
print(ndt + datetime.timedelta(hours=5))
print(ndt - datetime.timedelta(hours=5))

parse

Parses datetime from a string.

Note: parse uses very high cost method, so avoid this as much as you can. Use strptime

from nepali.datetime import parser as nepalidatetime_parser
ndt = nepalidatetime_parser.parse('29 Jestha, 2078, 1:30 PM')

nepalihumanize

nepalihumanize converts nepalidatetime to nepali human readable form

from nepali.datetime import nepalihumanize  

Creating new object

# object from nepali datetime
ndt = nepalidatetime.now()
humanize_str = nepalihumanize(ndt)

# object from python datetime
dt = datetime.datetime.now()
humanize_str = nepalihumanize(dt)

Humanize with threshold
returns date in nepali characters if more than threshold(in seconds) else returns humanize form

humanize_str = nepalihumanize(ndt, threshold=60) # 60 seconds

# custom format after threshold
humanize_str = nepalihumanize(ndt, threshold=60, format='%Y-%m-%d') # 60 seconds

For Django Template

Add 'nepali' to your INSTALLED_APPS setting.

INSTALLED_APPS = [
	...
	'nepali',
	...
]

IN your Template

{% load nepalidatetime %}
{% nepalinow %}
{% nepalinow '%Y-%m-%d' %}
{{ datetimeobj|nepalidate:"%Y-%m-%d" }}
{{ datetimeobj|nepalidate_en:"%Y-%m-%d" }}
{{ datetimeobj|nepalihumanize }}