-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-conversion.cpp
73 lines (59 loc) · 1.29 KB
/
time-conversion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @file time-conversion.cpp
* @author Alexander Khvolis(ExpliuM)
* @link https://www.hackerrank.com/challenges/time-conversion/problem?isFullScreen=true
* @version 0.1
* @date 2023-02-17
*
* @copyright Copyright (c) 2023
*
*/
#include <functional>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <chrono>
#include <locale>
using namespace std;
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
string timeConversion(string s)
{
struct std::tm tm;
std::istringstream is(s);
string ampm;
is.imbue(std::locale("en_US.utf-8"));
is >> std::get_time(&tm, "%H:%M:%S") >> ampm;
if (ampm.compare("PM") == 0)
{
if (tm.tm_hour != 12)
{
tm.tm_hour += 12;
}
}
else if (tm.tm_hour == 12)
{
tm.tm_hour = 0;
}
std::ostringstream oss;
oss << std::put_time(&tm, "%H:%M:%S");
std::string result = oss.str();
return result;
}
int main()
{
// ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
string result = timeConversion(s);
// fout << result << "\n";
cout << result << "\n";
// fout.close();
return 0;
}