// Kevin Van Wilder // Rolnr: 78363 // Taak 1 #include #include #include #include "mytime.h" const int SecondsInDay(86400); const int SecondsInHour(3600); const int SecondsInMinute(60); // Zeller Algorithm // thanks to http://www.vectorbd.com/bfd/c/weekday.c int DayOfTheWeek(int date, int month, int year) { int day(0); int yr(0); int mn(0); int n1(0); int n2(0); #define ZELLER_OFFSET -1 yr = year; mn = month; if (mn < 3) { mn += 12; yr -= 1; } n1 = (26 * (mn + 1)) / 10; n2 = (int) ((125 * (long) yr) / 100); day = ((date + n1 + n2 - (yr / 100) + (yr / 400) + ZELLER_OFFSET) % 7); return day; } int SecondsInYear(int year) { if (year % 4) return 365 * SecondsInDay; else return 366 * SecondsInDay; } int DaysInMonth(int month, int year) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; break; case 2: if (year % 4) return 28; else return 29; break; default: return 30; } } int SecondsInMonth(int month, int year) { return DaysInMonth(month, year) * SecondsInDay; } void ConvertTime(time_t UnixTime, int date_output, int time_output) { // Find the Date // mytime Time; // years while ( UnixTime >= SecondsInYear(Time.year) ) { UnixTime -= SecondsInYear(Time.year); Time.year++; } // months while ( UnixTime >= SecondsInMonth(Time.month, Time.year) ) { UnixTime -= SecondsInMonth(Time.month, Time.year); Time.month++; } // days while ( UnixTime >= SecondsInDay ) { UnixTime -= SecondsInDay; Time.day++; } // hours while ( UnixTime >= SecondsInHour ) { UnixTime -= SecondsInHour; Time.hour++; } // minutes while ( UnixTime >= SecondsInMinute ) { UnixTime -= SecondsInMinute; Time.minute++; } // seconds Time.second = UnixTime; // Select the Output method // stringstream temp; char *monthArray [12] = {"januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"}; char *dayArray [7] = {"zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag", "zondag"}; switch (date_output) { case 1: temp << Time.day << "/" << Time.month << "/" << Time.year; break; case 2: temp << Time.day << " " << monthArray[Time.month] << " " << Time.year; break; case 3: temp << dayArray[DayOfTheWeek(Time.day, Time.month, Time.year)] << " " << Time.day << " " << monthArray[Time.month] << " " << Time.year; break; default: temp << ""; } switch (time_output) { case 1: temp << " " << Time.day << "/" << Time.month << "/" << Time.year; break; case 2: temp << " " << Time.day << " " << monthArray[Time.month] << " " << Time.year; break; case 3: temp << " " << dayArray[DayOfTheWeek(Time.day, Time.month, Time.year)] << " " << Time.day << " " << monthArray[Time.month] << " " << Time.year; break; default: temp << ""; } return temp.str(); } int main() { std::cout << "1128626312" << std::endl; std::cout << ConvertTime(1128626312,1,1) << std::endl; return 0; }