#include "date.h"
#include <QDate>
#include <cstdint>

#define MIN(A,B)    ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
#define MAX(A,B)    ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })

#define CLAMP(x, low, high) ({\
  __typeof__(x) __x = (x); \
  __typeof__(low) __low = (low);\
  __typeof__(high) __high = (high);\
  __x > __high ? __high : (__x < __low ? __low : __x);\
  })

Date::Date() {
}

Date::Date(int32_t y) {
	year = MAX(0, y);
}

Date::Date(int32_t y, int8_t m, int8_t d) {
	year = MAX(0, y);
	month = CLAMP(m, 1, 12);
	day = CLAMP(d, 1, 31);
}

void Date::SetYear(int32_t y) {
	year = MAX(0, y);
}

void Date::SetMonth(int8_t m) {
	month = CLAMP(m, 1, 12);
}

void Date::SetDay(int8_t d) {
	day = CLAMP(d, 1, 31);
}

int32_t Date::GetYear() {
	return year;
}

int8_t Date::GetMonth() {
	return month;
}

int8_t Date::GetDay() {
	return day;
}

QDate Date::GetAsQDate() {
	return QDate(year, month, day);
}
