Fixes in datetime
This commit is contained in:
parent
3aaba5e7a1
commit
99903b8026
2 changed files with 195 additions and 14 deletions
|
|
@ -1,6 +1,9 @@
|
||||||
from datetime import date, datetime, time
|
import time
|
||||||
|
from datetime import date, datetime
|
||||||
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 paramecio.citoplasma.sessions import get_session
|
||||||
|
|
||||||
#t=datetime.utcnow()
|
#t=datetime.utcnow()
|
||||||
|
|
||||||
|
|
@ -25,22 +28,157 @@ from settings import config
|
||||||
|
|
||||||
# Next convert to
|
# Next convert to
|
||||||
|
|
||||||
locale='es_ES'
|
# Change
|
||||||
|
|
||||||
format_date="yyyy/LL/dd"
|
#os.environ['TZ'] = 'America/New_York'
|
||||||
|
|
||||||
format_time="HH:mm:ss"
|
#time.tzset()
|
||||||
|
|
||||||
timezone='UTC'
|
#>>> time.timezone
|
||||||
|
#-3600
|
||||||
|
|
||||||
|
#>>> int(time.time())
|
||||||
|
#1451356872
|
||||||
|
|
||||||
|
#>>> time.mktime((2015, 12, 29, 12, 25, 36, 0, 1, 0))
|
||||||
|
#1451384736.0
|
||||||
|
|
||||||
|
# strftime
|
||||||
|
|
||||||
|
#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)
|
||||||
|
|
||||||
|
sql_format_time='%Y%m%d%H%M%S'
|
||||||
|
|
||||||
|
format_date_txt="%Y/%m/%d"
|
||||||
|
|
||||||
|
format_time_txt="%H%M%S"
|
||||||
|
|
||||||
|
timezone='Europe/Madrid'
|
||||||
|
|
||||||
|
if hasattr(config, 'format_date'):
|
||||||
|
format_date_txt=config.format_date
|
||||||
|
|
||||||
|
if hasattr(config, 'format_time'):
|
||||||
|
format_time_txt=config.format_time
|
||||||
|
|
||||||
|
if hasattr(config, 'timezone'):
|
||||||
|
timezone=config.timezone
|
||||||
|
|
||||||
|
@hook('before_request')
|
||||||
|
def set_timezone():
|
||||||
|
|
||||||
|
s=get_session()
|
||||||
|
|
||||||
|
timezone_local=timezone
|
||||||
|
|
||||||
|
if s!=None:
|
||||||
|
|
||||||
|
timezone_local=s.get('timezone', timezone)
|
||||||
|
|
||||||
|
if os.environ['TZ']!=timezone_local:
|
||||||
|
os.environ['TZ']=timezone_local
|
||||||
|
time.tzset()
|
||||||
|
|
||||||
|
#request.environ['TIMEZONE'] = request.environ['PATH_INFO'].rstrip('/')
|
||||||
|
|
||||||
|
def format_timedata(time):
|
||||||
|
|
||||||
|
year=int(time[:4])
|
||||||
|
|
||||||
|
month=int(time[4:6])
|
||||||
|
|
||||||
|
day=int(time[6:8])
|
||||||
|
|
||||||
|
hour=int(time[8:10])
|
||||||
|
|
||||||
|
minute=int(time[10:12])
|
||||||
|
|
||||||
|
second=int(time[12:14])
|
||||||
|
|
||||||
|
ampm=''
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
ampm=int(time[14:16])
|
||||||
|
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if ampm=='PM' or ampm=='pm':
|
||||||
|
|
||||||
|
if hour>0:
|
||||||
|
hour+=12
|
||||||
|
|
||||||
|
return (year, month, day, hour, minute, second)
|
||||||
|
|
||||||
|
def checkdatetime(y, m, d, h, mi, s):
|
||||||
|
|
||||||
|
try:
|
||||||
|
test=datetime.strptime(str(y)+'-'+str(m)+'-'+str(d)+' '+str(h)+'-'+str(mi)+'-'+str(s), '%Y-%m-%d %H-%M-%S')
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def obtain_timestamp(time):
|
||||||
|
|
||||||
|
y, m, d, h, mi, s=format_timedata(time)
|
||||||
|
|
||||||
|
if checkdatetime(m, d, y, h, mi, s):
|
||||||
|
|
||||||
|
return int(time.mktime((y, m, d, h, mi, s, 0, 1, 0)))
|
||||||
|
#return mktime($h, $mi, $s, $m, $d, $y);
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def format_datetime(format, timestamp, func_utc_return):
|
||||||
|
|
||||||
|
timestamp=obtain_timestamp(timestamp)
|
||||||
|
|
||||||
|
if timestamp:
|
||||||
|
|
||||||
|
offset=time.timezone
|
||||||
|
|
||||||
|
timestamp=func_utc_return(timestamp, offset)
|
||||||
|
|
||||||
|
# Return utc
|
||||||
|
|
||||||
|
return time.strptime(format, timestamp)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def local_to_gmt(time):
|
||||||
|
|
||||||
|
return format_datetime(sql_format_time, time, sum_utc)
|
||||||
|
|
||||||
|
def format_time(time):
|
||||||
|
|
||||||
|
return format_datetime(format_time_txt, time, substract_utc)
|
||||||
|
|
||||||
|
def format_date(time):
|
||||||
|
|
||||||
|
return format_datetime(format_date_txt, time, substract_utc)
|
||||||
|
|
||||||
|
def sum_utc(timestamp, offset):
|
||||||
|
|
||||||
|
return timestamp+offset
|
||||||
|
|
||||||
|
def substract_utc(timestamp, offset):
|
||||||
|
|
||||||
|
return timestamp-offset
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
if hasattr(config, 'timezone'):
|
if hasattr(config, 'timezone'):
|
||||||
timezone=config.timezone
|
timezone=config.timezone
|
||||||
|
|
||||||
#if hasattr
|
|
||||||
|
|
||||||
tzutc=get_timezone('UTC')
|
tzutc=get_timezone('UTC')
|
||||||
|
|
||||||
tz = get_timezone(timezone)
|
tz=get_timezone(timezone)
|
||||||
|
|
||||||
# In utc
|
# In utc
|
||||||
|
|
||||||
|
|
@ -50,9 +188,20 @@ def timenow():
|
||||||
|
|
||||||
return format_datetime(t, "yyyyLLddHHmmss", tzutc)
|
return format_datetime(t, "yyyyLLddHHmmss", tzutc)
|
||||||
|
|
||||||
|
def timegmt(time, tzc=None):
|
||||||
|
|
||||||
|
if tzc==None:
|
||||||
|
tzc=tz
|
||||||
|
|
||||||
|
year, month, day, hour, minute, second=obtain_fields_time(time)
|
||||||
|
|
||||||
|
t=datetime(year, month, day, hour, minute, second)
|
||||||
|
|
||||||
|
return format_datetime(t, "yyyyLLddHHmmss", tzutc)
|
||||||
|
|
||||||
#In utc
|
#In utc
|
||||||
|
|
||||||
def normalize_time(year, month, day, hour, minute, second):
|
def normalize_time(year, month, day, hour, minute, second, tz=None):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
|
@ -106,9 +255,27 @@ def format_tzdate(time, tzc=None):
|
||||||
|
|
||||||
return format_datetime(t, format_date, tzinfo=tzc)
|
return format_datetime(t, format_date, tzinfo=tzc)
|
||||||
|
|
||||||
|
def local_to_utc(date, tzc=None):
|
||||||
|
|
||||||
|
if tzc==None:
|
||||||
|
tzc=tz
|
||||||
|
|
||||||
|
year, month, day, hour, minute, second=obtain_fields_time(date)
|
||||||
|
|
||||||
|
t=datetime(year, month, day, hour, minute, second, tzinfo=get_timezone('Europe/Madrid'))
|
||||||
|
print(t)
|
||||||
|
#timestamp=int(time.mktime((year, month, day, hour, minute, second, 0, 1, 0)))
|
||||||
|
|
||||||
|
#timezone_sum=time.timezone
|
||||||
|
|
||||||
|
#timestamp-=timezone_sum
|
||||||
|
|
||||||
|
print(format_datetime(t, "yyyyLLddHHmmss", tzinfo=tzutc))
|
||||||
|
|
||||||
# return timenow()
|
# return timenow()
|
||||||
def obtain_timezone(timezone):
|
def obtain_timezone(timezone):
|
||||||
|
|
||||||
return get_timezone(timezone)
|
return get_timezone(timezone)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
@ -6,10 +6,25 @@ class TestFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
def test_timenow(self):
|
def test_timenow(self):
|
||||||
|
|
||||||
|
time='20121023401223'
|
||||||
|
|
||||||
|
time_set=datetime.format_timedata(time)
|
||||||
|
|
||||||
|
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_set=datetime.format_timedata(time)
|
||||||
|
|
||||||
|
self.assertTrue(datetime.checkdatetime(time_set[0], time_set[1], time_set[2], time_set[3], time_set[4], time_set[5]))
|
||||||
|
|
||||||
|
"""
|
||||||
|
tz=datetime.obtain_timezone('Europe/Madrid')
|
||||||
|
|
||||||
time=datetime.normalize_time(2012, 12, 21, 23, 24, 21)
|
time=datetime.normalize_time(2012, 12, 21, 23, 24, 21)
|
||||||
|
|
||||||
self.assertEqual(time, '20121221232421')
|
self.assertEqual(time, '20121221232421')
|
||||||
|
|
||||||
value=datetime.format_tztime(time)
|
value=datetime.format_tztime(time)
|
||||||
|
|
||||||
self.assertEqual(value, '23:24:21')
|
self.assertEqual(value, '23:24:21')
|
||||||
|
|
@ -18,8 +33,6 @@ class TestFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(value, '2012/12/21')
|
self.assertEqual(value, '2012/12/21')
|
||||||
|
|
||||||
tz=datetime.obtain_timezone('Europe/Madrid')
|
|
||||||
|
|
||||||
value=datetime.format_tzdate(time, tz)
|
value=datetime.format_tzdate(time, tz)
|
||||||
|
|
||||||
self.assertEqual(value, '2012/12/22')
|
self.assertEqual(value, '2012/12/22')
|
||||||
|
|
@ -28,4 +41,5 @@ class TestFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(value, '00:24:21')
|
self.assertEqual(value, '00:24:21')
|
||||||
|
|
||||||
|
print(datetime.local_to_utc('20121221232421', tz))
|
||||||
|
"""
|
||||||
Loading…
Add table
Add a link
Reference in a new issue