comparing time from another source

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

I've been looking at the time options in time.h, and they seem to be somewhat limited.  What I want to do is get a timedate value from a file or whatever and compare it to the current timedate. I also want to make some calculations based on the original date and the current date.

 

The only way that I can see to do this is to populate a tm structure ( I think that's the one) and then use it vs what I get with one of the time functions. I would assume that I could also do difftime with the same data.

 

It seems cumbersome, but  probably more reliable than other systems that I've seen. If there's a simpler way, I'd love to hear about it.

 

Thanks,

 

hj

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

'Tis easy to compare. Work with TotalSeconds, as long as the two time values use the same epoch.

 

It is a bit more problematic if the imported value is not in TotalSeconds. I think that time.h has a time parser that will convert a date struct that you populate into total seconds. That will do it.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. Oct 15, 2017 - 02:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are tm_t and time_t structures.
tm_t is used for human readable output.
time_t is used for maths
.
There are functions that can convert between the two structs.
A common use might be to calculate the time to take a cake out of the oven. e.g. add 45 minutes
Or to calculate the parturition date from an insemination time. e.g. add 281 days
.
David.

Last Edited: Sun. Oct 15, 2017 - 07:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim, I can't find totalseconds anywhere in time.h I'm missing something here. (not a first for me :) )

 

DP -- thanks for that explanation -- and thanks for the vocabulary improvement -- I' have never seen the workd parturition before.

 

hj

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On laptop now..

 

tm is a struct type that holds the different parts of a date/time (seconds, minutes, hours, day, month...). (Note that the member tm_year is "year since 1900"!)

 

time_t is an numerical type that holds the total number of seconds since the epoch (often 1970-01-01 00:00).

 

mktime() is a function that converts from a tm structure to a time_t time.

 

So, if you have two date/times's in the form of [year, month, day, hour, minute, second): Stuff them into tm structs. Convert both to time_t times. Compare.

 

Overview of time.h contents here ("<ctime>" is just the C++ way of referring to time.h - don't let that confuse you).

 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

+1 what he said. 

 

The whole point about "time" is a bit like something such as ADC readings. For most of your code you keep everything in an easily manipulated binary format. Only when it finally comes time to try and entertain the hairless chimp operating your device do you finally convert things back to "human readable". For time, inside computers it's a whole lot easier just doing anything in terms of 32 bit numbers so 1508677945 minus 1508269282 is the easy way to calculate the difference between now and some time a few days ago. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks to all, it's beginning to make sense. Johan, your comments were especially enlightening.

 

hj