Skip to content

Instantly share code, notes, and snippets.

@bheesham
Created May 30, 2012 03:17
Show Gist options
  • Save bheesham/2833516 to your computer and use it in GitHub Desktop.
Save bheesham/2833516 to your computer and use it in GitHub Desktop.
A clock that prints out the time in binary.
"""
Copyright (C) 2012 Bheesham Persaud.
pybinaryclock - A clock that prints out the time in binary.
pybinaryclock is Beerware <http://en.wikipedia.org/wiki/Beerware>.
Format: HHMMSS
Usage: python pybinclock.py
"""
import argparse
from time import strftime, localtime
from math import log, pow
# Gets the local time, returns the time in list format: [H, M, S] (Hours in 24 hour format)
def localtime_2_lst() :
utime = localtime()
return [
strftime( "%H", utime ),
strftime( "%M", utime ),
strftime( "%S", utime )
]
# Takes the list format of the time, turns it into binary, in an list
def lst_2_hbin( lst ) :
retlst = []
temp = []
for i in range( len( lst ) ) :
for j in range( len( lst[i] ) ) :
temp.append( num_2_bin( int( lst[i][j] ) ) ) # Don't forget to convert the string to an integer.
retlst.append( temp[:] ) # Short form for adding the temporary values to the return list
del temp[:] # Short form for clearing the list.
return retlst
def num_2_bin( num ) :
temp = 0
bin = [ 0, 0, 0, 0 ] # Start off with 0
while ( num > 0 ) :
temp = int( log( num, 2 ) )
num = num - pow( 2, temp )
bin[ temp ] = 1
bin.reverse() # Reverse it since we put the 1's in the list in reverse order. e.g. 8 = 0001
return bin
# Print the list
def printhbin( lst ) :
string = ""
digit = 0
while digit < len( lst[0][0] ) : # Figure out how long the binary string is.
for i in range( len( lst ) ) : # We have to go through each pair twice since each number
for j in range( len( lst[i] ) ) : # is represented by two columns of binary.
string = string + " " + str( lst[i][j][digit] )
string = string + "\n"
digit = digit + 1
print string
def main():
# "Get it, format it, switch it, print it, technologic, technologic, ..."
localtime = localtime_2_lst()
bintime = lst_2_hbin( localtime )
printhbin( bintime )
if __name__ == "__main__":
parser = argparse.ArgumentParser( version = '1.0', description = 'A clock that prints out the time in binary.' )
arguments = parser.parse_args()
main()
@psimonyi
Copy link

psimonyi commented Jun 2, 2012

Why lists of strings/lists-of-things [[hh],[mm],[ss]] instead of just a string/list-of-things [hhmmss]?

@bheesham
Copy link
Author

bheesham commented Jun 6, 2012

No idea, I just wrote what first came to mind, changed later.
I'll make those changes when I get the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment