Time In Words with Python

Have been playing with geektool this week. While tinkering with it, remembered a text clock that I saw in Opensuse Linux.

Sometime back, I wrote a fortran program to do this. But today cooked up a python script to do just that.

Here’s the result.

TimeInWords as geeklet powered by python

And here’s the code.

import datetime

class TimeInWords2():
    def __init__(self):
        self.words=["one", "two", "three", "four", "five", "six", "seven", "eight","nine", 
       "ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen",
       "seventeen", "eighteen", "nineteen", "twenty", "twenty one", 
       "twenty two", "twenty three", "twenty four", "twenty five", 
       "twenty six", "twenty seven", "twenty eight", "twenty nine", "half"]


    def caltime(self):
        hrs = dd.hour
        mins = dd.minute
        header="It is "
        if (hrs >12):
        if (mins == 0):
            hr = self.words[hrs-1]
            msg=header + hr + " o'clock."
        elif (mins < 31):      
               hr = self.words[hrs-1]
               mn = self.words[mins-1]
               msg = header + mn + " past " + hr + "."
            hr = self.words[hrs]
            mn =self.words[(60 - mins-1)]
            msg = header + mn + " to " + hr + "."
        return msg

if __name__ == '__main__':
    t = TimeInWords2()
    print t.caltime()


3 thoughts on “Time In Words with Python

    • I have foud the solution : it’s because in Python 3, print statement has been replaced with a print() function. So the last line is print (t.caltime())


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s