Back

best way to handle the 24 hr time format?

#26
I always found that subtracting 12 was too cumbersome. It's an extra step in what is already a one-step process.

The only reason a 12-hour clock means anything to your brain is because you associate certain times with meaningful events. 7:30am is breakfast, 12:00pm is lunch, 5:00pm is when you get off work, 11:00 is when you go to bed, etc. So just take a handful of meaningful events that happen after noon and re-assign the times.

15:00 schools let out
17:00 people get off work
19:00 prime time TV shows air
23:00 time to go to sleep
Reply
#27
dizmox Wrote:
unauthorized Wrote:Or you could do it in C:
Code:
char* to12htime(int time24, int min)
{
    if(time24 < 0 || time24 > 23) return NULL;
    if(min < 0 || min > 60) return NULL;
    char* tstr = malloc(9);
    if(time24 > 12) sprintf(tstr, "%d:%d PM", time24-12, min);
    else sprintf(tstr,"%d:%d AM", time24, min);
    return tstr; // remember kids, never return dynamic buffers like this ;)
}
Now, you can call to12htime() every time you need to convert. Remember to check that return value. And free the returned buffer when you are done with it.
I'll cheat and use Matlab.
Code:
mod(hour,12)
You both realize that C also has the modulus operator?
Code:
hour % 12
Just about any competent programing language does.

Personally, I've always used the subtract two method in my head.
Reply
#28
My mistake, then. D:
Reply
May 16 - 30 : Pretty Big Deal: Save 31% on all Premium Subscriptions! - Sign up here
JapanesePod101
#29
pm215 Wrote:(waaay off topic, but) *blink*. If you want to make the program exit on malloc failure then wrap malloc. If you don't check you don't get a crash, you get undefined behaviour, and you never want that. (If you're really unlucky you get undefined behaviour which is a security hole.)
Actually, any attempt to reference data by a NULL pointer would be caught by the CRT as per the standard specifications. This is one of the few security features in C.

@Daichi, dizmox: I thought it was bluntly obvious that I was trying to use the most pointlessly long way to do it. Why else would I use C?
Reply
#30
unauthorized Wrote:Actually, any attempt to reference data by a NULL pointer would be caught by the CRT as per the standard specifications. This is one of the few security features in C.
The C standard disagrees with you:
C99 standard section 6.5.3.2 paragraph 4 Wrote:The unary * operator denotes indirection. [...] If an
invalid value has been assigned to the pointer, the behavior of the unary * operator is
undefined.(90)
C99 footnote 90 Wrote:Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime.
Just because most C runtimes happen to make NULL pointer dereferences crash doesn't mean they are obliged to by the standard. In an embedded environment it's quite plausible that they will not be caught but instead you'll just scribble on low memory. The implementation is entirely at liberty to start nethack or cause demons to fly out of your nose or anything else it feels like. It's not a good idea to ignore malloc returns on platforms which do fault on accesses to NULL either -- Mark Dowd's neat Flash player exploit gets its starting point from a memory allocation call that some programmer in Adobe forgot to check for failure.
Reply
#31
pm215 Wrote:The C standard disagrees with you:
........
Just because most C runtimes happen to make NULL pointer dereferences crash doesn't mean they are obliged to by the standard. In an embedded environment it's quite plausible that they will not be caught but instead you'll just scribble on low memory.
I wasn't aware of that. Thanks for bringing it to my attention.

pm215 Wrote:The implementation is entirely at liberty to start nethack or cause demons to fly out of your nose or anything else it feels like.
So THIS is where the little suckers came from! For a moment, I was worried.
Reply
#32
Quote:@Daichi, dizmox: I thought it was bluntly obvious that I was trying to use the most pointlessly long way to do it. Why else would I use C?
Yeah, I did, but I didn't know there was a modulus function. I gave up on C halfway through some beginner's tutorial. Tongue
Edited: 2009-12-28, 9:13 pm
Reply