27
2
Amazingly, this simple task doesn't seem to exist already, so...
Your task is to write a program that takes as input a 12 hour time, and converts it into "military time", or 24-hour time format.
Input will be in the form:
HH:MM am/pm
Although slight variations are allowed:
The space separating the am/pm from the rest of the time is optional.
The last part can either be "am"/"pm", or "a"/"p".
Any capitalization is fine.
Output will be the inputted time, transformed into 24-hour format. It can be a number, or a string.
For both the input and output:
0s in the first place are optional. 0s in the last 3 places are mandatory.
the delimiter separating hours and minutes can be a ":", " " (a space), or nothing.
Other notes:
Midnight can be expressed either as 0000, or 2400.
Midnight will be considered "am", while noon will be considered "pm".
Your program can be a function, or a full program, and should either return the result, or output it to the stdout. Trailing whitespace is ok.
Examples (you aren't required to support every format):
12:00am -> 0000
1200 pm -> 1200
1234Am -> 00:34
134a -> 134
01:34PM -> 13 34
1234pm -> 1234
This is code golf, so the smallest number of bytes wins. Since this is so trivial to solve using a built-in, it would be nice to see some code that manually solves this (but use of built-ins are fine).
Just to clarify, you aren't required to support every possible format. Supporting only a single input and a single output format (of your choice) is fine. I would however like to limit the format as outlined above (which is already quite free). {1134,'pm'}
, for example, would be unacceptable.
What formats are we required to support? Just one? – redstarcoder – 2017-01-06T14:33:57.167
@redstarcoder Yes. Any format that conforms to the requirements above is ok, but as noted above the example cases, you aren't required to support every format. A single format of input and a single format of output are fine. – Carcigenicate – 2017-01-06T14:36:11.880
are our formats allowed to require leading zeroes? For example,
01:54pm
would work, but1:54pm
wouldn't? – FlipTack – 2017-01-06T20:41:48.057Yes. You can pick the required input format. – Carcigenicate – 2017-01-06T20:47:52.413
Are we allowed to include seconds in our input and output? – 12Me21 – 2017-01-26T13:26:20.427
@12Me21 No, sorry. The output should be exactly as outlined above. Maybe if this was asked write after I posted it, but this far down the road, I don't want to start making exceptions to allowed output. – Carcigenicate – 2017-01-26T14:29:38.060
Downvoter, care to comment? – Carcigenicate – 2017-05-05T22:34:52.733