how to compare date time in web2py?

1,951 views
Skip to first unread message

Amit

unread,
Jul 19, 2012, 5:05:41 AM7/19/12
to web...@googlegroups.com
Hi,
I have some records in a table and each record is having one field of type datetime [fromat : 2012-07-19 23:12:0 (YYYY-MM-DD HH:MM:SS)], table structure is:

Emp_ID
Emp_Name
Emp_Address
Emp_Salary
updated_on(Type: datetime)

periodically getting data against each Emp_ID and updating to the particular record, every 20 mins data has to be updated for each Emp_ID, for e.g : suppose for one Emp_ID data has updated on 2012-07-19 10:10:00 then again it has to update on 2012-07-19 10:30:00 etc...

and if data is not updated in 20 mins then one scheduler will verify against the updated_on column value and inform user that data has not updated  for particular employee.

Problem facing:
How to compare current datetime with updated_on coulmn value in web2py? can anybody please share me the code to achieve the same?

Niphlod

unread,
Jul 19, 2012, 10:36:59 AM7/19/12
to web...@googlegroups.com
current datetime is:
request.now

or
import datetime
datetime.datetime.now()

Andrew

unread,
Jul 20, 2012, 9:19:59 AM7/20/12
to web...@googlegroups.com
I'm not sure if web2py has something built-in to do this calculation but for other similar issues in the past I've just converted my dates to epoch and done the necessary math to see the difference between the two date-times. 

Jonathan Lundell

unread,
Jul 20, 2012, 9:34:04 AM7/20/12
to web...@googlegroups.com
On 20 Jul 2012, at 6:19 AM, Andrew wrote:
I'm not sure if web2py has something built-in to do this calculation but for other similar issues in the past I've just converted my dates to epoch and done the necessary math to see the difference between the two date-times. 

web2py ordinarily deals with datetime fields as Python datetime objects, which can just be compared (with due allowance for timezones). request.now has the current local time as a datetime; request.utcnow has UTC.

I use epoch dates myself in some cases, in particular to communicate with iOS clients through JSON, passing them as integers. The Python docs have quite a bit of material on manipulating dates, though it's a bit confusing in places. The modules of most interest are datetime, time and calendar.

Ovidio Marinho

unread,
Jul 20, 2012, 5:21:26 PM7/20/12
to web...@googlegroups.com

Difference of Two Dates

#----------------------------- 
# Dates produce timedeltas when subtracted.

diff = date2 - date1
diff = datetime.date(year1, month1, day1) - datetime.date(year2, month2, day2)
#----------------------------- 

bree = datetime.datetime(1981, 6, 16, 4, 35, 25)
nat  = datetime.datetime(1973, 1, 18, 3, 45, 50)

difference = bree - nat
print "There were", difference, "minutes between Nat and Bree"
#=> There were 3071 days, 0:49:35 between Nat and Bree

weeks, days = divmod(difference.days, 7)

minutes, seconds = divmod(difference.seconds, 60)
hours, minutes = divmod(minutes, 60)

print "%d weeks, %d days, %d:%d:%d" % (weeks, days, hours, minutes, seconds)
#=> 438 weeks, 5 days, 0:49:35

#----------------------------- 
print "There were", difference.days, "days between Bree and Nat." 
#=> There were 3071 days between bree and nat
      

       Ovidio Marinho Falcao Neto
                Web Developer
             ovid...@gmail.com 
          ovidio...@itjp.net.br
                 ITJP - itjp.net.br
               83   8826 9088 - Oi
               83   9334 0266 - Claro
                        Brasil
              



2012/7/20 Jonathan Lundell <jlun...@pobox.com>
--
 
 
 

akshay...@gmail.com

unread,
May 26, 2017, 9:36:23 AM5/26/17
to web2py-users, amit.k...@gmail.com
You can compare time here by using the relational operators. Access the current time by using 'request.now' (its value will be in same format as 'update_on' field of your table) then you can just compare it as :

if (dbquery).updated_time > request.now:
   
//do something

Peter

unread,
May 28, 2017, 8:52:04 PM5/28/17
to web2py-users, amit.k...@gmail.com
I had some fun with this

max_age_allowed = 20 * 60  # total seconds in 20 minutes
update_age = request.now - person.created_on # Duration in datetime format since record created
update_age = update_age.total_seconds() # Duration converted to seconds
print "It is %s seconds since record created (threshold is %s seconds)" % (update_age,max_age_allowed )
if update_age > max_age_allowed:
print 'Do something'
else:
print 'Nothing to do here'


output

It is 2102030.41814 seconds since record created (threshold is 1200 seconds)
Do something

Hope this helps.
Reply all
Reply to author
Forward
0 new messages