24 hour time conversion

Try 12:30 or 0:30 and you will get these results: 0:30 pm and 0:30 am. These are wrong and should read 12:30 pm and 12:30 am.

Yes, there is. It is actually an interesting problem, in that it ends up being a friggin’ IQ test to write it. It took me 45 minutes. (In my defense, I did it in notepad instead of an IDE, which added development time.)

Mod returns the remainder, which is unhelpful. Here’s a big hint: you need an operator that performs integer division.

The challenge, as I read it:

Create a single line algorithm that returns a properly formatted 12-hour clock time string when passed an integer Hours and integer Minutes in 24-hour clock notation. No conditional statements are permitted, so no fair using IIF() immediate ifs.

I have done just such a thing, so rest assured it can be done. Try to figure it out yourself before verifying that my one liner works:


Public Function StandardTime(Hours As Integer, Minutes As Integer) As String
    StandardTime = 12 * (2 * Abs(Hours \ 13 - 1) - 1) + ((Hours - (Abs((Hours \ 13) - 1) * 12)) * Abs((Abs(Hours - 12) \ 12) - 1)) & ":" & Format(Minutes, "00") & " " & Chr(97 + 15 * (Hours \ 12)) & "m"
End Function

In case you can’t figure out what my algorithm is doing:

“” does regular division but only returns the integer part of the answer. (For example, 24 \ 12 = 2, whereas 23 \ 12 = 1, and 11 \ 12 = 0.)

“&” does string concatenation, automatically converting numbers (and anything else) into strings before concatenating.

Abs(AnyNumber) returns the absolute value.

Format(AnyNumber,“00”) pretties up the minutes to look like 01-09 and 11-59.

Chr(AnyNumber) returns the ascii character associated with that number. “a” is 97, for example.

.
Do nothing simply when a way can be found to make it complex and wonderful! :rolleyes:

:wink:

Ellis, you use the wrong programming tool!

Java (untested):

import java.text.*;

static DateFormat time12 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
static DateFormat time24 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.GERMAN);
static {
time24.setTimeZone(time12.getTimeZone());
}


String input24; // String with 0:00 - 23:59 time format.

String output12 = time12.format(time24.parse(input24)); // this the one line that counts.

and vice versa:


String input12; // String with 1:00 - 12:59 am/pm time format.

String output24 = time24.format(time12.parse(input12)); // again the line that counts.

This took less than 5 minutes (including the time to lookup JavaDoc).

cu

I’m ashamed to be part of this thread. It’s is 09:18 in the East, and in six hours it will be 15:18. Ask me how I did that…go ahead…

I added 6 and 9…together.

Shortcut that.

Apparently…yes. :smack:

That is not an answer to the question I was addressing:

…which is clearly asking for an algorithm. Your answer is a function call. While that internal function may use an algorithm, as opposed to conditional commands, I would tend to doubt it. (Why bother, accept as an intellectual exercise? There is no point.)

I gotta say, I’m really not feeling the love here. Where’s the props for my mad coding skills? Ok, a fourth grader could handle the coding I posted. But the logic was impressive, at least a little, no?

Such a shameless plea for validation surely deserves a pitting…

(But c’mon, that was tough, and I was impressed with myself.)

Hour12 = ((Hour24 + 1) % 12) - 1;

Oops, that should be:

Hour12 = ((Hour 24 - 1) % 12 + 1);

Does that return 12 if Hour24 is either 0 or 12? I’m guessing not. Unless (-1) % 12 + 1 = (11) % 12 + 1 = 12, that is.

My original algorithm, though functional, was unnecessarily complex. Optimally reduced, and removing the minutes and am/pm parts of it, the shortest possible algorithm appears to be:

Hour12 = Hour24 - 12 * (Hour24 \ 13 + (Hour24 - 12) \ 12)

The problem is that you need to subtract 12 if the hours is > 12, add 12 if the hours is 0, and leave hours alone for all other values.

Hour24 \ 13 will return 1 if hours > 12, otherwise it will return 0. So you can multiply this “identity element” by 12 for a basic subtraction that will cover all values from 1-23. (For 1-12, subtract 120. For 13-23, subtract 121.) It needs some help with 0, though.

(Hour24 - 12) \ 12 will return -1 if hours = 0, otherwise it will return zero. So for values 1-23, this element is ignored. (Adding 0 has no effect.) For 0, however, it will return -1. Since the green term returns 0 if the hours is 0, this element converts the “identity element” to -1. Since subtracting -12 is the same as adding 12, this handles the unique situation of 0 properly.

O ye gods! Often enough I am amused because I usually write down time of day in the 24-hour clock, which seems to confuse a lot of USA people, but hey, thanks to the SDMB, I fear I might have completely lost the ability to understand it myself.

How will I EVER manage to get a train or a 'bus again?

I will get lost, and starve to death, and then guess whom I shall blame.

Grr, grumble, too damn clever for their own good, some people! :eek:

In post #18 I already posted the shortest term: Hour12 = 1+((Hour24+11) mod 12)

Also I already posted there part of the algorithm you tried to do in one equation using “div” as integer division and “mod” as modulo or remainder (equal because valid values are all positive).

All I said, was, that you cannot convert it easily with modulo. For our brains it is easier to understand rules like 0 -> 12AM, 1-11 -> 1-11AM, 12 -> 12PM, 13-23 -> subtract 12, get 1-11PM. You can even make this simpler: On >=12 subtract twelve and make it PM; if 0 write 12 instead.

cu

My bad.

slinks away in shame

The 24 hour clock just comes naturally to most Brits. My clock-radio , VCR and microwave are all in the 24 hour format and , as Celyn has said, all bus , plane and train timetables are also written this way. So we don’t have to think of a conversion , we just get on with it.

Now we’re into algoritms? Geez. What is this, the government? Can’t we leave it at something simple, or must it always be absurdly complicated to derive a simple answer?

Hey , thats what google is for

Declan

You are absolutely correct!

Do nothing simply when a way can be found to make it complex and wonderful. :rolleyes: