Skip to content

Instantly share code, notes, and snippets.

@Sarverott
Last active January 31, 2020 23:40
Show Gist options
  • Save Sarverott/6f7735a4b936cbebf8e487a0e0501f83 to your computer and use it in GitHub Desktop.
Save Sarverott/6f7735a4b936cbebf8e487a0e0501f83 to your computer and use it in GitHub Desktop.
simple time class
#ifndef TIME_H
#define TIME_H
/* Sarverott 2020 */
//#include <iostream>
//using namespace std;
class time{
private:
int hour=0;
int minute=0;
int second=0;
public:
time(char* line){
//cout<<"###TIME created!###("<<line<<")\n";
insertTime(line);
}
time(int h, int m, int s){
//cout<<"###TIME created!("<<h<<":"<<m<<":"<<s<<")###\n";
insertTime(h, m, s);
}
~time(){
//cout<<"###TIME destroyed!###\n";
}
time(){
//cout<<"###TIME created!###\n";
}
void insertTime(int s){
insertTime(s/3600, s/60%60, s%60);
}
void insertTime(int h, int m, int s){
this->hour=h;
this->minute=m;
this->second=s;
}
void insertTime(char* line){
//cout<<endl<<"TEST HOUR:"<<line<<endl<<endl;
this->hour=(*(line++)-'0')*10+(*(line++)-'0');
line++;
this->minute=(*(line++)-'0')*10+(*(line++)-'0');
line++;
this->second=(*(line++)-'0')*10+(*(line++)-'0');
}
void clearTime(){
this->hour=0;
this->minute=0;
this->second=0;
}
int getHour(){
return this->hour;
}
int getMinute(){
return this->minute;
}
int getSecond(){
return this->second;
}
char* toString(){
int tmp=this->hour/100;
int outputLength=8;
while(tmp!=0){
tmp/=10;
outputLength++;
}
char* output=new char();
output[outputLength--]='\0';
output[outputLength--]='0'+this->second%10;
output[outputLength--]='0'+this->second/10%10;
output[outputLength--]=':';
output[outputLength--]='0'+this->minute%10;
output[outputLength--]='0'+this->minute/10%10;
output[outputLength--]=':';
tmp=this->hour;
while(tmp!=0){
output[outputLength--]='0'+tmp%10;
tmp/=10;
}
if(this->hour<10){
output[0]='0';
}
if(this->hour==0){
output[1]='0';
}
//cout<<endl<<output<<endl;
return output;
}
/*void coutPrint(){
if(this->hour<10)cout<<'0';
cout<<this->hour;
cout<<':';
if(this->minute<10)cout<<'0';
cout<<this->minute;
cout<<':';
if(this->second<10)cout<<'0';
cout<<this->second;
}*/
};
#endif // TIME_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment