patterncsharpMinor
Converting a 12 hour time string to a 24 hour time string
Viewed 0 times
convertinghourstringtime
Problem
Input - Standard input - HH:MM:SS[AM|PM] format.
Examples:
The program reads one line per instance.
Output - military format (24 hour format):
Examples:
- 12:00:00AM
- 12:00:00PM
- 01:00:10AM
- 01:00:10PM
The program reads one line per instance.
Output - military format (24 hour format):
- 00:00:00
- 12:00:00
- 01:00:10
- 13:00:10
using System;
class Solution {
static void Main(String[] args) {
var time = Console.ReadLine().Trim();
MilitaryTime mt = new MilitaryTime(time);
Console.WriteLine(mt);
}
class MilitaryTime{
string newTime = null;
string originalTime = null;
string correctHour(string time){
var ampmLen = 2;
var ampm = time.Substring(time.Length-ampmLen, ampmLen);
var hourIndex = 0;
var hour = time.Split(':')[hourIndex];
var h = hour;
if(ampm.Equals("PM")){
h = (int.Parse(hour) + 12).ToString();
}
if(hour.Equals("12") || hour.Equals("24")){
if(ampm.Equals("AM")){
h = "00";
} else if(ampm.Equals("PM")){
h = "12";
}
}
return h;
}
string minutesAndSeconds(string time){
var ampmLen = 2;
var hourLen = 2;
var startIndex = hourLen; // since we are taking after hour HH:MM:SS
return time.Substring(startIndex, time.Length - ampmLen - hourLen);
}
public MilitaryTime(string time){
originalTime = time;
}
public override string ToString(){
if(string.IsNullOrEmpty(newTime)){
newTime = correctHour(originalTime) + minutesAndSeconds(originalTime);
}
return newTime;
}
}
}- Is this code smelly?
- I re-factored code to this state, but did I miss anything?
- Is there a better way to solve this problem, using .net classes and features I might have missed?
- Does it look like that it's writt
Solution
Here's how to parse dates and times using built-in .Net libraries:
Some points to note:
static void Main()
{
string strTime = "01:00:10AM";
DateTime time;
if (DateTime.TryParseExact(strTime, "hh:mm:sstt", null,
System.Globalization.DateTimeStyles.None, out time))
{
Console.WriteLine("Parsed \"{0}\" as {1:HH\\:mm\\:ss}", strTime, time);
}
else
{
Console.WriteLine("Could not parse \"{0}\" as a DateTime", strTime);
}
Console.ReadLine();
}Some points to note:
- I'm not reinventing the wheel. The .Net DateTime library is the best way to handle points in time. (Use TimeSpan for duration)
- I'm using custom DateTime format strings for both parsing the string and displaying the parsed DateTime result.
- I'm using the
TryParsepattern the .Net supplies for converting strings to various built-in types: e.g., int.TryParse, double.TryParse, etc.
- In the output, I want to display the parsed time as
HH:mm:ss(note that capital H is 24 hour time and lowercase h is 12 hour time). I have to escape the colons in there because of how string.Format works. You escape colons using backslashes like thisHH\:mm\:ss. The problem is that you then have to escape the backslashes (or use verbatim strings). You do that with an extra backslash like thisHH\\:mm\\:ss
Code Snippets
static void Main()
{
string strTime = "01:00:10AM";
DateTime time;
if (DateTime.TryParseExact(strTime, "hh:mm:sstt", null,
System.Globalization.DateTimeStyles.None, out time))
{
Console.WriteLine("Parsed \"{0}\" as {1:HH\\:mm\\:ss}", strTime, time);
}
else
{
Console.WriteLine("Could not parse \"{0}\" as a DateTime", strTime);
}
Console.ReadLine();
}Context
StackExchange Code Review Q#110191, answer score: 5
Revisions (0)
No revisions yet.