std::string DateTime::to_short_time_string() const { throw_if_null(); // hh:mm StringFormat format("%1:%2"); format.set_arg(1, get_hour(), 2); format.set_arg(2, get_minutes(), 2); return format.get_result(); }
std::string DateTime::to_short_date_string() const { throw_if_null(); StringFormat format("%1-%2-%3"); format.set_arg(1, get_year(), 4); format.set_arg(2, get_month(), 2); format.set_arg(3, get_day(), 2); return format.get_result(); }
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions *) { if (!ob.get_type_info().is_const() && ob.get_type_info() == typeid(Result)) { return static_cast<Result *>(throw_if_null(ob.get_ptr())); } else { throw chaiscript::detail::exception::bad_any_cast(); } }
std::string DateTime::to_long_time_string() const { throw_if_null(); // hh:mm:ss StringFormat format("%1:%2:%3"); format.set_arg(1, get_hour(), 2); format.set_arg(2, get_minutes(), 2); format.set_arg(3, get_seconds(), 2); return format.get_result(); }
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions *) { if (ob.get_type_info().bare_equal_type_info(typeid(Result))) { auto p = throw_if_null(ob.get_const_ptr()); return std::cref(*static_cast<const Result *>(p)); } else { throw chaiscript::detail::exception::bad_any_cast(); } }
unsigned char DateTime::get_week() const { throw_if_null(); int day_of_week = 1 + ((get_day_of_week()+6)%7); DateTime dt = *this; DateTime nearest_thursday = dt.add_days(4 - day_of_week); DateTime jan1(nearest_thursday.year, 1, 1); int days = jan1.get_difference_in_days(nearest_thursday); int week = 1 + days / 7; // Count of Thursdays return week; }
std::string DateTime::to_short_datetime_string() const { throw_if_null(); // 2008-04-01 StringFormat format("%1-%2-%3 %4:%5:%6"); format.set_arg(1, get_year(), 4); format.set_arg(2, get_month(), 2); format.set_arg(3, get_day(), 2); format.set_arg(4, get_hour(), 2); format.set_arg(5, get_minutes(), 2); format.set_arg(6, get_seconds(), 2); return format.get_result(); }
DateTime &DateTime::add_months(int months) { throw_if_null(); int years = months / 12; year += years; int months_left = months - (years*12); month += months_left; if (month > 12) { month -= 12; year++; } return *this; }
std::string DateTime::to_string() const { throw_if_null(); // Mon Feb 3 12:32:54 2008 std::string months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; std::string days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; StringFormat format("%1 %2 %3 %4:%5:%6 %7"); format.set_arg(1, days[get_day_of_week()]); format.set_arg(2, months[get_month() - 1]); format.set_arg(3, get_day()); format.set_arg(4, get_hour(), 2); format.set_arg(5, get_minutes(), 2); format.set_arg(6, get_seconds(), 2); format.set_arg(7, get_year()); return format.get_result(); }
unsigned int DateTime::get_day_of_week() const { throw_if_null(); if (year < 1600) throw Exception("Unsupported date specified"); int century_anchor_days[4] = { 2, // 1600: Tuesday 0, // 1700: Sunday 5, // 1800: Friday 3 // 1900: Wednesday }; int century_value = year / 100; int century_day = century_anchor_days[century_value%4]; int y = year % 100; int a = y/12; int b = y%12; int c = b/4; int d = a + b + c; int doomsday = (century_day + d%7) % 7; // Doomsday of this year int month_doomsdays[12] = { 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 }; // Days in each month that are doomsdays bool leap_year = (year%4) == 0; // Leap years are every 4th year if ((year%100) == 0 && (year%400) != 0) // Except for those divisible by 100 unless they are also divisible by 400 leap_year = false; if (leap_year) { month_doomsdays[0] = 4; month_doomsdays[1] = 8; } int day_of_week = (doomsday + day - month_doomsdays[month-1]) % 7; if (day_of_week < 0) day_of_week += 7; return day_of_week; }
DateTime DateTime::to_local() const { throw_if_null(); if (timezone == local_timezone) { return *this; } else { #ifdef WIN32 SYSTEMTIME local_time; SYSTEMTIME system_time; system_time.wYear = year; system_time.wMonth = month; system_time.wDay = day; system_time.wHour = hour; system_time.wMinute = minute; system_time.wSecond = seconds; system_time.wMilliseconds = nanoseconds / 1000000; BOOL result = SystemTimeToTzSpecificLocalTime(0, &system_time, &local_time); if (result == FALSE) throw Exception("SystemTimeToTzSpecificLocalTime failed"); DateTime datetime; datetime.timezone = local_timezone; datetime.year = local_time.wYear; datetime.month = local_time.wMonth; datetime.day = local_time.wDay; datetime.hour = local_time.wHour; datetime.minute = local_time.wMinute; datetime.seconds = local_time.wSecond; datetime.nanoseconds = local_time.wMilliseconds * 1000000; datetime.nanoseconds += (nanoseconds % 1000000); return datetime; #else time_t unix_ticks; unix_ticks = year - 1970; unix_ticks *= 365; // Days in a year // Count number of leap years int current_year = year; int num_leaps = 0; for (int cnt=1970; cnt < current_year; cnt++) { if ( ( ((cnt % 4) == 0) && ((cnt % 100 !=0)) ) || ((cnt % 400)==0) ) { num_leaps++; } } unix_ticks += num_leaps; // Extra days switch (month) { case (12): unix_ticks += 30; // Nov case (11): unix_ticks += 31; // Oct case (10): unix_ticks += 30; // Sep case (9): unix_ticks += 31; // Aug case (8): unix_ticks += 31; // Jul case (7): unix_ticks += 30; // Jun case (6): unix_ticks += 31; // May case (5): unix_ticks += 30; // Apr case (4): unix_ticks += 31; // Mar case (3): unix_ticks += 28; // Feb if ( ( ((current_year % 4) == 0) && ((current_year % 100 !=0)) ) || ((current_year % 400)==0) ) { unix_ticks++; // Leap year } case (2): unix_ticks += 31; // Jan break; } unix_ticks += day - 1; unix_ticks *= 24; // Hours in day unix_ticks += hour; unix_ticks *= 60; // Minutes per hour unix_ticks += minute; unix_ticks *= 60; // Seconds per minute unix_ticks += seconds; tm tm_local; memset(&tm_local, 0, sizeof(tm)); tm *result = localtime_r(&unix_ticks, &tm_local); if (result == nullptr) throw Exception("localtime_r failed"); DateTime datetime; datetime.timezone = local_timezone; datetime.year = result->tm_year+1900; datetime.month = result->tm_mon+1; datetime.day = result->tm_mday; datetime.hour = result->tm_hour; datetime.minute = result->tm_min; datetime.seconds = result->tm_sec; datetime.nanoseconds = nanoseconds; return datetime; #endif } }
DateTime &DateTime::add_years(int years) { throw_if_null(); year += years; return *this; }
unsigned char DateTime::get_minutes() const { throw_if_null(); return minute; }
unsigned short DateTime::get_year() const { throw_if_null(); return year; }
/* void DateTime::add_hours(int hours) { throw_if_null(); } void DateTime::add_minutes(int minutes) { throw_if_null(); } void DateTime::add_seconds(int seconds) { throw_if_null(); } void DateTime::add_nanoseconds(int nanoseconds) { throw_if_null(); } */ std::string DateTime::to_long_date_string() const { throw_if_null(); throw Exception("DateTime::to_long_date_string() not implemented"); }
unsigned char DateTime::get_month() const { throw_if_null(); return month; }
unsigned char DateTime::get_hour() const { throw_if_null(); return hour; }
unsigned char DateTime::get_seconds() const { throw_if_null(); return seconds; }
DateTime DateTime::to_utc() const { throw_if_null(); if (timezone == utc_timezone) { return *this; } else { #ifdef WIN32 SYSTEMTIME local_time; SYSTEMTIME system_time; local_time.wYear = year; local_time.wMonth = month; local_time.wDay = day; local_time.wHour = hour; local_time.wMinute = minute; local_time.wSecond = seconds; local_time.wMilliseconds = nanoseconds / 1000000; #if _MSC_VER BOOL result = TzSpecificLocalTimeToSystemTime(0, &local_time, &system_time); #else BOOL result = SystemTimeToTzSpecificLocalTime(0, &system_time, &local_time); #endif if (result == FALSE) throw Exception("TzSpecificLocalTimeToSystemTime failed"); DateTime datetime; datetime.timezone = utc_timezone; datetime.year = system_time.wYear; datetime.month = system_time.wMonth; datetime.day = system_time.wDay; datetime.hour = system_time.wHour; datetime.minute = system_time.wMinute; datetime.seconds = system_time.wSecond; datetime.nanoseconds = system_time.wMilliseconds * 1000000; datetime.nanoseconds += (nanoseconds % 1000000); return datetime; #else tm tm_local; memset(&tm_local, 0, sizeof(tm)); tm_local.tm_year = year-1900; tm_local.tm_mon = month-1; tm_local.tm_mday = day; tm_local.tm_hour = hour; tm_local.tm_min = minute; tm_local.tm_sec = seconds; tm_local.tm_isdst = -1; time_t unix_ticks = mktime(&tm_local); if (unix_ticks == -1) throw Exception("mktime failed"); memset(&tm_local, 0, sizeof(tm)); tm *result = gmtime_r(&unix_ticks, &tm_local); if (result == nullptr) throw Exception("gmtime_r failed"); DateTime datetime; datetime.timezone = utc_timezone; datetime.year = result->tm_year+1900; datetime.month = result->tm_mon+1; datetime.day = result->tm_mday; datetime.hour = result->tm_hour; datetime.minute = result->tm_min; datetime.seconds = result->tm_sec; datetime.nanoseconds = nanoseconds; return datetime; #endif } }
byte64 DateTime::to_ticks() const { throw_if_null(); if (timezone == local_timezone) { return to_utc().to_ticks(); } else { #ifdef WIN32 SYSTEMTIME system_time; FILETIME file_time; system_time.wYear = year; system_time.wMonth = month; system_time.wDay = day; system_time.wHour = hour; system_time.wMinute = minute; system_time.wSecond = seconds; system_time.wMilliseconds = nanoseconds / 1000000; BOOL result = SystemTimeToFileTime(&system_time, &file_time); if (result == FALSE) throw Exception("SystemTimeToFileTime failed"); byte64 ticks = (((byte64)file_time.dwHighDateTime) << 32) + file_time.dwLowDateTime; ticks += (nanoseconds % 1000000)/100; return ticks; #else byte64 ticks; ticks = year - 1601; ticks *= 365; // Days in a year // Count number of leap years int current_year = year; int num_leaps = 0; for (int cnt=1601; cnt < current_year; cnt++) { if ( ( ((cnt % 4) == 0) && ((cnt % 100 !=0)) ) || ((cnt % 400)==0) ) { num_leaps++; } } ticks += num_leaps; // Extra days switch (month) { case (12): ticks += 30; // Nov case (11): ticks += 31; // Oct case (10): ticks += 30; // Sep case (9): ticks += 31; // Aug case (8): ticks += 31; // Jul case (7): ticks += 30; // Jun case (6): ticks += 31; // May case (5): ticks += 30; // Apr case (4): ticks += 31; // Mar case (3): ticks += 28; // Feb if ( ( ((current_year % 4) == 0) && ((current_year % 100 !=0)) ) || ((current_year % 400)==0) ) { ticks++; // Leap year } case (2): ticks += 31; // Jan break; } ticks += day - 1; ticks *= 24; // Hours in day ticks += hour; ticks *= 60; // Minutes per hour ticks += minute; ticks *= 60; // Seconds per minute ticks += seconds; ticks *= 10000000; // To ticks ticks += nanoseconds / 100; return ticks; #endif } }
GraphicContext& DisplayWindow::get_gc() const { throw_if_null(); return impl->provider->get_gc(); }
DateTime::TimeZone DateTime::get_timezone() const { throw_if_null(); return timezone; }
unsigned int DateTime::get_nanoseconds() const { throw_if_null(); return nanoseconds; }
DateTime &DateTime::add_days(int days) { throw_if_null(); if (days > 0) { while (days > 0) { int days_in_month = DateTime::get_days_in_month(month, year); int days_left_in_current_month = days_in_month - day; if (days <= days_left_in_current_month) { day += days; break; } days -= (days_left_in_current_month + 1); day = 1; month++; if (month > 12) { year++; month = 1; } } } else { days *= -1; while (days > 0) { int days_in_month = DateTime::get_days_in_month(month, year); int prev_month = month-1; int prev_month_year = year; if (prev_month < 1) { prev_month = 12; prev_month_year--; } int days_left_in_current_month = day; if (days < days_left_in_current_month) { day -= days; break; } int days_in_prev_month = DateTime::get_days_in_month(prev_month, prev_month_year); days -= days_left_in_current_month; day = days_in_prev_month; month--; if (month < 1) { year--; month = 12; } } } /* int daynum = get_day_number(); daynum += days; set_date_from_daynumber(daynum); */ return *this; }
unsigned char DateTime::get_day() const { throw_if_null(); return day; }
InputContext DisplayWindow::get_ic() const { throw_if_null(); return impl->provider->get_ic(); }
IODeviceProvider *IODevice::get_provider() { throw_if_null(); return impl->provider; }