view source

Converting seconds to English readable time duration

June 12th, 2008 . by polygeek

In a project I’m working on I get the number of seconds since the device was turned on and I need to display a message to the user similar to this: System uptime is 2 years, 1 month, 13 days, 1 hour and 15 seconds.

I figured that since showing System uptime is common to many websites it would be easy to find someone who had already written some code to do the conversion. After about 15 minutes of searching I hadn’t found anything and figured I could have written the code myself in that time. ( It actually took closer to 30 minutes to write the code because John Wilker stole my mojo and I was feeling pretty stupid. )

view source

Trouble with time

So I lied. It ended up taking hours to get the code to work just right, at least mostly right.

There were losts of little things that I had to accomodate. Like if the time output ends with hours then it needs to say, “blah, blah, blah and some hours.” Originally I only took into account the “and” going before the seconds. But if there are no seconds then … you get my meaning.

Oh, and adding a space in some places to the output string turned out to be harder than I thought it would be. But after messing with output for a while I think I got it.

Time is an illusion, lunch-time doubly so

But then the issue came up: how many seconds are there in a year, or whatever. There are lots of different ways to measure time durations. Turns out we’re pretty well set for things like minutes, hours, and days. Beyond that it starts getting messy.

The way my code works is to take the number of seconds you input and create a date starting that many seconds after January 1, 1970. It’s then doing conversions to see how many years, month, days, etc. after that date. Which is probably going to be an approximation of what you really want. But the problem is that I don’t know what you really want. It could be that you want to measure time before or after right now. That would be a different algorythm. If that’s what you need then you have my source to start from. This solution works for my current need and I’ll bet it would work for Douglas Adams, if he needed such a thing.

If you know of some standards that are used in time calculations, such as this I’d love to hear about it in the comments. Or anything else you might have to share on the matter.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Digg
  • Facebook

5 Responses to “Converting seconds to English readable time duration”

  1. comment number 1 by: Tink

    Is it feasible to create 2 DateFormatters, give one a start date and one the current time, then once they are formated minus one the days, hours, mins etc value from each other?

  2. comment number 2 by: Gareth Arch

    I had needed to do some date conversions/calculations recently and had created my own dateUtils class. I had based it around ColdFusion’s date functions (as that is what I was used to), so I had a dateAdd( datePart, number, date ), dateDiff( datePart, startDate, endDate ), etc. I was a little surprised before I began work on it that AS didn’t have more functions that would calculate all of this for you. Nothing was too crazy, but it seems like a basic feature of any programming language. If you like, I’ll dig up the dateDiff one, as you could just recursively loop over it, subtracting the previously extracted unit of time from the total seconds until you progressively got back to seconds.

    In your example, if I do 60 seconds, click convert, I get one minute. However, if I then add 1 month, I get 30 days, 10 hours, 1 minute, and 2 seconds. I guess this is due to the way you are handling the date/time calculation from seconds. I’m pretty sure my dateDiff method handles all of that correctly.

  3. comment number 3 by: polygeek

    @Tink, I didn’t mess with the DateFormatters but that sounds like it would work just fine, as long as that’s what you’re trying to do. ( see comment below ).

  4. comment number 4 by: polygeek

    @Gareth, As @Tink mention, I think that the DateFormaters in the Flex framework have most of what you’d want to do with time calculations.

  5. comment number 5 by: polygeek

    What I really learned from this is that you really have to think about what you’re measuring when it comes to time. If you have a number of seconds and you want to arbitrarily convert it then I think my solution above works out pretty well.

    But if you want to calculate something like, “exactly one month ago starting now” then you have to go about it differently. It basically comes down to measuring time is a real mess.

    That, I think, is one of the best arguments that there is no God - of the Christian variety. Since time is so important in many religious celebrations it would make sence for God to have created the Earth/Moon so that they orbited precisely around the sun. For instance, if the Moon orbited the Earth exactly 12 times in a year and the Earth rotated an exact integer number of times per year then that would be very convenient and provide insurmountable evidence for the existence of God.

    Or it could be that God just wanted to mess with us. :-)

Leave a Reply

Name

Mail (never published)

Website

- Why ask? This confirms you are a human user!

   




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites