Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Tuesday, January 08, 2008

Python: Gregorian date to Hijri date conversion and vice versa

Ported from Javascript. Credit goes to the original author of the Javascript (the script is quite widespread but I've never seen those using the script write the proper credit). Below is the code:

#!/usr/bin/env python

import math

def intPart(floatNum):
if floatNum < -0.0000001: return math.ceil(floatNum - 0.0000001)
return math.floor(floatNum + 0.0000001)

def Gregorian2Hijri(yr, mth, day):
if ((yr > 1582) or ((yr == 1582) and (mth > 10)) or \
((yr == 1582) and (mth == 10) and (day > 14))):
jd1 = intPart((1461 * (yr + 4800 + \
intPart
((mth - 14) / 12.0))) / 4)
jd2 = intPart((367 * (mth - 2 - 12 * \
(intPart((mth - 14) / 12.0)))) / 12)
jd3 = intPart((3 * (intPart((yr + 4900 + \
intPart((mth - 14) / 12.0)) / 100))) / 4)
jd = jd1 + jd2 - jd3 + day - 32075
else:
jd1 = intPart((7 * (yr + 5001 + \
intPart
((mth - 9) / 7.0))) / 4)
jd2 = intPart((275 * mth) / 9.0)
jd = 367 * yr - jd1 + jd2 + day + 1729777

l = jd - 1948440 + 10632
n = intPart((l - 1) /10631.0)
l = l - 10631 * n + 354
j1 = (intPart((10985 - l) / 5316.0)) * (intPart((50 * l) / 17719.0))
j2 = (intPart(l / 5670.0)) * (intPart((43 * l) / 15238.0))
j = j1 + j2
l1 = (intPart((30 - j) / 15.0)) * (intPart((17719 * j) / 50.0))
l2 = (intPart(j / 16.0)) * (intPart((15238 * j) / 43.0))
l = l - l1 - l2 + 29
m = intPart((24 * l) / 709.0)
d = l - intPart((709 * m) / 24.0)
y = 30 * n + j - 30

return y, m, d

def Hijri2Gregorian(yr, mth, day):
jd1 = intPart((11 * yr + 3) / 30.0)
jd2 = intPart((mth - 1) / 2.0)
jd = jd1 + 354 * yr + 30 * mth - jd2 + day + 1948440 - 385

if jd > 2299160:
l = jd + 68569
n = intPart((4 * l) / 146097.0)
l = l - intPart((146097 * n + 3) / 4.0)
i = intPart((4000 * (l + 1)) / 1461001.0)
l = l - intPart((1461 * i) / 4.0) + 31
j = intPart((80 * l) / 2447.0)
d = l - intPart((2447 * j) / 80.0)
l = intPart(j / 11.0)
m = j + 2 - 12 * l
y = 100 * (n - 49) + i + l
else:
j = jd + 1402
k = intPart((j - 1) / 1461.0)
l = j - 1461 * k
n = intPart((l - 1) / 365.0) - intPart(l / 1461.0)
i = l - 365 * n + 30
j = intPart((80 * i) / 2447.0)
d = i - intPart((2447 * j) / 80.0)
i = intPart(j / 11.0)
m = j + 2 - 12 * i
y = 4 * k + n + i - 4716

return y, m, d
Usage:
# Convert from Gregorian to Hijri
print Gregorian2Hijri(1972, 12, 9)
print Gregorian2Hijri(2008, 1, 8)

# Convert from Hijri to Gregorian
print Hijri2Gregorian(1392, 11, 3)
print Hijri2Gregorian(1428, 12, 29)

Wednesday, March 14, 2007

Scheduleable Event Loop for asyncore/asynchat

Been a while since I write a blog. Below is the Python code which is quite usable to replace the default asyncore loop. It's based on Sam Rushing code which can be found at http://squirl.nightmare.com/medusa/async_sockets.html

import sys, time, asyncore

map = asyncore.socket_map

class EventLoop:
def __init__(self):
self.events = {}
self.poll = asyncore.poll3
self.__quit = None

def loop(self, timeout=30.0):
while map:
if self.__quit is True: break
now = int(time.time())
for k, v in self.events.iteritems():
if now >= k:
for o in v:
try:
o[0](*o[1][0], **o[1][1])
except:
type, value, tb = sys.exc_info()
parm = str(o[1])
logstr = "Error in Evenloop processing " + \
"scheduled task."
logstr = "Error details: %s: %s" % (type, value)
logstr = "Error details(parm): %s" % parm
logstr = "Error details(tb): %s" % \
tb.tb_frame.f_code
del self.events[k]
self.poll(timeout)

def schedule(self, delta, callback, *args, **kargs):
now = int(time.time())
if self.events.has_key(now + delta) is False:
self.events[now + delta] = []
self.events[now + delta].append((callback, (args, kargs)))

def unschedule(self, callback):
for k, v in self.events.iteritems():
for o in v:
if o[0] is callback:
del self.events[k][v][self.events[k][v].index(o)]
if len(v) == 0:
del self.events[k]

def quit(self):
self.__quit = True

Thursday, June 08, 2006

My little rant on Python

Today I thought to myself, why not try to play around with SysV shared memory and semaphore under Python. Then, to my surprise, this time the battery is not included. Yes, there is no module that come with standard Python which offer this facility. After painstakingly Google around, I found 2 modules which provide a mechanism to this IPC - but I'm not too convinced with these 2 modules. I guess I need to dig into the C codes of these external modules to boost my confidence level. Till then, I'd stick to Unix domain socket and pipe for my IPC under Python.

Thursday, May 25, 2006

Learn to program in ten years

The first time I read Peter Norvig's essay titled Teach Yourself Programming in Ten Years, I don't give much thought about it. Today, I've re-read and started to ponder my own progress over the years in achieving hacker's enlightenment.

The one that struck me was the similarity in my own experience in attempting to learn and grasp what I've learn. Like him, I've read numerous books, howtos, FAQ and other documents, yet I still feel like a novice. So what really went wrong? Why I'm still feel I'm at the same level as I was 2 or 3 years ago? Well, Peter Norvig certainly had given all the pointers but I failed to recognize them 2 years ago.

However, in my effort to learn, it's better late than never. The pointers he had laid out in the essay must be taken seriously.