View Single Post
Old 06-09-2017, 05:26 AM   #1110
Nicolas F
Groupie
Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.Nicolas F once ate a cherry pie in a record 7 seconds.
 
Posts: 161
Karma: 1842
Join Date: Jan 2016
Device: Kobo Glo HD
Quote:
Originally Posted by red_dragon View Post
Beware, this is kind of ugly because Calibre doesn't provide math functions (especially modulo) that would make life so much easier.
Thank you! I couldn't get it to work...
I just changed 0.49 with 0.499 because I had two books that gave me reading times of X hours and -1 minutes (the reading time in hours was something like 3.9917).

Edit: and after having looked at it a little more, I hadn't though to use regex, but we could also simply do rt1_hour=re(time1, '\.\d*', ''); to truncate the hour... This way no rounding occurs

So here is what I got:
Spoiler:
Code:
program:

# This program calculate a reading time per hour for an ebook

# Adjustments:
# w_min -> minimum words per minute
# w_max -> maximum words per minute
w_min=250; 
w_max=300;

# readingtime per hour=60
p_time=60;

# Needs a custom column for word count (#words)
# change it to your field name
words=raw_field('#words');

time1=format_number(divide(words,multiply(w_max, p_time)), '{0:.2f}');
time2=format_number(divide(words,multiply(w_min, p_time)), '{0:.2f}');

# Unfortunately Calibre doesn't provide a simple way to convert a float
# into a time string -> I used a regex
# The minutes can be set to use 2 numbers using python number formatting,
# otherwise times are displayed as 4:4h instead of 4:04h.
rt1_hour=re(time1, '\.\d*', '');
rt1_minute=format_number(multiply(subtract(time1,rt1_hour),60), '{0:02.0f}');

rt2_hour=re(time2, '\.\d*', '');
rt2_minute=format_number(multiply(subtract(time2,rt2_hour),60), '{0:02.0f}');

readingtime=strcat(rt1_hour,':', rt1_minute, 'h - ', rt2_hour, ':', rt2_minute, 'h');


Thanks again for sharing

Last edited by Nicolas F; 06-09-2017 at 07:00 AM.
Nicolas F is offline   Reply With Quote