Skip to content

Instantly share code, notes, and snippets.

@sakibccr
Last active May 28, 2025 10:59
Show Gist options
  • Save sakibccr/178103de62ec6cff427800e8c2b8f6a4 to your computer and use it in GitHub Desktop.
Save sakibccr/178103de62ec6cff427800e8c2b8f6a4 to your computer and use it in GitHub Desktop.
# simple script to print the current time in bcd
import time
def digit_to_bcd(num: int) -> list:
'''
Convert a single decimal digit (0-9) to a 4-bit BCD.
Return a list of booleans, each item is a bit.
'''
if 0 <= num <= 9:
return [bool(int(bit)) for bit in f'{num:04b}']
return ValueError("digit_to_bcd accepts only digits from 0 to 9.")
def get_bcd_time_rows() -> list[str]:
'''
Get the current time in HHMMSS format and convert each digit to BCD.
Return the formatted binary clock as a list of strings, one per row.
'''
local_time = time.localtime()
time_str = f'{local_time.tm_hour}{local_time.tm_min}{local_time.tm_sec}'
bcd_columns = [digit_to_bcd(int(d)) for d in time_str] # each nested list is a column, with 4 bits
# let's zip them for printing, since bcd clocks are vertical
bcd_rows = zip(*bcd_columns)
formatted_rows = []
for row in bcd_rows:
line = ''
for col, bit in enumerate(row):
line += '*' if bit else '.'
if col % 2 == 1:
line += ' ' # extra space after every second digit
formatted_rows.append(line)
return formatted_rows
def main():
for row in get_bcd_time_rows():
print(row)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment