aboutsummaryrefslogtreecommitdiffstats
path: root/.i3/py3status/80-datetime.py
blob: 5d578a02fc7f91dcf4ab083b2194e1b3165b9dbc (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Weitian LI <liweitianux@gmail.com>
# 2014/05/15

"""
datetime module for py3status
"""

## Get output of shell command:
## python 3.x:
## >>> subprocess.getoutput(cmd)
## >>> subprocess.getstatusoutput(cmd)
## python 2.7.x:
## >>> subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)

import re
import subprocess
import time


# regext to match date and time from output of 'date'
datetime_p = re.compile(r'(^\d+/\d+)\s*(\d+:\d+)')


class Py3status:
    """
    Datetime module to show current date and time.
    """

    def mydate(self, i3status_output_json, i3status_config):
        """
        Display current date.
        """
        POSITION = 0
        response = {
            'full_text': '',
            'name': 'mydate',
            'instance': 'first',
        }
        prompt = ''  # Icons: uF073 (calendar)
        # update datetime
        self._get_datetime()
        response['full_text'] = '{0} {1}'.format(prompt, self.date)
        # cache status for 5 seconds
        response['cached_until'] = time.time() + 5
        return (POSITION, response)

    def mytime(self, i3status_output_json, i3status_config):
        """
        Display current time.
        """
        POSITION = 1
        response = {
            'full_text': '',
            'name': 'mytime',
            'instance': 'first',
        }
        prompt = ''  # Icons: uF017 (clock)
        # update datetime
        self._get_datetime()
        response['full_text'] = '{0} {1}'.format(prompt, self.time)
        # cache status for 5 seconds
        response['cached_until'] = time.time() + 5
        return (POSITION, response)

    def _get_datetime(self):
        """
        Get current date and time
        """
        cmd = 'date "+%m/%d %H:%M"'
        datetime = subprocess.getoutput(cmd)
        dt_m = datetime_p.match(datetime)
        self.date, self.time = dt_m.group(1, 2)