One of my apps has a lunar calendar integrated, and since lunar calendars aren’t as predictable as solar calendars, I have to provide an offset from the backend to determine the correct date. That offset update process is tedious, and I have been forgetting to update it recently… Hence, automation is a must.
STEP 1: THE SOURCE
Let’s find a reliable source to fetch the lunar date from… I like this one.
STEP 2: THE PARSER
I first tried to use beautifulsoup, which was an utter failure. The library is so bad, and didn’t work as I expect. Maybe it was because of unicode issues, I don’t know…
In any case, I then just wrote my own regex, and wished I had just done that in the first place:
STEP 3: THE BASE DATE
Unfortunately, my app doesn’t query the date from the backend (silly me), it queries the offset instead (/facepalm). Hence, I need to know what the base date is in order to calculate the offset! The base date is calculated in the app from a library I compiled (called
MCEngine). So, all I had to do is write a command line wrapper around it to give me the date:
STEP 4: THE GLITCH
As with most software, there is this awkward edge case that you have to take care of. Since I am querying the date from the website above, I may (for example) query it on Monday, when they are displaying the date for Tuesday! So, that’s why in the code above I am printing the weekday (
stringifyWeekday). Then, using some simple rules, I can find the “circular distance” from two indexes with the following:
I realized there is another thing I must take care of… The
weekday_diff is always positive, but I need to know in which direction is the diff (i.e. is the objc date ahead, or is the website ahead?).