You can try:

task_duration = (db.t_periods.f_end.epoch() - db.t_periods.f_start.epoch()).
sum().with_alias('task_duration')

That should give you the duration in seconds, which you can then convert to 
a Python timedelta object if desired.

Anthony

On Thursday, March 8, 2018 at 2:27:24 PM UTC-5, backseat wrote:
>
> Thanks Anthony. It almost works (but it fails in an odd way). I was 
> getting some odd results, and I've narrowed it down to this:
>
> In [9]: query = (p.f_task == 16306)
>
> In [10]: task_duration = (db.t_periods.f_end - db.t_periods.f_start) 
>  
> In [11]: results = db(query).select(p.id, p.f_start, p.f_end, 
> task_duration).as_list() 
>  
> In [12]: results 
> Out[12]:  
> [{'_extra': {'(t_periods.f_end - t_periods.f_start)': 118.0}, 
>   't_periods': {'f_end': datetime.datetime(2018, 1, 8, 13, 59, 21), 
>    'f_start': datetime.datetime(2018, 1, 8, 13, 58, 3), 
>    'id': 37278L}}]
>
>
> That shows a task_duration of 118.0, whereas actually it's 1:18.
>
> Further investigation suggests that MySQL doesn't like the datetime - 
> datetime construct:
>
> MariaDB [pytrack2]> SELECT `t_periods`.`id`, `t_periods`.`f_start`, 
> `t_periods`.`f_en
> d`, (`t_periods`.`f_end` - `t_periods`.`f_start`) AS duration FROM 
> `t_periods` WHERE 
> (`t_periods`.`f_task` = 16306); 
> +-------+---------------------+---------------------+----------+ 
> | id    | f_start             | f_end               | duration | 
> +-------+---------------------+---------------------+----------+ 
> | 37278 | 2018-01-08 13:58:03 | 2018-01-08 13:59:21 |      118 | 
> +-------+---------------------+---------------------+----------+
>
> Maybe I need to build a view in MySQL and use that.
>
> Keith
>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to