forked from AhmedRaja1/Hacktoberfest-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.cpp
45 lines (34 loc) · 920 Bytes
/
clock.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
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int sec_prev=0;
while(1)
{
int seconds, minutes, hours;
string str;
//storing total seconds
time_t total_seconds=time(0);
//getting values of seconds, minutes and hours
struct tm* ct=localtime(&total_seconds);
seconds=ct->tm_sec;
minutes=ct->tm_min;
hours=ct->tm_hour;
//converting it into 12 hour format
if(hours>=12)
str="PM";
else
str="AM";
hours=hours>12?hours-12:hours;
//printing the result
if(seconds==sec_prev+1 || (sec_prev==59 && seconds==0))
{
system("CLS");
cout<< (hours<10?"0":"") << hours <<":" << (minutes<10?"0":"") << minutes << ":" << (seconds<10?"0":"") << seconds << " " << str <<endl;
}
sec_prev=seconds;
}
return 0;
}