473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re: Writing elapsed time as a string

En Fri, 02 May 2008 16:13:41 -0300, Simon Pickles <si*******@googlemail.comescribió:
I'm sorry if this has been asked a thousand (million) times.

Is there a nifty pythonesque way to produce a string representing an
elapsed time period, in terms of years, months, days, hours, mins, seconds?

I am storing times in a MySQL db, and would love to be able to write the
time elapsed between accesses of certain data. These are in seconds
since the epoch, as produced by time.time()
Two options:

a) You can construct a datetime object with that info, using datetime.datetime.fromtimestamp()
(note that the MySQLdb module already returns datetime objects for timestamp columns in MySQL).
If you substract two datetime objects, you get a timedelta object, which stores info as whole days, seconds (fraction of day) and microseconds (fraction of second). If you can live with years having exactly 365 days and months having exactly 30 days (or up to 35!):

def timedelta_str(td):
def plural(n):
if n>1: return 's'
return ''
out = []
y, d = divmod(td.days, 365)
if y: out.append('%d year' % y + plural(y))
m, d = divmod(d, 30)
if m: out.append('%d month' % m + plural(m))
if d: out.append('%d day' % d + plural(d))
h, s = divmod(td.seconds, 60*60)
if h: out.append('%d hour' % h + + plural(h))
m, s = divmod(s, 60)
if m: out.append('%d min' % m + plural(m))
if s: out.append('%d sec' % s + plural(s))
return ' '.join(out)

pyfrom datetime import datetime
pyd1 = datetime(1992, 5, 11, 8, 23, 8, 612)
pyd2 = datetime.now()
pyprint d1
1992-05-11 08:23:08.000612
pyprint d2
2008-05-08 09:21:53.592000
pyprint timedelta_str(d2-d1)
16 years 1 day 58 mins 45 secs

or b) Use the dateutil package <http://labix.org/python-dateutil>
It is probable right in front of me in the docs but I am spinning off
into outer space (mentally!)
Nope... showing time intervals as years,months,days... isn't trivial, because there are many special cases, and sometimes legal issues too. It's impossible to make everyone happy in this topic.

--
Gabriel Genellina

Jun 27 '08 #1
0 1475

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Besta | last post by:
Hello all, I am having trouble creating a windows service with a timer. Everything seems to go ok but the elapsed event does not fire.Can anyone shed any light on this, may be something simple as...
4
by: VB Programmer | last post by:
When I run my ASP.NET 2.0 web app on my dev machine it works perfect. When I precomile it to my web deployment project and then copy the debug files to my web server I get this problem when trying...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
1
by: Bill | last post by:
I have a large number of records that have an elapsed time in seconds for each one that I was to have a total time spent. I can sum them up with query easily enough but I need to be able to display...
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm facing right now is that, I'm using the 'time()'...
12
by: pekka | last post by:
I'm trying to measure user input time with my Timer class object. It isn't as easy as I expected. When using std::cin between timer start and stop, I get zero elapsed time. For some unknown reason,...
0
by: ravitunk | last post by:
hi all...i have a web service with a timer which has an Elapsed time of 1second(code should get executed for every second).....with the following code.....its running perfectly for the first time and...
0
by: M.-A. Lemburg | last post by:
On 2008-05-08 14:31, Gabriel Genellina wrote: Using mxDateTime that's pretty easy (using number of days): 3050:18:01:25.91 or, if you'd rather like to see things broken down as relative...
9
by: Ross | last post by:
I'm a newbie at this, and have searched a lot but can't find something that seems appropriate for measuring a recurring elapsed time. Creating an object with: var mydate = new Date(); seems...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.