Friday 16 September 2011

Python script for convert Georgion date to the Nepali date & Nepali date to Georgion Date

#     **
#     * currently can only calculate the date between BS 2000-2089
#     * currently can only calculate the date between AD 1944-2033...
#     *
#     **


import time
from datetime import datetime
from dateutil.relativedelta import relativedelta

bs = [
    [0,[2000,30,32,31,32,31,30,30,30,29,30,29,31]],
    [1,[2001,31,31,32,31,31,31,30,29,30,29,30,30]],
    [2,[2002,31,31,32,32,31,30,30,29,30,29,30,30]],
    [3,[2003,31,32,31,32,31,30,30,30,29,29,30,31]],
    [4,[2004,30,32,31,32,31,30,30,30,29,30,29,31]],
    [5,[2005,31,31,32,31,31,31,30,29,30,29,30,30]],
    [6,[2006,31,31,32,32,31,30,30,29,30,29,30,30]],
    [7,[2007,31,32,31,32,31,30,30,30,29,29,30,31]],
    [8,[2008,31,31,31,32,31,31,29,30,30,29,29,31]],
    [9,[2009,31,31,32,31,31,31,30,29,30,29,30,30]],
    [10,[2010,31,31,32,32,31,30,30,29,30,29,30,30]],
    [11,[2011,31,32,31,32,31,30,30,30,29,29,30,31]],
    [12,[2012,31,31,31,32,31,31,29,30,30,29,30,30]],
    [13,[2013,31,31,32,31,31,31,30,29,30,29,30,30]],
    [14,[2014,31,31,32,32,31,30,30,29,30,29,30,30]],
    [15,[2015,31,32,31,32,31,30,30,30,29,29,30,31]],
    [16,[2016,31,31,31,32,31,31,29,30,30,29,30,30]],
    [17,[2017,31,31,32,31,31,31,30,29,30,29,30,30]],
    [18,[2018,31,32,31,32,31,30,30,29,30,29,30,30]],
    [19,[2019,31,32,31,32,31,30,30,30,29,30,29,31]],
    [20,[2020,31,31,31,32,31,31,30,29,30,29,30,30]],
    [21,[2021,31,31,32,31,31,31,30,29,30,29,30,30]],
    [22,[2022,31,32,31,32,31,30,30,30,29,29,30,30]],
    [23,[2023,31,32,31,32,31,30,30,30,29,30,29,31]],
    [24,[2024,31,31,31,32,31,31,30,29,30,29,30,30]],
    [25,[2025,31,31,32,31,31,31,30,29,30,29,30,30]],
    [26,[2026,31,32,31,32,31,30,30,30,29,29,30,31]],
    [27,[2027,30,32,31,32,31,30,30,30,29,30,29,31]],
    [28,[2028,31,31,32,31,31,31,30,29,30,29,30,30]],
    [29,[2029,31,31,32,31,32,30,30,29,30,29,30,30]],
    [30,[2030,31,32,31,32,31,30,30,30,29,29,30,31]],
    [31,[2031,30,32,31,32,31,30,30,30,29,30,29,31]],
    [32,[2032,31,31,32,31,31,31,30,29,30,29,30,30]],
    [33,[2033,31,31,32,32,31,30,30,29,30,29,30,30]],
    [34,[2034,31,32,31,32,31,30,30,30,29,29,30,31]],
    [35,[2035,30,32,31,32,31,31,29,30,30,29,29,31]],
    [36,[2036,31,31,32,31,31,31,30,29,30,29,30,30]],
    [37,[2037,31,31,32,32,31,30,30,29,30,29,30,30]],
    [38,[2038,31,32,31,32,31,30,30,30,29,29,30,31]],
    [39,[2039,31,31,31,32,31,31,29,30,30,29,30,30]],
    [40,[2040,31,31,32,31,31,31,30,29,30,29,30,30]],
    [41,[2041,31,31,32,32,31,30,30,29,30,29,30,30]],
    [42,[2042,31,32,31,32,31,30,30,30,29,29,30,31]],
    [43,[2043,31,31,31,32,31,31,29,30,30,29,30,30]],
    [44,[2044,31,31,32,31,31,31,30,29,30,29,30,30]],
    [45,[2045,31,32,31,32,31,30,30,29,30,29,30,30]],
    [46,[2046,31,32,31,32,31,30,30,30,29,29,30,31]],
    [47,[2047,31,31,31,32,31,31,30,29,30,29,30,30]],
    [48,[2048,31,31,32,31,31,31,30,29,30,29,30,30]],
    [49,[2049,31,32,31,32,31,30,30,30,29,29,30,30]],
    [50,[2050,31,32,31,32,31,30,30,30,29,30,29,31]],
    [51,[2051,31,31,31,32,31,31,30,29,30,29,30,30]],
    [52,[2052,31,31,32,31,31,31,30,29,30,29,30,30]],
    [53,[2053,31,32,31,32,31,30,30,30,29,29,30,30]],
    [54,[2054,31,32,31,32,31,30,30,30,29,30,29,31]],
    [55,[2055,31,31,32,31,31,31,30,29,30,29,30,30]],
    [56,[2056,31,31,32,31,32,30,30,29,30,29,30,30]],
    [57,[2057,31,32,31,32,31,30,30,30,29,29,30,31]],
    [58,[2058,30,32,31,32,31,30,30,30,29,30,29,31]],
    [59,[2059,31,31,32,31,31,31,30,29,30,29,30,30]],
    [60,[2060,31,31,32,32,31,30,30,29,30,29,30,30]],
    [61,[2061,31,32,31,32,31,30,30,30,29,29,30,31]],
    [62,[2062,30,32,31,32,31,31,29,30,29,30,29,31]],
    [63,[2063,31,31,32,31,31,31,30,29,30,29,30,30]],
    [64,[2064,31,31,32,32,31,30,30,29,30,29,30,30]],
    [65,[2065,31,32,31,32,31,30,30,30,29,29,30,31]],
    [66,[2066,31,31,31,32,31,31,29,30,30,29,29,31]],
    [67,[2067,31,31,32,31,31,31,30,29,30,29,30,30]],
    [68,[2068,31,31,32,32,31,30,30,29,30,29,30,30]],
    [69,[2069,31,32,31,32,31,30,30,30,29,29,30,31]],
    [70,[2070,31,31,31,32,31,31,29,30,30,29,30,30]],
    [71,[2071,31,31,32,31,31,31,30,29,30,29,30,30]],
    [72,[2072,31,32,31,32,31,30,30,29,30,29,30,30]],
    [73,[2073,31,32,31,32,31,30,30,30,29,29,30,31]],
    [74,[2074,31,31,31,32,31,31,30,29,30,29,30,30]],
    [75,[2075,31,31,32,31,31,31,30,29,30,29,30,30]],
    [76,[2076,31,32,31,32,31,30,30,30,29,29,30,30]],
    [77,[2077,31,32,31,32,31,30,30,30,29,30,29,31]],
    [78,[2078,31,31,31,32,31,31,30,29,30,29,30,30]],
    [79,[2079,31,31,32,31,31,31,30,29,30,29,30,30]],
    [80,[2080,31,32,31,32,31,30,30,30,29,29,30,30]],
    [81,[2081, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30]],
    [82,[2082, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30]],
    [83,[2083, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30]],
    [84,[2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30]],
    [85,[2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30]],
    [86,[2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30]],
    [87,[2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30]],
    [88,[2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30]],
    [89,[2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30]],
    [90,[2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30]]
    ]

nep_date = {'year':'', 'month':'', 'date':'', 'day':'','nmonth':'','num_day':''}
eng_date = {'year':'', 'month':'', 'date':'', 'day':'','emonth':'','num_day':''}
debug_info = "";


# Calculates wheather english year is leap year or not
#
# @param integer year
# @return boolean
#
def is_leap_year(year):

    a = year
    if (a%100==0):
        if(a%400==0):
            return True
        else:
            return False

    else:
        if (a%4==0):
            return True
        else:
            return False


def get_nepali_month(m):
    n_month = False

    if m==1:
        n_month = "Baisakh"

    if m==2:
        n_month = "Jestha"

    if m==3:
        n_month = "Ashadh"

    if m==4:
        n_month = "Shrawan"

    if m==5:
        n_month = "Bhadra"

    if m==6:
        n_month = "Ashwin"

    if m==7:
        n_month = "Kartik"

    if m==8:
        n_month = "Mangshir"

    if m==9:
        n_month = "Poush"

    if m==10:
        n_month = "Magh"

    if m==11:
        n_month = "Falgun"

    if m==12:
        n_month = "Chaitra"

    return  n_month


def get_english_month(m):
    eMonth = False

    if m==1:
        eMonth = "January"

    if m==2:
        eMonth = "February"

    if m==3:
        eMonth = "March"

    if m==4:
        eMonth = "April"

    if m==5:
        eMonth = "May"

    if m==6:
        eMonth = "June"

    if m==7:
        eMonth = "July"

    if m==8:
        eMonth = "August"

    if m==9:
        eMonth = "September"

    if m==10:
        eMonth = "October"

    if m==11:
        eMonth = "November"

    if m==12:
        eMonth = "December"

    return eMonth


def get_day_of_week(day):       

    if day==1:
        day = "Sunday"
   
    if day==2:
        day = "Monday"
   
    if day==3:
        day = "Tuesday"

    if day==4:
        day = "Wednesday"

    if day==5:
        day = "Thursday"

    if day==6:
        day = "Friday"

    if day==7:
        day = "Saturday"

    return day


def is_range_eng(yy, mm, dd):
    if yy<1944 or yy>2033:
        debug_info = "Supported only between 1944-2032"
        print debug_info
        return False

    if mm<1 or mm >12:
        debug_info = "Error! value 1-12 only"
        print debug_info
        return False

    if dd<1 or dd >31:
        debug_info = "Error! value 1-31 only"   
        print debug_info       
        return False

    return True


def is_range_nep(yy, mm, dd):       
    if(yy<2000 or yy>2089):
        debug_info="Supported only between 2000-2089"
        print debug_info
        return False

    if(mm<1 or mm >12):
        debug_info="Error! value 1-12 only"
        print debug_info
        return False
   
    if(dd<1 or dd >32):
        debug_info="Error! value 1-31 only"
        print debug_info   
        return False       

    return True


#     **
#     * currently can only calculate the date between AD 1944-2033...
#     *
#     * @param unknown_type yy
#     * @param unknown_type mm
#     * @param unknown_type dd
#     * @return unknown
#     **
   
def eng_to_nep(yy,mm,dd):

    if (is_range_eng(yy, mm, dd) == False):

        return False

    else:           

        #english month data.
        month = [31,28,31,30,31,30,31,31,30,31,30,31]
        lmonth = [31,29,31,30,31,30,31,31,30,31,30,31]

        def_eyy = 1944        #spear head english date...
        def_nyy = 2000
        def_nmm = 9
        def_ndd = 17-1        #spear head nepali date...
        total_eDays=0
        total_nDays=0
        a=0
        day=7-1                #all the initializations...
        m = 0
        y = 0
        i =0
        j = 0
        numDay=0
       
        # count total no. of days in-terms of year
        for i in range(0,(int(yy)-def_eyy)):    #total days for month calculation...(english)
            if(is_leap_year(def_eyy+i)==1):
                for j in range(0,12):
                    total_eDays += lmonth[j]
            else:
                for j in range(0,12):
                    total_eDays += month[j]

        # count total no. of days in-terms of month                   
        for i in range(0, mm-1):       
            if(is_leap_year(yy)==1):
                total_eDays += lmonth[i]
            else:
                total_eDays += month[i]
       
        # count total no. of days in-terms of date
        total_eDays += dd
       
       
        i = 0
        j = def_nmm                   
        total_nDays = def_ndd
        m = def_nmm
        y = def_nyy
       
        # count nepali date from array
        while(total_eDays != 0):
            #print " i and j",i,j
            a = bs[i][1][j]
            total_nDays = total_nDays + 1                #count the days
            day = day + 1                                #count the days interms of 7 days
            if(total_nDays > a):
                m = m + 1
                total_nDays = 1
                j = j + 1

            if day > 7:
                day = 1
            if m > 12:
                y = y + 1
                m = 1

            if j > 12:
                j = 1
                i = i + 1

            total_eDays = total_eDays - 1
       
        numDay=day
       
        nep_date["year"] = y
        nep_date["month"] = m
        nep_date["date"] = total_nDays
        nep_date["day"] = get_day_of_week(day)
        nep_date["nmonth"] = get_nepali_month(m)
        nep_date["num_day"] = numDay
        return nep_date


   
#     **
#     * currently can only calculate the date between BS 2000-2089
#     *
#     * @param unknown_type yy
#     * @param unknown_type mm
#     * @param unknown_type dd
#     * @return unknown
#     **

def nep_to_eng(yy,mm,dd):
   
    def_eyy = 1943
    def_emm=4
    def_edd=14-1        # init english date.
    def_nyy = 2000
    def_nmm = 1
    def_ndd = 1        # equivalent nepali date.
    total_eDays=0
    total_nDays=0
    a=0
    day=4-1        # initializations...
    m = 0
    y = 0
    i=0
    k = 0
    numDay = 0
    j=0
   
    month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
    lmonth = [0,31,29,31,30,31,30,31,31,30,31,30,31]
   
    if is_range_nep(yy, mm, dd)==False:
        return False
       
    else :
       
        # count total days in-terms of year
        for i in range(0, (yy-def_nyy)):   
            for j in range(1,13):
                total_nDays += bs[k][1][j]
            k = k + 1
       
        # count total days in-terms of month           
        for j in range(1,mm):
            total_nDays += bs[k][1][j]


        # count total days in-terms of dat
        total_nDays += dd           
       
        #calculation of equivalent english date...
        total_eDays = def_edd
        m = def_emm
        y = def_eyy
        while(total_nDays != 0):
            if(is_leap_year(y)):
                a = lmonth[m]

            else:
                a = month[m]

            total_eDays = total_eDays + 1
            day = day + 1

            if(total_eDays > a):
                m = m + 1
                total_eDays = 1

                if(m > 12):
                    y = y + 1
                    m = 1

            if(day > 7):
                day = 1;
            total_nDays = total_nDays - 1

        numDay = day

        eng_date["year"] = y                   
        eng_date["month"] = m                   
        eng_date["date"] = total_eDays       
        eng_date["day"] = get_day_of_week(day)                   
        eng_date["emonth"] = get_english_month(m)             
        eng_date["num_day"] = numDay           
       
        return eng_date           



#print "Enter Date in (yy/mm/dd) format"
print "in which you want to convert \n for eng to napali 1\n for nepali to eng 2"
n = raw_input()

print "Enter Year"
yy = raw_input()
print "Enter Month"
mm = raw_input()
print "Enter date"
dd= raw_input()

if int(n) == 1:
    date_n=eng_to_nep(int(yy),int(mm),int(dd))
    print "nowwwwwwwww nep",date_n

if int(n) == 2:
    date_n=nep_to_eng(int(yy),int(mm),int(dd))
    print "nowwwwwwwww eng",date_nhttp://turkeshpatel.blogspot.com/

2 comments: