Commit 70bfe89e authored by Robert Sebille's avatar Robert Sebille
Browse files

utc2tz done

parent 487b9894
List errors
AaaaHorodatage
2 : utc_seconds not in range -43200 >= utc_seconds <= +50400 in _convert_offset_seconds2string
2 : offset_seconds not in range -43200 >= utc_seconds <= +50400 in _convert_offset_seconds2string(offset_seconds)
3 : range error for year, month, day, hour, minute or second input in encode
4 : missing *offset argument in set_utc_local_offset
5 : *offset must be a string or an integer in set_utc_local_offset.
6 : utc_local_offset out of rang in set_utc_local_offset.
4 : missing *offset argument in set_utc_local_offset(*offset).
5 : *offset must be a string or an integer in set_utc_local_offset(*offset).
6 : *offset out of range in set_utc_local_offset(*offset).
-43200 >= *offset <= +50400 or '-12:00' >= *offset <= '+14:00'.
7 : missing *new_offset argument in utc2ts(ts, *new_offset).
8 : *new_offset must be a string or an integer in utc2ts(ts, *new_offset).
9 : *new_offset out of range in utc2ts(ts, *new_offset).
-43200 >= *new_offset <= +50400 or '-12:00' >= *new_offset <= '+14:00'.
AaaaHorodatageTools
51 : offset UTC timestamp 1 does not match offset UTC timestamp 2 in diffTs
......
......@@ -15,18 +15,29 @@ __Below, in the same directory as this README file__
## Timezones
### Setting your timezone
Your local timezone is stored in _utc_local_offset_ attribute
Your local timezone is stored in _utc_local_offset_ attribute
Default is +7200 seconds, that is to say +02:00; Europe/Brussels summertime
_utc_local_offset_ range: -43200 >= _utc_seconds_ <= +50400
You can use _get_utc_local_offset_ or _set_utc_local_offset_ public methods to get or set it.
You can use public methods to get or set it.
get_utc_local_offset()
set_utc_local_offset(*offset)
-43200 >= *offset <= +50400 or '-12:00' >= *offset <= '+14:00'
### Converting timezone
You can use _ts2utc(ts)_ and _utc2ts(ts)_ methods to convert timestamps from utc to a specific and from a specific timezone or utc. (_utc2ts(ts)_ is currently under construction).
You can use methods to convert timestamp from utc to a specific timezone and from a specific timezone to utc.
tz2utc(ts)
utc2tz(ts, *new_offset)
# -43200 >= *new_offset <= +50400 or '-12:00' >= *new_offset <= '+14:00'
### Converting offset
You can use offset_seconds2string(offset_seconds)_ and utc_string2seconds(offset_string)_ methods to convert offsets from seconds to string and from string to seconds.
You can use this methods to convert offset from seconds to string and from string to seconds.
offset_seconds2string(offset_seconds) # -43200 >= *new_offset <= +50400
offset_string2seconds(offset_string) # no control
### Timezones sites
Some timezones sites can help you to determine tour timezone.
......@@ -38,11 +49,18 @@ Some timezones sites can help you to determine tour timezone.
the reference epoch of the AaaaHorodatage class stored in _epoch_ class attribute
Default is 1900. Range: 0 <= _epoch_ <= 9999.
You can use _get_epoch_ or _set_epoch_ class methods to get or set it.
You can use this class methods to get or set it.
get_epoch()
set_epoch(num) # 0 <= epoch <= 9999
Please, be consistent with yourself! e.g., you must encode and decode the same timestamp with the same epoch; or the results will be incoherent. It's why this method is a class method.
Remember this: The only way to know what is the reference epoch of a time stamp, is to remember it.
## Opérations
Encode, decode, difference, ... see Help below
_NB: sum, operator +, operator -, ... are "to do"
## Help:
import aaaa.ahordattools
help(aaaa.ahordattools)
......
......@@ -70,8 +70,9 @@ class AaaaHorodatage:
# méthodes
def _offset_seconds2string(self, offset_seconds=0):
if offset_seconds < -43200 or offset_seconds > +50400:
print("Fatal error: utc_seconds not in range -43200 >= utc_seconds\
<= +50400 in _offset_seconds2string")
print("Fatal error 2: offset_seconds not in range -43200 ", end="")
print(">= offset_seconds <= +50400 in ", end="")
print("_offset_seconds2string(offset_seconds)")
sys.exit(2)
chaineoffset = "+"
if offset_seconds < 0:
......@@ -150,7 +151,7 @@ class AaaaHorodatage:
# On ajoute l'offset UTC (local ou Z) sous forme de chaine
self.ts = str(self.ts) + "Z" + self.get_utc_local_offset("string")
if self.utc:
self.ts = self.ts2utc(self.ts)
self.ts = self.tz2utc(self.ts)
# print (annee, " bissextile = ", self.bis)
# print (self.ts, " est l'horodatage pour le ", self.jour, "/", \
......@@ -308,19 +309,19 @@ class AaaaHorodatage:
"""
if len(offset) < 1:
print("Fatal error: missing *offset argument in ", end="")
print("set_utc_local_offset.")
print("Fatal error 4: missing *offset argument in ", end="")
print("set_utc_local_offset(*offset).")
sys.exit(4)
if not isinstance(offset[0], int) and not isinstance(offset[0], str):
print("Fatal error: *offset must be a string ", end="")
print("or an integer in set_utc_local_offset.")
print("Fatal error 5: *offset must be a string ", end="")
print("or an integer in set_utc_local_offset(*offset).")
sys.exit(5)
offset_candidate = offset[0]
if isinstance(offset[0], str):
offset_candidate = self._offset_string2seconds(offset[0])
if offset_candidate < -43200 or offset_candidate > +50400:
print("Fatal error: utc_local_offset out of rang in ", end="")
print("set_utc_local_offset.")
print("Fatal error 6: *offset out of rang in ", end="")
print("set_utc_local_offset(*offset).")
print(" -43200 >= *offset <= +50400")
print("or '-12:00' >= *offset <= '+14:00'.")
sys.exit(6)
......@@ -374,32 +375,32 @@ class AaaaHorodatage:
if isinstance(d, list) or isinstance(d, tuple) or isinstance(d, dict):
self.erreur = ""
if self.annee < self.get_epoch() or self.annee > 9999:
self.erreur = "Fatal error, year = " + str(self.annee) + \
self.erreur = "Fatal error 3, year = " + str(self.annee) + \
". It must be [" + \
str(self.get_epoch()) +\
"-9999, integer] in encode."
if self.mois < 1 or self.mois > 12:
self.erreur = "Fatal error, month = " + str(self.mois) + \
self.erreur = "Fatal error 3, month = " + str(self.mois) + \
". It must be [1-12, integer] in encode."
self.nb_jours = self._get_ljm(self.mois - 1)
if self.mois == 2:
self.nb_jours += self._bissextile(self.annee)
if self.jour < 1 or self.jour > self.nb_jours:
self.erreur = "Fatal error, day = " + str(self.jour) + \
self.erreur = "Fatal error 3, day = " + str(self.jour) + \
". For " + str(self.mois) + "/" + \
str(self.annee) + \
", day must be [1-" + \
str(self.nb_jours) + ", integer] in encode."
if self.heures < 0 or self.heures > 23:
self.erreur = "Fatal error, hour = " + str(self.heures) + \
self.erreur = "Fatal error 3, hour = " + str(self.heures) + \
". It must be between [0-23, integer] in encode."
if self.minutes < 0 or self.minutes > 59:
self.erreur = "Fatal error, minutes = " + str(self.minutes) + \
". It must be between [0-59, integer] in encode."
self.erreur = "Fatal error 3, minutes = " + str(self.minutes)\
+ ". It must be between [0-59, integer] in encode."
if self.secondes < 0 or self.secondes > 59:
self.erreur = "Fatal error, seconds = " + str(self.secondes) + \
". It must be between [0-59, integer] in encode."
self.erreur = "Fatal error 3, seconds = " + str(self.secondes)\
+ ". It must be between [0-59, integer] in encode."
if self.erreur != "":
print(self.erreur)
......@@ -469,27 +470,79 @@ class AaaaHorodatage:
utc_string: timezone offset in the form +/-HH:MM"""
return self._offset_string2seconds(offset_string)
def ts2utc(self, ts):
"""Convert a local Aaaa timestamp to an UTC."""
def tz2utc(self, ts):
"""Convert a timestamp to an UTC."""
ts_list = self.decode("list", ts)
# ts_tmp_list[3] = heure, ts_tmp_list[4] = minutes
tmp = ts.split("Z")
ts_sec = int(tmp[0])
annee = int(ts_sec % 10000)
ts_sec = int(ts_sec / 10000)
utc_ts = self._offset_string2seconds(tmp[1])
offset_ts = self._offset_string2seconds(tmp[1])
# test pour 31/12 ou 1/1
ts_secondes_extremes = (ts_list[3] * 3600) + (ts_list[3] * 60)
if utc_ts < 0 and (ts_secondes_extremes + int(math.fabs(utc_ts))) >\
86400:
annee += 1
if utc_ts > 0 and (ts_secondes_extremes - int(math.fabs(utc_ts))) < 0:
annee -= 1
ts_sec -= utc_ts
if ts_list[1] == 12 and ts_list[2] == 31:
if offset_ts < 0 and (ts_secondes_extremes +
int(math.fabs(offset_ts))) > 86400:
annee += 1
if ts_list[1] == 1 and ts_list[2] == 1:
if offset_ts > 0 and (ts_secondes_extremes -
int(math.fabs(offset_ts))) < 0:
annee -= 1
ts_sec -= offset_ts
return str(ts_sec) + str(annee) + "Z+00:00"
def utc2tz(self, ts, *new_offset):
"""Convert a timestamp UTC to an other timezone.
Avalaible inputs:
- Number of seconds (-43200 >= *new_offset <= +50400)
- String (+/-HH:MM) ("-12:00" >= *new_offset <= "+14:00")
"""
if len(new_offset) < 1:
print("Fatal error 7: missing *new_offset argument in ", end="")
print("utc2tz(*new_offset).")
sys.exit(7)
if not isinstance(new_offset[0], int) and \
not isinstance(new_offset[0], str):
print("Fatal error 8: *new_offset must be a string ", end="")
print("or an integer in utc2tz(*new_offset).")
sys.exit(8)
offset = new_offset[0]
if isinstance(new_offset[0], str):
offset = self._offset_string2seconds(offset[0])
if offset < -43200 or offset > +50400:
print("Fatal error 9: *new_offset out of rang in ", end="")
print("utc2tz(*new_offset).")
print(" -43200 >= *new_offset <= +50400")
print("or '-12:00' >= *new_offset <= '+14:00'.")
sys.exit(9)
ts_list = self.decode("list", ts)
# ts_tmp_list[3] = heure, ts_tmp_list[4] = minutes
tmp = ts.split("Z")
ts_sec = int(tmp[0])
annee = int(ts_sec % 10000)
ts_sec = int(ts_sec / 10000)
# test pour 31/12 ou 1/1
ts_secondes_extremes = (ts_list[3] * 3600) + (ts_list[3] * 60)
if ts_list[1] == 12 and ts_list[2] == 31:
if offset > 0 and (ts_secondes_extremes +
int(math.fabs(offset))) > 86400:
annee += 1
if ts_list[1] == 1 and ts_list[2] == 1:
if offset < 0 and (ts_secondes_extremes -
int(math.fabs(offset))) < 0:
annee -= 1
ts_sec += offset
return str(ts_sec) + str(annee) + "Z" +\
self._offset_seconds2string(offset)
if __name__ == "__main__":
# Pour tester le module ...
s = 12
......@@ -498,7 +551,7 @@ if __name__ == "__main__":
A = 2000
M = 2
J = 29
epo = 1800
epo = 1900
print("(NB: If you enter " + str(J) + "/" + str(M) + "/" + str(A) + " " +
str(h) + ":" + str(m) + ":" + str(s) +
......@@ -509,7 +562,15 @@ if __name__ == "__main__":
print(AaaaHorodatage.get_epoch())
k = AaaaHorodatage()
k.set_utc_local_offset("+02:00")
date_test = [2000, 12, 31, 21, 5, 0]
k.set_utc_local_offset("+00:00")
myK = k.encode(date_test)
print("test1 ", k.decode("string", myK))
k.set_utc_local_offset("+04:00")
print("= ", k.utc2tz(myK, k.get_utc_local_offset()))
print("test2 ", k.decode("string", k.utc2tz(myK, k.get_utc_local_offset())))
print("test3 ", k.decode("string", k.tz2utc(myK)))
print(k.get_utc_local_offset())
print(k.get_utc_local_offset("string"))
print("\tUTC offset (string): " + k.get_utc_local_offset("string"))
......
......@@ -115,12 +115,12 @@ class AaaaHorodatageTools(AaaaHorodatage):
ts1_split = self.ts1.split("Z")
ts2_split = self.ts2.split("Z")
if ts1_split[1] != ts2_split[1]:
print("Fatal error: offset UTC1" + ts1_split[1] +
print("Fatal error 51: offset UTC1" + ts1_split[1] +
" does not match offset UTC2" + ts2_split[1] +
"\nProgram halted.")
print("Consider to standardize the 2 timestamps to Z (+00:00)")
print(" using <instance>.ts2utc(timestamp1)")
print(" using <instance>.ts2utc(timestamp2)")
print(" using <instance>.tz2utc(timestamp1)")
print(" using <instance>.tz2utc(timestamp2)")
sys.exit(51)
# ts2 > ts1 => sens diff = +, sinon -
self.ts1 = int(ts1_split[0])
......
......@@ -75,7 +75,7 @@ list or dict.\nFor exemples with your timestamp above:")
myKbTestTrue))
print("\nI can convert a local ts timestamp in an UTC timestamp too:")
myTsTestTrue = kb.ts2utc(myKbtest)
myTsTestTrue = kb.tz2utc(myKbtest)
print("\tI convert local ts " + myKbtest + " to UTC " + myTsTestTrue)
print("\tDecoding this UTC timestamp: ")
print("\tFrom ", kb.decode("string", myKbtest), " to ",
......
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