Skip to content

Instantly share code, notes, and snippets.

@jonman364
Created November 4, 2014 15:38
Show Gist options
  • Select an option

  • Save jonman364/22f4c82b780c0ad95fa0 to your computer and use it in GitHub Desktop.

Select an option

Save jonman364/22f4c82b780c0ad95fa0 to your computer and use it in GitHub Desktop.
#include "calendar.h"
calendar::calendar() : QDate(){
}
calendar::calendar(int y, int m, int d) : QDate(y, m, d){
}
calendar::calendar(const QDate &date) : QDate(){
setDate(date.year(), date.month(), date.day());
}
bool calendar::isHoliday() const{
bool retval = false;
if(*this == newYear(year()))
retval = true;
else if(*this == MLKDay(year()))
retval = true;
else if(*this == memorial(year()))
retval = true;
else if(*this == independence(year()))
retval = true;
else if(*this == labor(year()))
retval = true;
else if(*this == thanksgiving(year()))
retval = true;
else if(*this == thanksgivingFriday(year()))
retval = true;
else if(*this == christmasEve(year()))
retval = true;
else if(*this == christmas(year()))
retval = true;
else if(*this ==newYearEve(year()))
retval = true;
return retval;
}
bool calendar::isDayOff() const{
bool retval = false;
if(isHoliday())
retval = true;
else if(month() == 1 && ((day() == 2 && newYear(year()).dayOfWeek() == Qt::Sunday) || (day() == 3 && newYear(year()).dayOfWeek() == Qt::Saturday)))
retval = true;
else if(month() == 7 && ((day() == 3 && independence(year()).dayOfWeek() == Qt::Saturday) || (day() == 5 && independence(year()).dayOfWeek() == Qt::Sunday)))
retval = true;
else if(month() == 12 && (day() == 23 && christmasEve(year()).dayOfWeek() == Qt::Saturday) || (day() == 22 && christmasEve(year()).dayOfWeek() == Qt::Sunday))
retval = true;
else if(month() == 12 && ((day() == 26 && christmas(year()).dayOfWeek() == Qt::Sunday) || (day() == 27 && christmas(year()).dayOfWeek() == Qt::Saturday)))
retval = true;
else if(month() == 12 && ((day() == 30 && newYearEve(year()).dayOfWeek() == Qt::Saturday) || (day() == 29 && newYearEve(year()).dayOfWeek() == Qt::Sunday)))
retval = true;
return retval;
}
calendar calendar::nextHoliday() const{
return calendar(year(), month(), day());
}
int calendar::dayOfWeekOfMonth() const{
int retval = -1;
if(isValid()){
if(day() < 8)
retval = 1;
else if(day() < 15)
retval = 2;
else if(day() < 22)
retval = 3;
else if(day() < 29)
retval = 4;
else
retval = 5;
}
return retval;
}
bool calendar::isWeekend() const{
if(dayOfWeek() > Qt::Friday)
return true;
else
return false;
}
bool calendar::isPayday() const{
bool retval = false;
if(year() >= 2008){
calendar start(2008, 1, 10);
if(daysTo(start) % 14 == 0 && !isDayOff())
retval = true;
else if(static_cast<calendar>(addDays(1)).isDayOff()){
if(addDays(1).daysTo(start) % 14 == 0 && !isDayOff())
retval = true;
else if(static_cast<calendar>(addDays(2)).isDayOff() && addDays(2).daysTo(start) % 14 == 0)
retval = true;
}
}
return retval;
}
#ifndef CALENDAR_H_
#define CALENDAR_H_
#include <QDate>
class calendar : public QDate{
public:
calendar();
calendar(int y, int m, int d);
calendar(const QDate &date);
bool isHoliday() const;
bool isDayOff() const;
calendar nextHoliday() const;
int dayOfWeekOfMonth() const; //Eg 1st Saturday
bool isWeekend() const;
bool isPayday() const;
static calendar newYear(int y){
return calendar(y, 1, 1);
}
static calendar MLKDay(int y){
calendar jan(y, 1, 1);
if(jan.dayOfWeek() != Qt::Monday)
jan = jan.addDays(Qt::Sunday + 1 - jan.dayOfWeek());
return jan.addDays(14);
}
static calendar memorial(int y){
calendar may(y, 5, 31);
return may.addDays(0 - may.dayOfWeek() + 1);
}
static calendar independence(int y){
return calendar(y, 7, 4);
}
static calendar labor(int y){
calendar sept(y, 9, 1);
if(sept.dayOfWeek() != Qt::Monday)
sept = sept.addDays(Qt::Sunday + 1 - sept.dayOfWeek());
return sept;
}
static calendar thanksgiving(int y){
calendar nov(y, 11, 1);
if(nov.dayOfWeek() <= Qt::Thursday)
nov = nov.addDays(Qt::Thursday - nov.dayOfWeek());
else if(nov.dayOfWeek() == Qt::Sunday)
nov = nov.addDays(4);
else
nov = nov.addDays(11 - nov.dayOfWeek());
return nov.addDays(21);
}
static calendar thanksgivingFriday(int y){
calendar nov(calendar::thanksgiving(y));
return nov.addDays(1);
}
static calendar christmasEve(int y){
return calendar(y, 12, 24);
}
static calendar christmas(int y){
return calendar(y, 12, 25);
}
static calendar newYearEve(int y){
return calendar(y, 12, 31);
}
// Extra stuff
static calendar retireeMeeting(int y, int m){
calendar ret(y, m, 1);
if(ret.dayOfWeek() <= Qt::Wednesday)
ret = ret.addDays(Qt::Wednesday - ret.dayOfWeek());
else
ret = ret.addDays(10 - ret.dayOfWeek());
return ret.addDays(7);
}
};
#endif /*CALENDAR_H_*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment