Commit c76d68b7 authored by Robert Sebille's avatar Robert Sebille
Browse files

Conversion posix + maj doc

parent 17616b8a
......@@ -12,7 +12,9 @@ AaaaHorodatage
9: *new_offset out of range in utc2ts(ts, *new_offset).
-43200 >= *new_offset <= +50400 or '-12:00' >= *new_offset <= '+14:00'.
10: timestamp less than epoch in tz2utc(ts).
53: number of seconds to substract to big in __sub__(seconds=0).
Substraction < <ts_epoch>-01-01 epoch of the timestamp. Program halted.")
54: aaaa_ts must be an UTC timestamp in convert_utc2posix(aaaa_ts).
AaaaHorodatageTools
51 : offset UTC timestamp 1 does not match offset UTC timestamp 2 in diffTs
......
# AaaaHorodatage
This program is under GNU General Public License, version 3, see LICENCE text file.<br />
This program is under GNU General Public License, version 3, see LICENCE text file.
This program needs python 3 and modules aaaa.ahordat, aaaa.ahordattools.
## AaaaHorodatage format
......@@ -14,18 +15,32 @@ __Below, in the same directory as this README.md file__
### Code:
>>> from aaaa.ahordattools import AaaaHorodatageTools
>>> <new instance> = AaaaHorodatageTools()
or
>>> <new instance> = AaaaHorodatageTools(AaaaHorodatage)
>>> <your code>
Exemples:
- <new instance\> = _AaaaHorodatageTools()_ will done 2000Z+02:00 = 2000-01-01T00:00:00+02:00, embedded epoch = 2000, (Default).
- <new instance\> = _AaaaHorodatageTools("9782823001970Z+00:00")_ will done 9782823001970Z+00:00 = 2000-12-31T17:05:00+00:00, embedded epoch = 1970.
### Modifiers
An AaaaHordatage instance has two public attibutes:
_self.ts_ (its timestamp) and _self.utc_local_offset_ (its UTC offset in seconds).
Some methods, i call modifiers, can change this attributes; they will be documented with _update self.ts, self.utc_local_offset_
### Generate timestamps
encode(d="", utc=False) # possible inputs:
update self.ts
d="": keyboard input
d= list, tuple or dictionary.
utc=False: Local UTC offset encoding (see "Setting your timezone")
utc=True: UTC encoding
self.ts is updated
decode("typ", ts) typ = "string", "list", "dict" ou "dictionary"
ts = timestamp: I will use ts, self.ts will not be modified.
ts = timestamp: I will use ts.
ts = none or "": I will use self.ts.
NB: encode year > 9999 with an epoch = 1000 can causes long calculations.
......@@ -38,23 +53,42 @@ _utc_local_offset_ range: -43200 >= _utc_seconds_ <= +50400
You can use public methods to get or set it.
get_utc_local_offset(typ="") # typ="": default, seconds (integer); typ="string": "+/-HH:MM"
set_utc_local_offset(*offset) # work with encode() Default +02:00
-43200 >= *offset <= +50400 or '-12:00' >= *offset <= '+14:00'
get_utc_local_offset(typ="")
typ="": default, seconds (integer); typ="string": "+/-HH:MM"
set_utc_local_offset(*offset)
update self.utc_local_offset /!\ NOT self.ts /!\
work with encode(). Default +02:00
-43200 >= *offset <= +50400 or '-12:00' >= *offset <= '+14:00'
Play attention, with __set_utc_local_offset(*offset)__. It's update _self.utc_local_offset_ only
To update _self.ts_ with the new _self.utc_local_offset_, you must re-encode the date. Example:
h = AaaaHorodatageTools()
myDate = (2020, 2, 23, 45, 30, 40)
myTs = h.encode(myDate)
h.set_utc_local_offset(-14400) # update self.utc_local_offset to -04:00
myTs = h.encode(myDate) # update self.ts
To update _self.ts_ and _self.utc_local_offset_ in the same time, see _tz2utc(ts)_ and _utc2tz(ts, *new_offset)_
### Converting timezone
You can use methods to convert timestamp from utc to a specific timezone and from a specific timezone to utc.
tz2utc(ts) # self.ts is updated
ts = timestamp: I will use ts, self.ts is updated with ts.
ts = none or "": I will use self.ts. It will be updated.
utc2tz(ts, *new_offset) # -43200 >= *new_offset <= +50400 or
'-12:00' >= *new_offset <= '+14:00'. self.ts is updated
tz2utc(ts)
update self.ts, self.utc_local_offset
ts = timestamp: I will use ts.
ts = none or "": I will use self.ts.
utc2tz(ts, *new_offset)
update self.ts, self.utc_local_offset
ts = timestamp: I will use ts, self.ts is updated with ts.
ts = "": I will use self.ts. It will be updated.
-43200 >= *new_offset <= +50400 or
'-12:00' >= *new_offset <= '+14:00'.
### Converting offset
You can use this methods to convert offset from seconds to string and from string to seconds.
You can use this utility 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
......@@ -80,7 +114,10 @@ You can use this class methods to get or set it.
### Converting epoch
You can change and adapt the embedded epoch of a timestamp.
convert2epoch(self, ts, epoch) # Convert an embedded epoch to an other. 1000 <= epoch <= 9999. self.ts is updated.
convert2epoch(self, ts, epoch)
update self.ts
Convert an embedded epoch to an other.
1000 <= epoch <= 9999.
## Operations
Operations relate to timestamps of the same UTC offset and at the same epoch only.
......@@ -97,12 +134,27 @@ Operations relate to timestamps of the same UTC offset and at the same epoch onl
ts1 and ts2 = none: keyboard inputs
+ operator: new_ts = ts + nb_seconds
update new_ts.ts and new_ts.utc_local_offset
Add nb_seconds at timestamp to new_timestamp
Tips: use dhms2second(d=0, h=0, m=0, s=0) method to
convert days + hours + minutes + seconds in seconds
Tips: use dhms2second(d=0, h=0, m=0, s=0) utility method
to convert days + hours + minutes + seconds in seconds
- operator: new_ts = ts - nb_seconds
update new_ts.ts and new_ts.utc_local_offset
new_ts seconds must be >= 0 (epoch-01-01)
Add nb_seconds at timestamp to new_timestamp
Tips: use dhms2second(d=0, h=0, m=0, s=0) utility method
to convert days + hours + minutes + seconds in seconds
### Conversion to and from Posix
convert_aaaa_utc2posix(aaaa_ts)
Return a Posix timestamp from an Aaaa timestamp
aaaa_ts must be at UTC.
### To do
sum, operator +, operator -, ... are "to do"
convert_posix2aaaa_utc(posix_ts):
Return an Aaaa timestamp from a Posix timestamp
posix_ts will be considered at UTC time.
## Help:
import aaaa.ahordattools
......
......@@ -52,18 +52,20 @@ class AaaaHorodatage:
# année de référence (attribut de classe)
epoch = 2000
def __init__(self):
# self.utc_local_offset = -7200
# self.utc_local_offset = 55000
self.utc_local_offset = 7200 # Europe/Brussels summertime
def __init__(self, ts=""):
# private
# Liste nombre de jours par mois, moins décembre et année non bissextile
self._ljm = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
self._nb_sec_par_jour = 86400
# public, but to define with _ljm
self.ts = self.encode((self.get_epoch(), 1, 1, 0, 0, 0))
# public
if ts == "":
self.utc_local_offset = 7200 # Europe/Brussels summertime
# defined with _ljm and utc_local_offset
self.ts = self.encode((self.get_epoch(), 1, 1, 0, 0, 0))
else:
self.ts = ts
tmp = self.ts.split("Z")
self.set_utc_local_offset(self.offset_string2seconds(tmp[1]))
# protected #
# attributs
......@@ -333,6 +335,10 @@ class AaaaHorodatage:
- Number of seconds (-43200 >= *offset <= +50400)
- String (+/-HH:MM) ("-12:00" >= *offset <= "+14:00")
update self.utc_local_offset, but /!\ NOT self.ts /!\\
To update self.ts with the self.new utc_local_offset,
you must re-encode the date
"""
if len(offset) < 1:
print("Fatal error 4: missing *offset argument in ", end="")
......@@ -370,6 +376,8 @@ class AaaaHorodatage:
NB: encode year > 9999 with en epoch = 1000
can causes long calculations.
self.ts will be updated.
utc: True, UTC timezone (default) | False, local timezone
"""
......@@ -503,8 +511,9 @@ class AaaaHorodatage:
def tz2utc(self, ts=""):
"""Convert the timestamp ts to an UTC.
ts = timestamp: I will use ts, self.ts is updated with ts.
ts = none or "": I will use self.ts. It will be updated.
ts = timestamp: I will use ts.
ts = none or "": I will use self.ts.
self.ts, self.utc_local_offset will be updated.
"""
if ts == "":
......@@ -522,6 +531,8 @@ class AaaaHorodatage:
print("Fatal error 10: timestamp less than epoch in tz2utc(", ts,
").", sep="")
sys.exit(10)
self.utc_local_offset = 0
self.ts = str(ts_sec) + str(epoch) + "Z+00:00"
return self.ts
......@@ -533,9 +544,9 @@ class AaaaHorodatage:
- Number of seconds (-43200 >= *new_offset <= +50400)
- String (+/-HH:MM) ("-12:00" >= *new_offset <= "+14:00")
ts = timestamp: I will use ts, self.ts is updated with ts.
ts = "": I will use self.ts. It will be updated.
ts = timestamp: I will use ts.
ts = "": I will use self.ts.
self.ts, self.utc_local_offset will be updated.
"""
if len(new_offset) < 1:
......@@ -566,6 +577,7 @@ class AaaaHorodatage:
ts_epoch = int(ts_sec % 10000)
ts_sec = int(ts_sec / 10000)
ts_sec += offset
self.utc_local_offset = offset
self.ts = str(ts_sec) + str(ts_epoch) + "Z" +\
self._offset_seconds2string(offset)
......
......@@ -75,15 +75,21 @@ class AaaaHorodatageTools(AaaaHorodatage):
# def decode(self, typ="string", t=0):
# For dev end
def __init__(self):
AaaaHorodatage.__init__(self)
def __init__(self, ts=""):
AaaaHorodatage.__init__(self, ts)
# Special #
def __str__(self):
"""Return the string self.ts"""
return self.get_ts()
def __add__(self, seconds=0):
"""Add seconds to timestamps
"""Add seconds to self.ts and return a new timestamp
with the result. Overload of plus operator.
new_timestamp.ts and new_timestamp.utc_local_offset
will be updated. Not self.ts, neither
self.utc_local_offset
Tips: use dhms2second(d=0, h=0, m=0, s=0) method to
convert days + hours + minutes + seconds in seconds
......@@ -96,6 +102,37 @@ class AaaaHorodatageTools(AaaaHorodatage):
ts_seconds += seconds
aaaa = AaaaHorodatageTools()
aaaa.ts = str(ts_seconds) + str(ts_epoch) + "Z" + ts_offset
aaaa.set_utc_local_offset(self.offset_string2seconds(ts_offset))
return aaaa
def __sub__(self, seconds=0):
"""Subtract seconds from self.ts and return a new
timestamp with the result. Overload of minus operator.
new_timestamp.ts and new_timestamp.utc_local_offset
will be updated. Not self.ts, neither
self.utc_local_offset
Tips: use dhms2second(d=0, h=0, m=0, s=0) method to
convert days + hours + minutes + seconds in seconds
"""
tmp = self.ts.split("Z")
ts_offset = tmp[1]
tmp = int(tmp[0])
ts_epoch = int(tmp % 10000)
ts_seconds = int(tmp / 10000)
if ts_seconds >= seconds:
ts_seconds -= seconds
else:
print("Fatal error 53: number of seconds to substract ", end="")
print("to big in __sub__(seconds=0). Substract < ", end="")
print(ts_epoch, "-01-01 (epoch of the timestamp)\nProgram halted.",
sep="")
sys.exit(53)
aaaa = AaaaHorodatageTools()
aaaa.ts = str(ts_seconds) + str(ts_epoch) + "Z" + ts_offset
aaaa.set_utc_local_offset(self.offset_string2seconds(ts_offset))
return aaaa
# protected #
......@@ -211,6 +248,34 @@ class AaaaHorodatageTools(AaaaHorodatage):
"""Convert days + hours + minutes + seconds in seconds"""
return d * self._get_nb_sec_par_jour() + h * 3600 + m * 60 + s
def convert_aaaa_utc2posix(self, aaaa_ts):
"""Return a Posix timestamp from an Aaaa timestamp
aaaa_ts must be at UTC.
"""
save_local_ts = self.get_ts()
aaaa_ts = self.convert2epoch(aaaa_ts, 1970)
self.ts = save_local_ts
tmp = aaaa_ts.split("Z")
offset_ts_str = tmp[1]
offset_ts_int = self._offset_string2seconds(offset_ts_str)
if offset_ts_int != 0:
print("Fatal error 54: aaaa_ts must be an UTC timestamp in \
convert_utc2posix(self, aaaa_ts)\nprogram halted.")
ts_sec = int(tmp[0])
ts_sec = int(ts_sec / 10000)
return ts_sec
def convert_posix2aaaa_utc(self, posix_ts):
"""Return an Aaaa timestamp from a Posix timestamp
posix_ts will be considered at UTC time.
"""
return str(posix_ts) + "1970Z+00:00"
if __name__ == "__main__":
# at top, # from ahordat import AaaaHorodatage # to test module
......
......@@ -72,13 +72,18 @@ if __name__ == "__main__":
print("+-----------------------------+")
print("And now, let's see about the difference between 2 dates.")
print("I have encoded: ", k.decode("string", myDT))
print("You have encoded: ", k.decode("string", myKb))
print("You minus me: ", k.diffDays("string", myDT, myKb))
print("Me minus you: ", k.diffDays("string", myKb, myDT))
print("And now: ", k.decode(), " + 1T01:10:20")
print("I have encoded: ", k.decode("string", myDT))
print("You have encoded: ", k.decode("string", myKb))
myKb = k.utc2tz(myKb, "+02:00")
print("First, convert yours to +02:00 ", k.decode("string", myKb))
print("And now, you minus me: ", k.diffDays("string", myDT, myKb))
print("Me minus you: ", k.diffDays("string", myKb, myDT))
print("Addition: ", k.decode(), " + 1T01:10:20")
k2 = k + k.dhms2seconds(1, 1, 10, 20)
print(" = ", k2.decode())
print(" = ", k2.decode())
print("And substraction: ", k.decode(), " - 1T01:10:20")
k2 = k - k.dhms2seconds(1, 1, 10, 20)
print(" = ", k2.decode())
print("\nPlay attention to epoch ...")
rep = True
......@@ -119,4 +124,23 @@ if __name__ == "__main__":
" = ", k.decode("string", myDT))
print("epoch attribute = 2000 not changed: ", k.get_epoch())
rep = True
while rep:
print("+-----------------------------+")
rep = input("| Press Enter to continue ... |")
rep = False
print("+-----------------------------+")
print("Conversion Aaaa timestamps <> Posix timestamp")
print("Remember date test set above to tz UTC and")
print("epoch 1900: ", myDTutc, " = ",
k.decode("string", myDTutc))
myPosix = k.convert_aaaa_utc2posix(myDTutc)
print("Its posix timestamp is: ", str(myPosix))
myDTutc = k.convert_posix2aaaa_utc(myPosix)
k.encode(k.decode("list", myDTutc))
print("Reconvert to Aaaa timestamp ")
print("epoch 1970 ", myDTutc, " - ", k.decode())
print("You can verify on http://www.unixtime.fr/, choosing UTC time.")
print("\n+-------------+\n| Goodbye ... |\n+-------------+\n")
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