Package: nepali
nepalinepaliis a python package containing features that will be useful for Nepali projects.The major feature of this package is nepalidatetime, which is compatible with python's datetime feature. It helps nepali date to english, parsing nepali datetime, nepali timezone, and timedelta support in nepali datetime.Exampleimportdatetimefromnepaliimportphone_numberfromnepali.datetimeimportnepalidate,parsernepali_datetime=parser.parse('2079-02-15')# 2079-02-15 00:00:00date=datetime.date(2017,3,15)nepali_date=nepalidate.from_date(date)# 2073-12-02phone_number.parse("+977-9845217789")# {#     'type':      'Mobile',#     'number':    '9845217789',#     'operator':  <Operator: Nepal Telecom># }RequirementsPython >= 3Installationpip install nepaliFeaturesDate and Timedate_converternepalidatenepalidatetimenepalihumanizetimezoneparsestrftime() and strptime() Format CodesNumbersnepalinumberPhone NumberLocationsFor Django TemplateDate and Timedate_converterDate converter module converts english date to nepali and nepali date to english. It doesn't contain any extra functionality.Convert English date to Nepali datefromnepali.date_converterimportconverternp_year,np_month,np_date=converter.english_to_nepali(en_year,en_month,en_date)Examplefromnepali.date_converterimportconverternp_year,np_month,np_date=converter.english_to_nepali(2023,2,7)print(np_year,np_month,np_date)# 2079 10 24Convert Nepali date to English datefromnepali.date_converterimportconverteren_year,en_month,en_date=converter.nepali_to_english(np_year,np_month,np_date)Examplefromnepali.date_converterimportconverteren_year,en_month,en_date=converter.nepali_to_english(2079,10,24)print(en_year,en_month,en_date)# 2023 2 7nepalidateCreating a new nepalidate objectfromnepali.datetimeimportnepalidate# nepalidate object with year, month, daynp_date=nepalidate(year,month,day)# nepalidate object with today's datenp_date=nepalidate.today()# parse nepali datenp_date=nepalidate.strptime('2078-01-18',format='%Y-%m-%d')Getting nepalidate object from python datetime# from date objectnp_date=nepalidate.from_date(date_obj)# from datetime objectnp_date=nepalidate.from_datetime(datetime_obj)Attributes and Methodsnp_date.year# 2078 (year)np_date.month# 1 (month)np_date.day# 18 (day)np_date.to_date()# datetime.date objectnp_date.to_datetime()# datetime.datetime objectnp_date.to_nepalidatetime()# nepalidatetime objectnp_date.strftime("%Y-%m-%d")# 2078-01-18np_date.strftime_ne("%Y-%m-%d")# २०७८-०१-१८np_date.weekday()# Sunday => 0, Monday => 1, ..., Saturday => 6nepalidatetimeCreating a new nepalidatetime objectfromnepali.datetimeimportnepalidatetime# nepalidate object with year, month, day, hour, minute, secondnp_datetime=nepalidatetime(year,month,day[,hour[,minute[,second]]])# nepalidate object with current date and timenp_datetime=nepalidate.now()np_datetime=nepalidate.today()# parse nepali datetimenp_datetime=nepalidatetime.strptime('2078-01-12 13:12',format='%Y-%m-%d%H:%M')Getting nepalidatetime object from python datetime# from date objectnp_datetime=nepalidatetime.from_date(date_obj)# from datetime objectnp_datetime=nepalidatetime.from_datetime(datetime_obj)Getting nepalidatetime object from nepalidatenp_datetime=nepalidatetime.from_nepalidate(nepali_date)Attributes and Methodsnp_date.year# 2078 (year)np_date.month# 1 (month)np_date.day# 18 (day)np_date.hour# 23 (hour)np_date.minute# 59 (minute)np_date.second# 59 (day)np_date.to_date()# datetime.date objectnp_date.to_datetime()# datetime.datetime objectnp_date.to_nepalidate()# nepalidatetime objectnp_date.to_time()# nepalitime object (datetime.time compatible)np_date.strftime("%Y-%m-%d%H:%M")# 2078-01-18 23:59np_date.strftime_ne("%Y-%m-%d%H:%M")# २०७८-०१-१८ २३:५९np_date.weekday()# Sunday => 0, Monday => 1, ..., Saturday => 6Timedelta support# timedelta addition and subtractionnp_datetime-datetime.timedelta(days=3)# returns nepalidatetime# comparison between two datesnp_datetime1-np_datetime2# returns timedelta objectnp_datetime1<np_datetime2# returns bool (True/False)np_datetime1>=datetime.datetime.now()# returns bool (True/False)...nepalihumanizeReturns readable form of nepali date.fromnepali.datetimeimportnepalihumanizenepalihumanize(datetime,[threshold,format])Thethresholdis and optional field and is in seconds and the format is for thestrftimeformat. If the datetime object crosses the threshold it print the date with the format. Theformatis also an optional and is%B %d, %Yin default.Examplefromnepali.datetimeimportnepalihumanize,nepalidatetimenp_datetime=nepalidatetime(2079,10,5)output=nepalihumanize(np_datetime)# output: ३ महिना अघिoutput=nepalihumanize(np_datetime,threshold=1400)# 1400 = 2 * 30 * 24; two months threshold# output: माघ ०५, २०७९timezoneNepaliTimeZoneYou can useNepaliTimeZonedirectly to your datetime object.fromnepali.timezoneimportNepaliTimeZonedatetime.datetime(2018,8,12,16,23,tzinfo=NepaliTimeZone())nowReturns current datetime object with timezonefromnepaliimporttimezonetimezone.now()datetime.now()vstimezone.now():datetime.now()doesn't contain timezone, buttimezone.now()will contain timezone of the system.utc_nowReturns current UTC datetime object (with timezone UTC)fromnepaliimporttimezonetimezone.utc_now()parseParses date with commonly used date formats. Auto detects date format. If you are sure about the format, please usestrptime.fromnepali.datetime.parserimportparsenp_datetime=parse(datetime_str)Examplenp_datetime=parse("2079-02-15")# 2079-02-15 00:00:00np_datetime=parse("२०७८-०१-१८")# 2078-01-15 00:00:00np_datetime=parse("2079/02/15")# 2079-02-15 00:00:00np_datetime=parse("2079-02-15 15:23")# 2079-02-15 15:23:00np_datetime=parse("2079-02-15 5:23 AM")# 2079-02-15 05:23:00np_datetime=parse("2079-02-15 5:23 AM")# 2079-02-15 05:23:00np_datetime=parse("Jestha 15, 2079")# 2079-02-15 00:00:00strftime() and strptime() Format CodesDirectiveMeaningExample%AWeekday as locale’s abbreviated name.Sun, Mon, …, Sat (आइत, सोम, …)%AWeekday as locale’s full name.Sunday, Monday, …, Saturday%dDay of the month as a zero-padded decimal number.01, 02, …, 31%-dDay of the month as a decimal number.1, 2, …, 31%BMonth as locale’s full name.Baishakh, Jestha, …, Chaitra%mMonth as a zero-padded decimal number.01, 02, …, 12%-mMonth as a decimal number.1, 2, …, 12%yYear without century as a zero-padded decimal number.00, 01, …, 99%YYear with century as a decimal number.2001, 2078, 2079, …, 2099%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23%-HHour (24-hour clock) as a decimal number.0, 1, 2, …, 23%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12%-IHour (12-hour clock) as a decimal number.1, 2, …, 12%pLocale’s equivalent of either AM or PM.AM, PM (en_US)%MMinute as a zero-padded decimal number.00, 01, …, 59%-MMinute as a decimal number.0, 1, 2, …, 59%SSecond as a zero-padded decimal number.00, 01, …, 59%-SSecond as a decimal number.0, 1, 2, …, 59%fMicrosecond as a decimal number, zero-padded to 6 digits.000000, 000001, …, 999999%%A literal'%'character.%NumbersfromnepaliimportnumberconvertConverts english number to nepali.np_number=number.convert("1234567890")# १२३४५६७८९०revertConverts english number to nepali.en_number=number.revert("१२३४५६७८९०")# 1234567890add_commaAdds comma in nepali numbers.number_text=number.add_comma("1234567890")# 1,23,45,67,890nepalinumbernepalinumberis a new data type, which can be used to represent Nepali (Devanagari) numbers. It allows us to perform arithmetic operations, just like with int and float. Additionally, it can be used to parse numbers and output them in Devanagari format.fromnepali.numberimportnepalinumberParsinga=nepalinumber("१८.२७")print(a)# 18.27b=nepalinumber(15)print(b)# 15Nepali (Devanagari) outputa=nepalinumber("18.27")print(a.str_ne())# १८.२७Arithmetic operationsa=nepalinumber("1")b=nepalinumber("२")c=a+b*3print(c)# 7Phone Numberfromnepaliimportphone_numberis_validChecks is the given number is a valid nepali phone number.phone_number.is_valid("9851377890")# Truephone_number.is_valid("+977-142314819")# Truephone_number.is_valid("8251377890")# FalseparseParse phone number and returns details of the number.phone_number.parse("9851377890")# {'type': 'Mobile', 'number': '9851377890', 'operator': <Operator: Nepal Telecom>}phone_number.parse("+977-142314819")# {'type': 'Landline', 'number': '0142314819', 'area_code': '01'}LocationsProvides details of Nepal's Province, District, and Municipality.fromnepali.locationsimportprovinces,districts,municipalitiesfromnepali.locations.utilsimportget_province,get_district,get_municipality# Provinceget_province(name="Bagmati")# Bagmati Province# Districtget_district(name="Kathmandu")# Kathmandu# Municipalityget_municipality(name="Kathmandu")# Kathmandu Metropolitan City# Municipalityget_municipality(name_nepali="विराटनगर")# Biratnagar Metropolitan CityFor DjangoWe have created a new Django package calleddjango-nepalito supportnepalipackage. For more information, please visitdjango-nepali.ContributionWe appreciate feedback and contribution to this package. To get started please see ourcontribution guide