Commit 559ee8cb authored by Robert Sebille's avatar Robert Sebille
Browse files

Resolution ahordattools_conflict-20180530-234944.py

parent b4f0083d
#!/usr/bin/env python3
# -*-coding:Utf-8 -*
"""Tools for timestamps of AaaaHorodatage class, Child class of
AaaaHorodatage.
Timestamp format = SAAAA where S = the number of seconds
from the epoch until the date entered and AAAA =
the year of the timestamp.
Attribute(s):
- epoch: AaaaHorodatage class reference year of the timestamp.
Ranges: 0 <= epoch <= year <= 10000.
Ranges and input are managed, but not exceptions.
"""
#########################################
# GNU General Public License, version 3 #
############################################################################
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
# Robert Sebille, Belgium, 2018 - #
# #
############################################################################
# As compliant as possible with the PEP8 #
# https://www.python.org/dev/peps/pep-0008/ #
#############################################
import os
import sys
import math
from datetime import *
# value to test module
#from ahordat import AaaaHorodatage
# value for production
from aaaa.ahordat import AaaaHorodatage
class AaaaHorodatageTools(AaaaHorodatage):
##### For dev start
# in AaaaHorodatage:
# année de référence (attribut de classe)
# epoch = 1900
### private
# attributs
# def _get_ljm(self, i):
# ljm = property(_get_ljm)
# methodes
# def _bissextile(self, an = 0):
# def _input_entier(self, annonce=""):
# def _calcul_encode(self):
# def _cal def _input_entier(self, annonce=""):cul_decode(self, typ):
### méthodes publiques
# classe (cls)
# def get_epoch(cls):
# get_epoch = classmethod(get_epoch)
# def set_epoch(cls, num):
# set_epoch = classmethod(set_epoch)
# instance (self)
# def encode(self, d=""):
# def decode(self, typ="string", t=0):
##### For dev end
utc_server_offset = 0
def __init__(self):
AaaaHorodatage.__init__(self)
#AaaaHorodatageTools.utc_server_offset = get_utc_server_offset(cls)
### private
# attributs
# méthodes
def _convert_utc_server_offset(cls):
chaineoffset = "+"
if cls.utc_server_offset < 0:
chaineoffset = "-"
realoffset = math.fabs(cls.utc_server_offset)
if int(realoffset / (3600)) < 10:
chaineoffset += "0"
chaineoffset += str(int(realoffset / (3600))) + ":"
if int((realoffset / 60) % 60) < 10:
chaineoffset += "0"
chaineoffset += str(int((realoffset / 60) % 60))
return chaineoffset
_convert_utc_server_offset = classmethod(_convert_utc_server_offset)
### méthodes publiques
# class (cls)
def get_utc_server_offset(cls, typ=""):
"""Get the offset UTC for the server time.
Return:
- Number of seconds (default)
- String (+/-HH:MM) if typ = "string"
"""
cls.utc_server_offset =\
math.ceil(datetime.timestamp(datetime.now()) -
datetime.timestamp(datetime.utcnow()))
retour = cls.utc_server_offset
if typ == "string":
retour = cls._convert_utc_server_offset()
return retour
get_utc_server_offset = classmethod(get_utc_server_offset)
# instance (self)
def diffTs(self, typ="string", ts1=0, ts2=0):
"""Difference between 2 timestamps in days, hours, minutes, seconds.
Return typ =
- "string": return a string (YYYY-MM-DD HH:MM:SS). Default.
- "list": return a list (fixed order).
fixed order: year, month and day, hour, minutes, seconds.
- "dict" or "dictionary": return a dict (any order).
ts1 > 9999 = timestamp; ts1 = 0: keyboard input.
ts2 > 9999 = timestamp; ts2 = 0: keyboard input.
ts1, ts2 = none: keyboard inputs
"""
self.ts1 = ts1
self.ts2 = ts2
# if keyboard
if self.ts1 == 0:
while self.ts1 < 10000:
print("Decode timestamp 1 [> 9999, integer]: ", end="")
self.ts1 = self._input_entier()
if self.ts2 == 0:
while self.ts2 < 10000:
print("Decode timestamp 2 [> 9999, integer]: ", end="")
self.ts2 = self._input_entier()
# if arg
if self.ts1 < 10000:
print("Fatal error, timestamp 1 = " + str(self.ts1) + ". It must be\
> 9999, integer]")
sys.exit(1)
if self.ts2 < 10000:
print("Fatal error, timestamp 2 = " + str(self.ts2) + ". It must be\
> 9999, integer]")
sys.exit(1)
# ts2 > ts1 => sens diff = +, sinon -
self.sens_diff = "+"
if self.ts1 > self.ts2:
self.ts1, self.ts2 = self.ts2, self.ts1
self.sens_diff = "-"
#recup ts1, ts2
self.ts1 = int(self.ts1 / 10000)
self.ts2 = int(self.ts2 / 10000)
self.dts = self.ts2 - self.ts1
self.secondes = self.dts % 60
self.minutes = int((self.dts / 60) % 60)
self.heures = int((self.dts / (60 * 60)) % 24)
self.jours = int(self.dts / (60 * 60 * 24))
if typ == "string":
self.retour = (self.sens_diff + str(self.jours) + " " +
str(self.heures) + ":" + str(self.minutes) + ":" +
str(self.secondes))
if typ == "list":
self.retour = list()
self.retour.append(self.sens_diff)
self.retour.append(self.jours)
self.retour.append(self.heures)
self.retour.append(self.minutes)
self.retour.append(self.secondes)
if typ == "dict" or typ == "dictionary":
self.retour = dict()
self.retour["sensdiff"] = self.sens_diff
self.retour["days"] = self.jours
self.retour["hours"] = self.heures
self.retour["minutes"] = self.minutes
self.retour["seconds"] = self.secondes
return self.retour
if __name__ == "__main__":
# at top, # from ahordat import AaaaHorodatage # to test module
h = AaaaHorodatageTools()
print(h.get_utc_server_offset())
print(h.get_utc_server_offset("string"))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment