I made a binary clock

Posted on 2015-03-18 (Wed) in programming

I like this way of a binary clock. It’s easily readable cause you only have to count to 24 and not to 60. Also it just looks better. Maybe I’ll build a version for my wrist someday.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Binary clock - Runs under Windows/Linux/*NIX/OSX.
"""

import os
import time

__author__ = 'Ricardo Band'
__copyright__ = 'Copyright 2015, Ricardo Band'
__credits__ = ['Ricardo Band', ]
__license__ = 'MIT'
__version__ = '1.0.0'
__maintainer__ = 'Ricardo Band'
__email__ = 'email@ricardo.band'
__status__ = 'Production'
__docformat__ = 'reStructuredText'


def write_time(timestring):
    """
    Takes the time as string and prints the binary clock output.

    :param timestring: Time in the format %H:%M:%S
    :type timestring: str
    :returns: str -- binary clock output
    """
    numbers = {'0': '░░░░',
               '1': '■░░░',
               '2': '░■░░',
               '3': '■■░░',
               '4': '░░■░',
               '5': '■░■░',
               '6': '░■■░',
               '7': '■■■░',
               '8': '░░░■',
               '9': '■░░■'}

    for line in reversed(range(4)):
        for c in timestring:
            if c == ':':
                print(' ', end='')
            else:
                print(numbers[c][line], end='')
        print('')  # newline
    print(timestring.replace(':', ' ') if int(timestring[-1]) % 2 == 0 else timestring)

if __name__ == '__main__':
    try:
        while True:
            if os.name == 'nt':  # windows
                os.system('clr')
            else:
                os.system('clear')
            write_time(time.strftime('%H:%M:%S'))
            time.sleep(1)

    except KeyboardInterrupt:
        pass