Chinese Zodiac - python project

Denis McMahon denismfmcmahon at gmail.com
Thu Nov 14 02:07:11 CET 2013


On Wed, 13 Nov 2013 16:44:03 -0800, edmundicon wrote:

> Den tisdagen den 12:e november 2013 kl. 23:50:03 UTC+1 skrev Denis
> McMahon:
>> On Tue, 12 Nov 2013 14:04:08 -0800, edmundicon wrote:
>> 
>> 
>> 
>> > Greetings everyone! This is my first post on this forum :)
>> 
>> 
>> > 
>> > TL;DR: I want to convert the gregorian years into Chinese years, and
>> 
>> > deal with the fact that the Chinese new years are different each
>> 
>> > gregorian year. If I manage to do that, I'll know which year the user
>> > is
>> 
>> > born in Chinese years and can then give him a personal description
>> > based
>> 
>> > upon that year!
>> 
>> 
>> > 
>> > I started to learn Python programming language 2 months ago (noob),
>> > but
>> 
>> > I like it and I feel like if I keep learning I might become a great
>> 
>> > programmer one day!
>> 
>> 
>> > 
>> > I recently started a small Python project in which my mission is to
>> > give
>> 
>> > a personal description to a user based upon the year he / she is born
>> > in
>> 
>> > the Chinese Zodiac and I have run into some trouble. For instance, if
>> 
>> > someone is born on the 15'th January 1990, he is actually born 1989
>> 
>> > because the Chinese new year occurred on the 27:th January that year.
>> 
>> 
>> > 
>> > I have a text file which shows when the Chinese new years in
>> > gregorian
>> 
>> > years (normal years), starting from 1900-01-31 to 2007-02-18. It goes
>> 
>> > like this:
>> 
>> > 1900-1-31 1901-2-19 1902-2-08 1903-1-29 1904-2-16 1905-2-04 1906-1-25
>> 
>> > ...(and so on)
>> 
>> > 2007-02-18 ( I can't see the logic behind this really)
>> 
>> 
>> > 
>> > The Chinese calendar is divided into cycles of 60 years each, and
>> > each
>> 
>> > year has a combination of an animal and an element. There are 12
>> > animals
>> 
>> > and 5 elements, the animals changes each year, and the elements every
>> 
>> > other year. The current cycle was initiated in the year of 1984 which
>> 
>> > was the year of the Wood Rat. The personal descriptions for each
>> 
>> > combination has conveniently also been provided in text files.
>> 
>> 
>> > 
>> > The animals are in this order:
>> 
>> 
>> > 
>> > Rat Ox Tiger Rabbit Dragon Snake Horse Sheep Monkey Rooster Dog Boar
>> 
>> 
>> > 
>> > And the elements are:
>> 
>> 
>> > 
>> > Wood Fire Earth Metal Water
>> 
>> 
>> > 
>> > I have already created a modulus method which takes the input year
>> > (the
>> 
>> > gregorian year you were born) and gives you an element and an animal,
>> 
>> > for example if you type "1990" you are given Metal Horse. The problem
>> > I
>> 
>> > now have is to convert the gregorian years into Chinese years, and
>> > deal
>> 
>> > with the fact that the Chinese new years are different each gregorian
>> 
>> > year. If I manage to do that, I'll know which year the user is born
>> > in
>> 
>> > Chinese years and can then give him a personal description based upon
>> 
>> > that year!
>> 
>> 
>> > 
>> > Any advice will be greatly appreciated! Have a nice day :)
>> 
>> 
>> 
>> Here is one suggestion
>> 
>> 
>> 
>> Write a function to convert a gregorian date into the number of days
>> 
>> since 1st January 1900 (we'll call 1st january 1900 your epoch). You
>> will
>> 
>> need to take leap years into account.
>> 
>> 
>> 
>> Convert the users date of birth using this function.
>> 
>> 
>> 
>> Convert your chinese new year dates using this function.
>> 
>> 
>> 
>> You now have the simple task of comparing the users date of birth as
>> 
>> daynumber_since_epoch with the start date of each chinese year as
>> 
>> daynumber_since_epoch.
>> 
>> 
>> 
>> You may wish to create a list of tuples where each tuple has start_day,
>> 
>> end_day, year_name:
>> 
>> 
>> 
>> years = [(0,30,"stone pig"),(31,414,"stone weasel") ....... ]
>> 
>> 
>> 
>> You should be able to automate creating this list.
>> 
>> 
>> 
>> You could then search through the list of tuples with a function such
>> as:
>> 
>> 
>> 
>> yearname( birthdate ):
>> 
>>     foreach thing in years
>> 
>>         if birthdate is in the range specified by the thing
>> 
>>             return the yearname from the thing
>> 
>>     return "constipated program"
>> 
>> 
>> 
>> (this is obviously not written as python code, you have to do that bit
>> 
>> yourself)
>> 
>> 
>> 
>> --
>> 
>> Denis McMahon, denismfmcmahon at gmail.com
> 
> Sir, I am really happy that you answered, but could you *please*
> elaborate / give more great advice on how to write the function which
> converts your gregorian date into a Chinese year?
> 
> Should I do it like this? For example, if I pretend I'm born 1902-2-02,
> the difference in days between this date and 1900-1-01 is 366(because
> 1900 is a leap year) + 365(for 1901) + 31 (for january 1902) + 2(2 days
> into february 1902) = 764 days.
> 
> How will this integer value help me convert my gregorian date into
> Chinese years? I know, by knowing that 1984 (gregorian year) is the year
> of the Wood Rat, that 1900 (still gregorian year) should be Metal Rat,
> which means it is the 1st value in the animals list and the 4th value in
> the elements list, but I am still confused how to proceed.
> 
> If this is any help, I have written some code that makes you type in a
> gregorian year (not a date), compares it with 1984 (gregorian year) and
> gives you an animal & an element for that year:
> 
> [CODE]
> comp_year = 1984
>     
> greg_year = input("In which gregorian year were you born? ")
> dif_year = int(greg_year) - comp_year
> 
> steps_year = dif_year % len(animals)
> print(animals[steps_year])
> steps_elem = dif_year / 2 steps_elem = int(steps_elem % len(elements))
> print(elements[steps_elem])
> 
> [/CODE]
> 
> But I still dont know how to convert a gregorian date into a Chinese
> year.
> 
> Once again, thanks for your previous answer!

Define "day zero" as 1st of January 1900

Now, you have a list of the gregorian dates of chinese new years
You also have the gregorian date of birth of the person p

What you need is a function that converts gregorian date dd mm yyyy into 
days since day zero.

Now, you can take your chinese new year data, and convert the start date 
for any chinese new year into "days since day zero"

You can also take the gregorian birth date of a person and convert it 
into "days since day zero"

Now you only need to compare values expressed as "days since day zero"

You don't need to convert a gregorian date to a chinese year, you already 
start dates of chinese years as gregorian dates. What you need to do is 
simply compare two gregorian dates, using a common base datum.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list