Skip to content

Instantly share code, notes, and snippets.

@pratimugale
Last active September 8, 2019 11:50
Show Gist options
  • Save pratimugale/9c0756efde8466c661a01c17e5f02644 to your computer and use it in GitHub Desktop.
Save pratimugale/9c0756efde8466c661a01c17e5f02644 to your computer and use it in GitHub Desktop.
/* Author: Pratim Ugale <pratim.ugale@gmail.com>
*
* To change Date, Time settings of the clock, switch OFF "Automatically set time from the Internet" on your PC
*
* Run this program with root permissions(sudo)
*
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
using namespace std;
enum Volume{
LOW = 1,
MEDIUM = 2,
HIGH = 3
};
enum AlarmTone{
TONE1 = 1,
TONE2 = 2,
TONE3 = 3
};
class Clock{
private:
Volume volume;
AlarmTone tone;
int timeFormat = 12;
public:
Clock(){
setVolume(LOW);
setAlarmTone(TONE1);
}
void setVolume(Volume);
void showTime();
void setTime(int date, int month, int year,int hour, int min, int sec);
void changeTimeFormat();
void setAlarmTone(AlarmTone);
void ringClock();
void timer(int seconds);
int getTimeFormat();
};
void Clock::showTime(){
while(true){
system("clear");
time_t mytime = time(NULL);
cout<<ctime(&mytime)<<endl;
usleep(1000000);
}
}
void Clock::setVolume(Volume volume){
this->volume = volume;
cout<<"Volume set to "<<this->volume<<endl;
}
void Clock::setTime(int date, int month, int year,int hour, int min, int sec)
{
//buffer to format command
unsigned char buff[32]={0};
//formatting command with the given parameters
sprintf((char*)buff,(const char *)"date -s \"%02d/%02d/%04d %02d:%02d:%02d\"",month,date,year,hour,min,sec);
//execute formatted command using system()
system((const char *)buff);
}
void Clock::setAlarmTone(AlarmTone tone){
this->tone = tone;
cout<<"Alarm Tone changed to TONE"<<this->tone<<endl;
}
void Clock::changeTimeFormat(){
if(this->timeFormat == 12)
this->timeFormat = 24;
else if(this->timeFormat == 24)
this->timeFormat = 12;
}
int Clock::getTimeFormat(){
return this->timeFormat;
}
void Clock::timer(int seconds){
usleep(seconds * 1000000);
this->ringClock();
}
void Clock::ringClock(){
if (this->volume == 1)
cout<<"Timer rings with VOLUME: LOW and TONE: TONE"<<this->tone;
else if (this->volume == 2)
cout<<"Timer rings with VOLUME: MEDIUM and TONE: TONE"<<this->tone;
else if (this->volume == 3)
cout<<"Timer rings with VOLUME: HIGH and TONE: TONE"<<this->tone;
cout<<endl;
usleep(3000000);
}
int main(){
static Clock clock;
while(true){
system("clear");
time_t mytime = time(NULL);
struct tm * timeinfo;
timeinfo = localtime (&mytime);
char buffer [150];
if (clock.getTimeFormat() == 24)
strftime (buffer,150,"Clock Display \n\t|\n\t-> TIME : %R:%S %p (24 hr) \n\t-> DATE : %d/%m/%Y\n\t-> DAY : %A\n\t-> MONTH : %B \n",timeinfo);
else if (clock.getTimeFormat() == 12)
strftime (buffer,150,"Clock Display \n\t|\n\t-> TIME : %r (12 hr) \n\t-> DATE : %d/%m/%Y\n\t-> DAY : %A\n\t-> MONTH : %B \n",timeinfo);
puts (buffer);
//cout<<"CURRENT TIME: "<<endl<<"\t"<<ctime(&mytime)<<endl;
cout<<"Clock Features:"<<endl;
cout<<"1. Display Time Continuously \n2. Set Volume \n3. Set Date and Time \n4. Set Alarm Tone\n5. Switch b/w 12/24 Time Formats \n6. Use Timer \n7. Start Stopwatch"<<endl;
int input;
cin>>input;
switch(input){
case 1:
clock.showTime();
break;
case 2:
cout<<endl<<"Desired Volume:"<<endl;
cout<<"1 -> LOW \n2 -> MEDIUM \n3 -> HIGH"<<endl;
int volume;
cin>>volume;
if (volume == 1)
clock.setVolume(LOW);
else if (volume == 2)
clock.setVolume(MEDIUM);
else if (volume == 3)
clock.setVolume(HIGH);
break;
case 3:
cout<<"Enter (Day, Month, Year, Hours, Minutes, Seconds) in (DD, MM, YYYY, HH, MM, SS) format seperated by enter: "<<endl;
int day, month, year, hours, minutes, seconds;
cin>>day>>month>>year>>hours>>minutes>>seconds;
clock.setTime(day, month, year, hours, minutes, seconds);
break;
case 4:
cout<<endl<<"Desired Tone:"<<endl;
cout<<"1 -> TONE1 \n2 -> TONE2 \n3 -> TONE3"<<endl;
int tone;
cin>>tone;
if (tone == 1)
clock.setAlarmTone(TONE1);
else if (tone == 2)
clock.setAlarmTone(TONE2);
else if (tone == 3)
clock.setAlarmTone(TONE3);
break;
case 5:
clock.changeTimeFormat();
break;
case 6:
cout<<"Timer value in seconds: "<<endl;
int secs;
cin>>secs;
cout<<"The timer for "<<secs<<" seconds has started."<<endl;
clock.timer(secs);
break;
case 7:
cout<<"Stopwatch started."<<endl;
int stopwatch = 0;
while (true){
system("clear");
stopwatch++;
cout<<"STOPWATCH \n\t|\n\t-> "<<stopwatch<<" seconds."<<endl<<endl;
cout<<"Press ctrl-c to stop"<<endl;
usleep(1000000);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment