Completed
Pull Request — master (#349)
by Tomaz
07:18 queued 04:45
created

GetWeekBoundariesTimestampsAction.run()   A

Complexity

Conditions 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4286
cc 2
1
import time
2
import datetime
3
4
from st2common.util import isotime
5
from st2actions.runners.pythonrunner import Action
6
7
8
class GetWeekBoundariesTimestampsAction(Action):
9
    def run(self, date=None):
10
        if date:
11
            dt = isotime.parse(date)
12
        else:
13
            dt = datetime.datetime.utcnow()
14
15
        start_dt = (dt - datetime.timedelta(days=dt.weekday()))
16
        end_dt = (start_dt + datetime.timedelta(days=6))
17
        start_dt = start_dt.replace(hour=0, minute=0, second=0, microsecond=0)
18
        end_dt = end_dt.replace(hour=23, minute=59, second=59, microsecond=0)
19
20
        start_timestamp = int(time.mktime(start_dt.timetuple()))
21
        end_timestamp = int(time.mktime(end_dt.timetuple()))
22
23
        return start_timestamp, end_timestamp
24