JDManipulator.py 3.59 KB
from django.conf import settings
import datetime
import time
from decimal import *

JD_VALUE = 86400
TIMESTAMP_JD = 2440587.500000
DAILY_SECOND = 1 / 86400
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))

    return (time.mktime(current_time.timetuple()))


SIM_TIME_START = getSimTime()

def JulianSeconds(value):
    return (Decimal(value) / SECOND_DIV)

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

def getPreciseNightStart():
    return (Decimal(getNightStart()))

def getPreciseNightEnd():
    return (Decimal(getNightEnd()))

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

def secondsToPreciseJulianDate(time_seconds):
    return (Decimal(time_seconds) / 86400 + Decimal(TIMESTAMP_JD))

def julianSecondsToSeconds(seconds_julian):
    return seconds_julian * 86400

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)
    current_time = current_time.replace(hour=18, minute=0, second=0, microsecond=0)
    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)
    current_time = current_time.replace(hour=6, minute=0, second=0, microsecond=0)
    return (time.mktime(current_time.timetuple()))

def getDefaultNightStart():
    current_time = datetime.datetime.now()
    print("*********************")
    print("*********************")
    print("current time is", current_time)
    print("SIM_TIME_START", SIM_TIME_START)
    print("*********************")
    print("*********************")
    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)
    return (time.mktime(current_time.timetuple()))

def getDefaultNightEnd():
    current_time = datetime.datetime.now()
    current_time += datetime.timedelta(days=1)
    current_time = current_time.replace(hour=6, minute=0, second=0, microsecond=0)
    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())