NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » time structure check
February 04, 2010 by Solorbob |
If one had two time structures and wanted to see if timeA was greater or less than timeB, how would one go about it? Below is the structure def. typedef struct { // structure for keeping time data
}time; . . . time timeA,timeB; thanks, Shawn |
---|---|
February 04, 2010 by Frozenlock |
I'm quite a noob for writing in C, but if it's simply to check if timeA is greater than timeB, I would do sometime like this:
Warning, it doesn't check if there is an equality. Frozenlock |
February 04, 2010 by bretm |
If timeA is 4hr 30min and timeB is 5hr 20min, the first "if" will be skipped, but it will go into the second "if" because timeA.minutes is greater than timeB.minutes, so that's not quite right.
I think that should work. Sign will be positive if A>B, negative if A<B, or zero if A=B. |
February 05, 2010 by Solorbob |
Thanks all. I'll see if I can get my time checks working tonight. |
February 05, 2010 by Frozenlock |
Didn't think of that, nice catch Solorbob! |
February 05, 2010 by Frozenlock |
Err, I meant bretm... |
February 05, 2010 by N3Roaster |
Another option to consider here is to simply convert the time structure to second equivalents and compare those. That is,
Then it's just a matter of comparing a with b. |
February 05, 2010 by bretm |
That would work if sizeof(int) is 4, but it's probably going to be 2, so the expression will overflow at 9h 6m 8s. |
February 06, 2010 by N3Roaster |
Checking <stdint.h> shows you're right about the sizeof(int). If the overflow issue is relevant for this use, the basic concept still works. Just replace int with long int, unsigned long int, or if portability is required, a type that's explicit about how many bits it needs such as int32_t or uint32_t. |
February 06, 2010 by Solorbob |
Thanks. I'll keep the overflow in mind while making the changes. |
February 06, 2010 by wayward |
If you have the luxury of modifying the declaration of type "time", you could rewrite it into something more suitable for comparison; for example, (ab)using the fact that 16-bit AVRs are little-endian:
NB. this works only on little-endian architectures, so 16-bit AVR (not AVR32 which is big-endian!) and x86 are both fine. For big-endianness you'd reverse the order of declarations inside the time_t.access struct, having hours up front and seconds at the end. Needless to say, use this approach only if you are 100% certain you will never ever want to port the code to another platform. To wrap things up, here's a nifty function from Jan Wolter's UNIX Incompatibility Notes, showing how to determine endianness during runtime:
HTH, Zoran |
February 07, 2010 by wayward |
Please replace all references to "16-bit AVR" with "8-bit AVR" in the above post. The code as shown would fail to work on a pure 16-bit architecture, anyway. :) |
Please log in to post a reply.
Did you know that you can impress a loved one with a digitally-controlled Valentine's Day Card with randomly twinkling LEDs? Learn more...
|