Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Last active November 30, 2021 18:31
Show Gist options
  • Save eduardoaugustojulio/12aa1745cb40d8ddf074aece1f3765c2 to your computer and use it in GitHub Desktop.
Save eduardoaugustojulio/12aa1745cb40d8ddf074aece1f3765c2 to your computer and use it in GitHub Desktop.
An inherited class from otl_datime that makes it easy to work with dates in the otlv4 library
#ifndef OTLDATETIME_H
#define OTLDATETIME_H
/*
* abstracao do otl_datetime
*/
#include <iomanip>
#include <otlv4.h>
class OtlDatetime : public otl_datetime
{
private:
public:
OtlDatetime(int ayear=1970,
int amonth=1,
int aday=1,
int ahour=0,
int aminute=0,
int asecond=0,
unsigned long afraction=0,
int afrac_precision=0) :
otl_datetime(ayear,
amonth,
aday,
ahour,
aminute,
asecond,
afraction,
afrac_precision)
{
}
OtlDatetime(const char *str, const char *fmt){
*this = fromString(str, fmt);
}
~OtlDatetime(){}
bool operator==(time_t in) { return (toTime_t() == in); };
bool operator!=(time_t in) { return !(*this == in); };
bool operator==(otl_datetime in){ return (year == in.year && month == in.month && day == in.day); }
bool operator!=(otl_datetime in){ return !(*this == in); }
bool operator<(otl_datetime in){ return ((year < in.year) || (year == in.year && month < in.month) || (year == in.year && month == in.month && day < in.day)); }
bool operator>(otl_datetime in){ return ((year > in.year) || (year == in.year && month > in.month) || (year == in.year && month == in.month && day > in.day)); }
bool operator<=(otl_datetime in){ return !(*this > in); }
bool operator>=(otl_datetime in){ return !(*this < in); }
OtlDatetime& operator=(const time_t &in){
struct tm dt;
localtime_r(&in, &dt);
*this = OtlDatetime(dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
return *this;
}
OtlDatetime operator ++ (int){ addDay(); return *this; }
OtlDatetime operator -- (int){ subDay(); return *this; }
OtlDatetime now(){
*this = std::time(0);
return *this;
}
int daysInMonth(){
int days;
if(month == 2){
days = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28;
} else if(month == 4 || month == 6 || month == 9 || month == 11){
days = 30;
}else{
days = 31;
}
return days;
}
void addDay(){
day++;
if(day > daysInMonth()){
addMonth();
day = 1;
}
}
void addDay(int n){
for(int i = 1; i <= n; i++){
addDay();
}
}
void subDay(){
day--;
if(day == 0){
subMonth();
day = daysInMonth();
}
}
void subDay(int n){
for(int i = 1; i <= n; i++){
subDay();
}
}
void addMonth(){
month++;
if(month > 12){
addYear();
month = 1;
}
}
void addMonth(int n){
for(int i = 1; i <= n; i++){
addMonth();
}
}
void subMonth(){
month--;
if(month == 0){
subYear();
month = 12;
}
}
void subMonth(int n){
for(int i = 0; i <= n; i++){
subMonth();
}
}
void addYear(){
year++;
}
void subYear(){
year--;
}
virtual bool isValid(){
OtlDatetime empty;
return (*this != empty);
}
virtual time_t toTime_t(){
struct tm tmp;
tmp.tm_year = year - 1900;
tmp.tm_mon = month - 1;
tmp.tm_mday = day;
tmp.tm_hour = hour;
tmp.tm_min = minute;
tmp.tm_sec = second;
tmp.tm_isdst = 0; // desabilita horario de verao
return mktime(&tmp);
}
virtual std::string toString(const char* fmt = "%F"){
char buffer[128];
time_t time = toTime_t();
strftime(buffer, sizeof(buffer), fmt, localtime(&time));
return std::string(buffer);
}
OtlDatetime fromString(const char *str, const char*fmt){
if(*str){
struct tm dt = { 0 };
if(strptime(str, fmt, &dt) == NULL){
return OtlDatetime();
}
return OtlDatetime(dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
}
return OtlDatetime();
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment