Blame view

src/utils/JDManipulator.py 3.59 KB
ff448d43   Jeremy   Update
1
from django.conf import settings
65149de7   Jeremy   Update
2
3
4
5
6
7
import datetime
import time
from decimal import *

JD_VALUE = 86400
TIMESTAMP_JD = 2440587.500000
05038bc8   Jeremy   Majordome logic i...
8
DAILY_SECOND = 1 / 86400
ff448d43   Jeremy   Update
9
10
11
12
13
14
15
16
17
18
19
SECOND_DIV = 86400

def getSimTime():
    current_time = datetime.datetime.now()
    if current_time.minute == 59:
        current_time = current_time.replace(hour=(current_time.hour + 1), minute=0, second=10)
    else:
        if current_time.second >= 50:
            current_time = current_time.replace(minute=current_time.minute + 1, second=10)
        else:
            current_time = current_time.replace(second=(current_time.second + 10))
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
20

ff448d43   Jeremy   Update
21
22
    return (time.mktime(current_time.timetuple()))

05bdcc44   Etienne Pallier   BIG DEMO tests (s...
23

ff448d43   Jeremy   Update
24
SIM_TIME_START = getSimTime()
05038bc8   Jeremy   Majordome logic i...
25
26

def JulianSeconds(value):
ff448d43   Jeremy   Update
27
    return (Decimal(value) / SECOND_DIV)
65149de7   Jeremy   Update
28
29
30
31

def getPreciseCurrentTime():
    return (Decimal(getCurrentTime()))

05038bc8   Jeremy   Majordome logic i...
32
33
def getPreciseNightStart():
    return (Decimal(getNightStart()))
65149de7   Jeremy   Update
34

05038bc8   Jeremy   Majordome logic i...
35
36
def getPreciseNightEnd():
    return (Decimal(getNightEnd()))
65149de7   Jeremy   Update
37
38
39
40

def secondsToJulianDate(time_seconds):
    return (time_seconds / 86400 + TIMESTAMP_JD)

ff448d43   Jeremy   Update
41
42
43
def secondsToPreciseJulianDate(time_seconds):
    return (Decimal(time_seconds) / 86400 + Decimal(TIMESTAMP_JD))

c53a13e0   Jeremy   Updating a lot of...
44
45
46
def julianSecondsToSeconds(seconds_julian):
    return seconds_julian * 86400

65149de7   Jeremy   Update
47
48
49
50
51
52
53
54
55
56
57
def julianDateToSeconds(time_julian):
    return ((time_julian - TIMESTAMP_JD) * 86400)

def getCurrentTime():
    current_time = datetime.datetime.now()
    return (time.mktime(current_time.timetuple()))

def getNextDefaultNightStart():
    current_time = datetime.datetime.now()
    if (current_time.hour >= 18):
        current_time += datetime.timedelta(days=1)
e8e6f017   Jeremy   Reworked devices ...
58
    current_time = current_time.replace(hour=18, minute=0, second=0, microsecond=0)
65149de7   Jeremy   Update
59
60
61
62
63
64
65
66
    return (time.mktime(current_time.timetuple()))

def getNextDefaultNightEnd():
    current_time = datetime.datetime.now()
    if (current_time.hour >= 18):
        current_time += datetime.timedelta(days=2)
    else:
        current_time += datetime.timedelta(days=1)
e8e6f017   Jeremy   Reworked devices ...
67
    current_time = current_time.replace(hour=6, minute=0, second=0, microsecond=0)
65149de7   Jeremy   Update
68
69
70
71
    return (time.mktime(current_time.timetuple()))

def getDefaultNightStart():
    current_time = datetime.datetime.now()
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
72
73
74
75
76
77
    print("*********************")
    print("*********************")
    print("current time is", current_time)
    print("SIM_TIME_START", SIM_TIME_START)
    print("*********************")
    print("*********************")
ff448d43   Jeremy   Update
78
79
80
81
82
83
84
85
86
87
88
89
    if settings.SIMULATOR:
        if settings.SIMULATOR:
            return SIM_TIME_START
        if current_time.minute == 59:
            current_time = current_time.replace(hour=(current_time.hour + 1), minute=0, second=10)
        else:
            if current_time.second > 50:
                current_time = current_time.replace(minute=current_time.minute + 1, second=10)
            else:
                current_time = current_time.replace(second=(current_time.second + 10))
    else:
        current_time = current_time.replace(hour=18, minute=0, second=0, microsecond=0)
65149de7   Jeremy   Update
90
91
92
93
94
    return (time.mktime(current_time.timetuple()))

def getDefaultNightEnd():
    current_time = datetime.datetime.now()
    current_time += datetime.timedelta(days=1)
e8e6f017   Jeremy   Reworked devices ...
95
    current_time = current_time.replace(hour=6, minute=0, second=0, microsecond=0)
65149de7   Jeremy   Update
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    return (time.mktime(current_time.timetuple()))

def datetimeToJulianDate(current_time):
    return (time.mktime(current_time.timetuple()) / JD_VALUE + TIMESTAMP_JD)


''' TODO '''
def getNightStart():
    return (getDefaultNightStart())

''' TODO '''
def getNightEnd():
    return (getDefaultNightEnd())

''' TODO '''
def getNextNightStart():
    return (getNextDefaultNightStart())

''' TODO '''
def getNextNightEnd():
    return (getNextNightEnd())