Now datetime.py use arrow how base for the datetime functions

This commit is contained in:
Antonio de la Rosa 2016-08-08 02:02:21 +02:00
parent bf46bc5087
commit 8f4e0440ed
3 changed files with 77 additions and 24 deletions

View file

@ -1,5 +1,6 @@
import time import time
from datetime import date, datetime from datetime import date, datetime, tzinfo
import arrow
# from babel.dates import format_date, format_datetime, format_time, get_timezone, UTC # from babel.dates import format_date, format_datetime, format_time, get_timezone, UTC
from settings import config from settings import config
from bottle import hook from bottle import hook
@ -49,11 +50,11 @@ from os import environ
#time.gmtime() #time.gmtime()
#time.struct_time(tm_year=2015, tm_mon=12, tm_mday=30, tm_hour=2, tm_min=6, tm_sec=56, tm_wday=2, tm_yday=364, tm_isdst=0) #time.struct_time(tm_year=2015, tm_mon=12, tm_mday=30, tm_hour=2, tm_min=6, tm_sec=56, tm_wday=2, tm_yday=364, tm_isdst=0)
sql_format_time='%Y%m%d%H%M%S' sql_format_time='YYYYMMDDHHmmss'
format_date_txt="%Y/%m/%d" format_date_txt="YYYY/MM/DD"
format_time_txt="%H:%M:%S" format_time_txt="HH:mm:ss"
timezone='Europe/Madrid' timezone='Europe/Madrid'
@ -134,7 +135,8 @@ def format_timedata(time):
def checkdatetime(y, m, d, h, mi, s): def checkdatetime(y, m, d, h, mi, s):
try: try:
test=datetime.strptime(str(y)+'-'+str(m)+'-'+str(d)+' '+str(h)+'-'+str(mi)+'-'+str(s), '%Y-%m-%d %H-%M-%S') #test=datetime.strptime(str(y)+'-'+str(m)+'-'+str(d)+' '+str(h)+'-'+str(mi)+'-'+str(s), '%Y-%m-%d %H-%M-%S')
test=arrow.arrow.Arrow(y, m, d, h, mi, s)
return True return True
except: except:
return False return False
@ -142,7 +144,7 @@ def checkdatetime(y, m, d, h, mi, s):
# Get the localtime # Get the localtime
def now(utc=False): def now(utc=False):
"""
actual=datetime.today() actual=datetime.today()
# Get localtime # Get localtime
@ -155,6 +157,14 @@ def now(utc=False):
final_date=local_to_gmt(final_date) final_date=local_to_gmt(final_date)
return final_date return final_date
"""
if not utc:
actual=arrow.now().format(sql_format_time)
else:
actual=arrow.utcnow().format(sql_format_time)
# Get actual timestamp # Get actual timestamp
@ -164,13 +174,20 @@ def obtain_timestamp(timeform, local=False):
if checkdatetime(y, m, d, h, mi, s): if checkdatetime(y, m, d, h, mi, s):
timestamp=int(time.mktime((y, m, d, h, mi, s, 0, 0, -1))) #timestamp=int(time.mktime((y, m, d, h, mi, s, 0, 0, -1)))
if local: if local:
offset=time.altzone #offset=time.altzone
return timestamp-offset #return timestamp-offset
t=arrow.arrow.Arrow(y, m, d, h, mi, s).to(environ['TZ'])
timestamp=t.timestamp
else:
timestamp=arrow.arrow.Arrow(y, m, d, h, mi, s).timestamp
return timestamp return timestamp
@ -182,16 +199,22 @@ def obtain_timestamp(timeform, local=False):
def timestamp_to_datetime(timestamp): def timestamp_to_datetime(timestamp):
time_set=substract_utc(timestamp) #time_set=substract_utc(timestamp)
return time.strftime(sql_format_time, time_set) #return time.strftime(sql_format_time, time_set)
return arrow.get(timestamp).format(sql_format_time)
def timestamp_to_datetime_local(timestamp): def timestamp_to_datetime_local(timestamp):
time_set=time.localtime(timestamp) #time_set=time.localtime(timestamp)
return time.strftime(sql_format_time, time_set) #return time.strftime(sql_format_time, time_set)
t=arrow.get(timestamp)
return t.to(environ['TZ']).format(sql_format_time)
def format_datetime(format_time, timeform, func_utc_return): def format_datetime(format_time, timeform, func_utc_return):
@ -206,9 +229,13 @@ def format_datetime(format_time, timeform, func_utc_return):
# Return utc # Return utc
time_set=func_utc_return(timestamp) #time_set=func_utc_return(timestamp)
return time.strftime(format_time, time_set) #return time.strftime(format_time, time_set)
t=func_utc_return(timestamp)
return t.format(format_time)
else: else:
@ -218,7 +245,7 @@ def format_datetime(format_time, timeform, func_utc_return):
def local_to_gmt(timeform): def local_to_gmt(timeform):
return format_datetime(sql_format_time, timeform, time.gmtime) return format_datetime(sql_format_time, timeform, substract_utc)
# time.localtime is useless, you need sum the time offset to the date # time.localtime is useless, you need sum the time offset to the date
@ -238,17 +265,35 @@ def format_fulldate(timeform):
return format_datetime(format_date_txt+' '+format_time_txt, timeform, sum_utc) return format_datetime(format_date_txt+' '+format_time_txt, timeform, sum_utc)
#Input is utc timestamp, return local arrow object
def sum_utc(timestamp): def sum_utc(timestamp):
offset=time.altzone #offset=time.altzone
return time.localtime(timestamp-offset) #return time.localtime(timestamp-offset)
t=arrow.get(timestamp)
return t.to(environ['TZ'])
#Input is local timestamp, return utc arrow object
def substract_utc(timestamp): def substract_utc(timestamp):
offset=time.altzone #offset=time.altzone
return time.localtime(timestamp+offset) #return time.localtime(timestamp+offset)
#t=arrow.get(timestamp).to('UTC')
timeform=timestamp_to_datetime(timestamp)
y, m, d, h, mi, s=format_timedata(timeform)
t=arrow.get(datetime(y, m, d, h, mi, s), environ['TZ']).to('UTC')
return t
""" """

View file

@ -19,7 +19,7 @@ setup(name='paramecio',
url='http://paramecioproject.com/', url='http://paramecioproject.com/',
packages=['paramecio'], packages=['paramecio'],
include_package_data=True, include_package_data=True,
install_requires=['bottle', 'mako', 'passlib', 'bcrypt', 'mysqlclient', 'sqlalchemy', 'Pillow', 'beaker>=1.8.0', 'itsdangerous', 'colorama','cherrypy'], install_requires=['bottle', 'mako', 'passlib', 'bcrypt', 'mysqlclient', 'sqlalchemy', 'Pillow', 'beaker>=1.8.0', 'itsdangerous', 'colorama','cherrypy', 'arrow'],
entry_points={'console_scripts': [ entry_points={'console_scripts': [
'paramecio = paramecio.console:start', 'paramecio = paramecio.console:start',
]}, ]},

View file

@ -12,7 +12,7 @@ class TestFieldMethods(unittest.TestCase):
self.assertFalse(datetime.checkdatetime(time_set[0], time_set[1], time_set[2], time_set[3], time_set[4], time_set[5])) self.assertFalse(datetime.checkdatetime(time_set[0], time_set[1], time_set[2], time_set[3], time_set[4], time_set[5]))
time='20121026231248' time='20121126231248'
time_set=datetime.format_timedata(time) time_set=datetime.format_timedata(time)
@ -26,9 +26,11 @@ class TestFieldMethods(unittest.TestCase):
datetime.set_timezone() datetime.set_timezone()
# Check conversions to gmt time
gmtstamp=datetime.local_to_gmt(time) gmtstamp=datetime.local_to_gmt(time)
self.assertEqual(gmtstamp, '20121026221248') self.assertEqual(gmtstamp, '20121126221248')
time_from_utc=datetime.format_time(time) time_from_utc=datetime.format_time(time)
@ -36,7 +38,13 @@ class TestFieldMethods(unittest.TestCase):
date_from_utc=datetime.format_date(time) date_from_utc=datetime.format_date(time)
self.assertEqual(date_from_utc, '2012/10/27') self.assertEqual(date_from_utc, '2012/11/27')
time_summer='20120826231248'
gmtstamp=datetime.local_to_gmt(time_summer)
self.assertEqual(gmtstamp, '20120826211248')
#today=datetime.now() #today=datetime.now()