Datetime documented
This commit is contained in:
parent
fc656af47a
commit
c96ed2e491
1 changed files with 283 additions and 12 deletions
|
|
@ -6,7 +6,7 @@ from settings import config
|
|||
#from paramecio.citoplasma.sessions import get_session
|
||||
from os import environ
|
||||
|
||||
"""Simple hook for timedate functions from Arrow datetime module
|
||||
"""Simple hook for timedate functions from Arrow datetime module. Maybe in the future use native python datetime functions or other libraries. Is simply an abstraction for not depend of particular library.
|
||||
"""
|
||||
|
||||
sql_format_time='YYYYMMDDHHmmss'
|
||||
|
|
@ -253,6 +253,18 @@ def timestamp_to_datetime_local(timestamp, tz=''):
|
|||
|
||||
def format_datetime(format_time, timeform, func_utc_return):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in other str datetime (normally, same YYYYMMDDHHmmss format). Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
format_time (str): The strtime string used for format the datetime
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
func_utc_return (function): A function used for get the datetime.
|
||||
|
||||
Returns:
|
||||
If timestamp is False, return False, if timestamp is valid, return the datetime formatted
|
||||
|
||||
"""
|
||||
|
||||
timestamp=obtain_timestamp(timeform)
|
||||
|
||||
if timestamp:
|
||||
|
|
@ -269,50 +281,159 @@ def format_datetime(format_time, timeform, func_utc_return):
|
|||
|
||||
def local_to_gmt(timeform, sql_format_time=sql_format_time):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in other str datetime. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Expects that timeform was in time not gmt and convert to gmt
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
sql_format_time (str): by default, the format is YYYYMMDDHHmmss, you can put other formatted str formats for date, here.
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted
|
||||
|
||||
"""
|
||||
|
||||
return format_datetime(sql_format_time, timeform, substract_utc)
|
||||
|
||||
# time.localtime is useless, you need sum the time offset to the date
|
||||
|
||||
def gmt_to_local(timeform, sql_format_time=sql_format_time):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format in UTC and convert in other str datetime. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Expects that timeform was in time gmt and convert to localtime
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
sql_format_time (str): by default, the format is YYYYMMDDHHmmss, you can put other formatted str formats for date, here.
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted
|
||||
|
||||
"""
|
||||
|
||||
return format_datetime(sql_format_time, timeform, sum_utc)
|
||||
|
||||
def format_time(timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in HH:mm:ss UTC format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted in UTC
|
||||
|
||||
"""
|
||||
|
||||
return format_datetime(format_time_txt, timeform, sum_utc)
|
||||
|
||||
def format_date(timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in YYYY/MM/DD UTC format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted in UTC
|
||||
"""
|
||||
|
||||
return format_datetime(format_date_txt, timeform, sum_utc)
|
||||
|
||||
def format_fulldate(timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in YYYY/MM/DD HH:mm:ss UTC format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted in UTC
|
||||
"""
|
||||
|
||||
return format_datetime(format_date_txt+' '+format_time_txt, timeform, sum_utc)
|
||||
|
||||
def format_local_time(timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in HH:mm:ss format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted
|
||||
"""
|
||||
|
||||
return format_datetime(format_time_txt, timeform, no_utc)
|
||||
|
||||
def format_local_date(timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in YYYY/MM/DD format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted
|
||||
"""
|
||||
|
||||
return format_datetime(format_date_txt, timeform, no_utc)
|
||||
|
||||
def format_local_fulldate(timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in YYYY/MM/DD HH:mm:ss format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted
|
||||
"""
|
||||
|
||||
return format_datetime(format_date_txt+' '+format_time_txt, timeform, no_utc)
|
||||
|
||||
def format_strtime(strtime, timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in strtime string UTC format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted in UTC
|
||||
"""
|
||||
|
||||
return format_datetime(strtime, timeform, sum_utc)
|
||||
|
||||
def format_local_strtime(strtime, timeform):
|
||||
|
||||
"""Get a datetime in YYYYMMDDHHmmss format and convert in strtime string format. Is a primitive function for other high level datetime functions.
|
||||
|
||||
Args:
|
||||
timeform (str): datetime in YYYYMMDDHHmmss format to convert to new format
|
||||
|
||||
Returns:
|
||||
If timeform is False, return False, if timeform is valid, return the datetime formatted
|
||||
"""
|
||||
|
||||
return format_datetime(strtime, timeform, no_utc)
|
||||
|
||||
#Input is utc timestamp, return local arrow object
|
||||
|
||||
def sum_utc(timestamp, tz=''):
|
||||
|
||||
"""Get timestamp in UTC and convert in arrow date object with timezone datetime
|
||||
|
||||
Args:
|
||||
timestamp (int): The timestamp for convert in other timezone
|
||||
tz (str): Timezone of timestamp
|
||||
|
||||
Returns:
|
||||
Return arrow object with new timezone selected
|
||||
"""
|
||||
|
||||
#offset=time.altzone
|
||||
|
||||
#return time.localtime(timestamp-offset)
|
||||
|
|
@ -328,6 +449,16 @@ def sum_utc(timestamp, tz=''):
|
|||
|
||||
def substract_utc(timestamp, tz=''):
|
||||
|
||||
"""Get local timestamp and convert in arrow date object with UTC datetime
|
||||
|
||||
Args:
|
||||
timestamp (int): The timestamp for convert in UTC timezone
|
||||
tz (str): Timezone of timestamp
|
||||
|
||||
Returns:
|
||||
Return arrow object with UTC timezone selected
|
||||
"""
|
||||
|
||||
#offset=time.altzone
|
||||
|
||||
#return time.localtime(timestamp+offset)
|
||||
|
|
@ -347,13 +478,34 @@ def substract_utc(timestamp, tz=''):
|
|||
|
||||
def no_utc(timestamp):
|
||||
|
||||
"""Return an arrow object based in timestamp value
|
||||
|
||||
Args:
|
||||
timestamp (int): The timestamp for convert in UTC timezone
|
||||
|
||||
Returns:
|
||||
Return arrow object based in timestamp value
|
||||
|
||||
"""
|
||||
|
||||
return arrow.get(timestamp)
|
||||
|
||||
# def date_to_sql(date_sql):
|
||||
|
||||
|
||||
|
||||
class TimeClass:
|
||||
"""Simple abstraction of arrow class, in future i can change arrow class by others
|
||||
|
||||
Args:
|
||||
timestamp (int, str, optional): You can set the initial arrow object with timestamp date or YYYYMMDDHHmmss date format
|
||||
tz (str): Timezone
|
||||
|
||||
Attributes:
|
||||
utc (bool): If True, the default timezone is UTC, if False, timezone is system default
|
||||
format_time (str): The default datetime format, YYYYMMDDHHmmss
|
||||
format_time_txt (str): Time text format, usually HH:mm:ss
|
||||
format_date_txt (str): Date text format, usually YYYY/MM/DD
|
||||
format_date_full (str): Full DateTime text format, usually YYYY/MM/DD HH:mm:ss
|
||||
tz (str): Default timezone for arrow object
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, timestamp=None, tz=''):
|
||||
|
||||
|
|
@ -372,13 +524,7 @@ class TimeClass:
|
|||
self.tz=tz
|
||||
|
||||
if type(timestamp).__name__=='int':
|
||||
"""
|
||||
if self.utc:
|
||||
|
||||
self.datetime=utcnow()
|
||||
|
||||
else:
|
||||
"""
|
||||
self.datetime=timestamp_to_datetime(timestamp)
|
||||
|
||||
else:
|
||||
|
|
@ -395,72 +541,154 @@ class TimeClass:
|
|||
self.t=arrow.get(datetime(y, m, d, h, mi, s), self.tz)
|
||||
|
||||
def add_month(self, num_months):
|
||||
"""Method for add months to datetime
|
||||
|
||||
Args:
|
||||
num_months (int): Number of months to add
|
||||
|
||||
Returns:
|
||||
New added datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(months=+num_months)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def substract_month(self, num_months):
|
||||
"""Method for substract months to datetime
|
||||
|
||||
Args:
|
||||
num_months (int): Number of months to substract
|
||||
|
||||
Returns:
|
||||
New substracted datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(months=-num_months)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def add_day(self, num_days):
|
||||
"""Method for add days to datetime
|
||||
|
||||
Args:
|
||||
num_days (int): Number of days to add
|
||||
|
||||
Returns:
|
||||
New added datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(days=+num_days)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def substract_day(self, num_days):
|
||||
"""Method for substract days to datetime
|
||||
|
||||
Args:
|
||||
num_days (int): Number of days to substract
|
||||
|
||||
Returns:
|
||||
New substracted datetime
|
||||
|
||||
"""
|
||||
|
||||
m=self.t.shift(days=-num_days)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def add_year(self, num_years):
|
||||
"""Method for add years to datetime
|
||||
|
||||
Args:
|
||||
num_years (int): Number of years to add
|
||||
|
||||
Returns:
|
||||
New added datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(years=+num_years)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def substract_year(self, num_years):
|
||||
"""Method for substract years to datetime
|
||||
|
||||
Args:
|
||||
num_years (int): Number of years to substract
|
||||
|
||||
Returns:
|
||||
New substracted datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(years=-num_years)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def add_hour(self, num_hours):
|
||||
"""Method for add hours to datetime
|
||||
|
||||
Args:
|
||||
num_hours (int): Number of hours to add
|
||||
|
||||
Returns:
|
||||
New added datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(hours=+num_hours)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def substract_hour(self, num_hours):
|
||||
"""Method for substract hours to datetime
|
||||
|
||||
Args:
|
||||
num_hours (int): Number of hours to substract
|
||||
|
||||
Returns:
|
||||
New substracted datetime
|
||||
"""
|
||||
|
||||
m=self.t.shift(hours=-num_hours)
|
||||
|
||||
return m.format(self.format_time)
|
||||
|
||||
def format(self):
|
||||
"""Method for get datetime formatted using format_date_full attribute
|
||||
|
||||
Returns:
|
||||
Datetime formatted with format_date_full attribute
|
||||
"""
|
||||
|
||||
return self.t.format(self.format_date_full)
|
||||
|
||||
def local_to_utc(self):
|
||||
"""Method for convert datetime from actual timezone to UTC"""
|
||||
|
||||
self.t=self.t.to('utc')
|
||||
|
||||
# Only use
|
||||
|
||||
def utc_to_local(self):
|
||||
"""Method for convert datetime from actual timezone from UTC to actual timezone"""
|
||||
|
||||
self.t=self.t.to(self.tz)
|
||||
|
||||
def local_to_tz(self, tz):
|
||||
"""Method for convert actual timezone to other timezone"""
|
||||
|
||||
self.t=self.t.to(tz)
|
||||
|
||||
def now(self, utc=False):
|
||||
"""Method for get actual datetime.
|
||||
|
||||
Args:
|
||||
utc (bool): If True, then get actual datetime in UTC datetime, if False, get actual datetime in selected timezone in tz attribute
|
||||
|
||||
Returns:
|
||||
|
||||
Actual datetime formatted in YYYYMMDDHHmmss format.
|
||||
"""
|
||||
|
||||
if not utc:
|
||||
|
||||
|
|
@ -472,6 +700,16 @@ class TimeClass:
|
|||
|
||||
def today(self, utc=False):
|
||||
|
||||
"""Method for get today datetime. Get now datetime with 00:00:00 time.
|
||||
|
||||
Args:
|
||||
utc (bool): If True, then get actual datetime in UTC datetime, if False, get actual datetime in selected timezone in tz attribute
|
||||
|
||||
Returns:
|
||||
|
||||
Actual datetime formatted in YYYYMMDD000000 format.
|
||||
"""
|
||||
|
||||
if utc:
|
||||
|
||||
return arrow.utcnow()[:8]+'000000'
|
||||
|
|
@ -480,10 +718,31 @@ class TimeClass:
|
|||
|
||||
def timestamp_to_datetime(self, timestamp):
|
||||
|
||||
"""Method for convert a timestamp in YYYYMMDDHHmmss format.
|
||||
|
||||
Args:
|
||||
|
||||
timestamp (int): datetime in timestamp format.
|
||||
|
||||
Returns:
|
||||
|
||||
Datetime in YYYYMMDDHHmmss format.
|
||||
"""
|
||||
|
||||
return arrow.get(timestamp).format(sql_format_time)
|
||||
|
||||
def obtain_timestamp(self, timeform):
|
||||
|
||||
"""Method for get timestamp from a datetime in YYYYMMDDHHmmss format.
|
||||
|
||||
Args:
|
||||
timeform (str): Datetime in YYYYMMDDHHmmss format.
|
||||
|
||||
Returns:
|
||||
|
||||
Datetime in YYYYMMDDHHmmss format.If timeform is incorrect, return False
|
||||
"""
|
||||
|
||||
y, m, d, h, mi, s=format_timedata(timeform)
|
||||
|
||||
if checkdatetime(y, m, d, h, mi, s):
|
||||
|
|
@ -497,7 +756,19 @@ class TimeClass:
|
|||
|
||||
def format_strtime(self, strtime, timeform):
|
||||
|
||||
#timestamp=self.obtain_timestamp(timeform)
|
||||
"""Method for get datetime formatted in strtime format
|
||||
|
||||
Args:
|
||||
strtime (str): The string used for format the datetime
|
||||
timeform (str): Datetime in YYYYMMDDHHmmss format.
|
||||
|
||||
Returns:
|
||||
|
||||
Datetime in strtime format.If timeform is incorrect, return False
|
||||
|
||||
"""
|
||||
|
||||
|
||||
try:
|
||||
y, m, d, h, mi, s=format_timedata(timeform)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue