problem is doing it within the db ^_^

Actually your answer is correct, and I need to rectify my example ....

period_to_update = db(db.periods.id==1).select().​first()
start_time = period_to_update.start_time
elapsed = request.now - start_time
period_to_update.update_​record(end_time=request.now, 
seconds_elapsed=elapsed.days*24*3600 + elapsed.seconds)

Il giorno lunedì 26 marzo 2012 13:29:07 UTC+2, tsvim ha scritto:
>
>
> Not sure if this is relevant, but you can do the following with datetime.
>
> >>> import datetime
> >>> a = datetime.datetime(2007, 12, 6, 16, 29, 43, 79043)
> >>> b = datetime.datetime(2011, 10, 3, 12, 0, 0, 0)
> >>> c = b-a
> >>> c
> datetime.timedelta(1396, 70216, 920957)
> >>> (c.microseconds + (c.seconds + c.days * 3600 * 24) * 10**6) / 10**6
> 120684616
>
> (I have Python 2.6 at work, otherwise you can do c.total_seconds())
>
> On Monday, March 26, 2012 1:41:09 AM UTC+2, Niphlod wrote:
>>
>> Doh, you're right. All the "datetime" api on fields are extracting, not 
>> converting the actual value....
>> At this point you can sum up years difference, month difference, etc etc 
>> etc separately and then extract the time passed by.
>> .....
>> duration_ye = (db.periods.end_time.year() - 
>> db.periods.start_time.year()).​​sum()
>> duration_mo = (db.periods.end_time.month() - 
>> db.periods.start_time.month())​​.sum()
>> duration_da = (db.periods.end_time.day() - 
>> db.periods.start_time.day()).​​sum()
>> .....
>>
>> This will be a "itchy" shot anyway.... even if you know that the months 
>> difference is "1" you can't really say how many days passed....
>>
>> Maybe at this stage is better to save unix time, that is an integer, and 
>> proceed simply with
>>
>> duration = (db.periods.end_time - db.periods.start_time).sum()
>>
>> Or, having the application filling in the seconds elapsed when it 
>> completes.... something like
>> period_to_update = db(db.periods.id==1).select().​​first()
>> start_time = period_to_update.start_time
>> period_to_update.update_​​record(end_time=request.now, 
>> seconds_elapsed=(request.now - start_time).seconds)
>>
>> Sorry for the wrong previous advices, really, I should have tested with 
>> all kinds of values (instead of 2012-01-01 00:00:00 and 2012-01-01 
>> 00:00:15).
>>
>

Reply via email to