connect existing tables with legacy date format

45 views
Skip to first unread message

yell...@gmail.com

unread,
Mar 3, 2016, 2:23:51 AM3/3/16
to web2py-users
Hi,

db = DAL(..., migrate_enable=False)

db.define_table('table', Field('fld01'), Field('fld02'),'integer',length=7)

db.fld02 actually is a date field with format CYYMMDD.

I would like to send this field to view and display as normal date format so that it will work with AJAX calendar. 

How could I do that? Many thanks.

Dave S

unread,
Mar 3, 2016, 3:36:08 AM3/3/16
to web2py-users


On Wednesday, March 2, 2016 at 11:23:51 PM UTC-8, yell...@gmail.com wrote:
Hi,

db = DAL(..., migrate_enable=False)

db.define_table('table', Field('fld01'), Field('fld02'),'integer',length=7)

db.fld02 actually is a date field with format CYYMMDD.

C is either 0 or 9 ?
 

I would like to send this field to view and display as normal date format so that it will work with AJAX calendar. 

How could I do that? Many thanks.

Use Python's datetime package, by parsing the integer and using the pieces.  The following should work, but is untested:

import datetime


day
= fld02 % 100
month
= (fld02 / 100) % 100
year    
= (fld02/10000) % 100
century
= 2000 if (fld02/100000 == 0) else 1900


df
= datetime.datetime(century + year, month, day)



This leaves the  hour, minute, seconds parts of df as 0.

You could also do the same thing with datetime.date() depending on your form fields.

Using datetime.strptime() isn't going to be any easier, since you would have to do the above calculations to turn your integer into string pieces to parse.

/dps

yell...@gmail.com

unread,
Mar 5, 2016, 10:01:00 AM3/5/16
to web2py-users
thank you for the suggestion Dave. I will give it a try and let you know the result.


On Wednesday, March 2, 2016 at 11:23:51 PM UTC-8, yell...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages