Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ES6 section 20.3.4.41.1 ToDateString(tv)
- std::string ToDateString(double time_val, DateCache* date_cache,
- ToDateStringMode mode = kDateAndTime) {
- std::stringstream ss;
- ss.sync_with_stdio(false); // to speed up by enabling buffering
- if (std::isnan(time_val)) {
- ss << "Invalid Date";
- return ss.str();
- }
- int64_t time_ms = static_cast<int64_t>(time_val);
- int64_t local_time_ms = date_cache->ToLocal(time_ms);
- int year, month, day, weekday, hour, min, sec, ms;
- date_cache->BreakDownTime(local_time_ms, &year, &month, &day, &weekday, &hour,
- &min, &sec, &ms);
- int timezone_offset = -date_cache->TimezoneOffset(time_ms);
- int timezone_hour = std::abs(timezone_offset) / 60;
- int timezone_min = std::abs(timezone_offset) % 60;
- const char* local_timezone = date_cache->LocalTimezone(time_ms);
- switch (mode) {
- case kDateOnly:
- ss << kShortWeekDays[weekday] << ' ' << kShortMonths[month] << ' '
- << std::setfill('0') << std::setw(2) << day << ' '
- << std::setfill('0') << std::setw(4) << year;
- return ss.str();
- case kTimeOnly:
- ss << std::setfill('0') << std::setw(2) << hour << ':'
- << std::setfill('0') << std::setw(2) << min << ':'
- << std::setfill('0') << std::setw(2) << sec
- << " GMT" << ((timezone_offset < 0) ? '-' : '+')
- << std::setfill('0') << std::setw(2) << timezone_hour
- << std::setfill('0') << std::setw(2) << timezone_min
- << " (" << local_timezone << ')';
- return ss.str();
- case kDateAndTime:
- ss << kShortWeekDays[weekday] << ' ' << kShortMonths[month] << ' '
- << std::setfill('0') << std::setw(2) << day << ' '
- << std::setfill('0') << std::setw(4) << year << ' '
- << std::setfill('0') << std::setw(2) << hour << ':'
- << std::setfill('0') << std::setw(2) << min << ':'
- << std::setfill('0') << std::setw(2) << sec
- << " GMT" << ((timezone_offset < 0) ? '-' : '+')
- << std::setfill('0') << std::setw(2) << timezone_hour
- << std::setfill('0') << std::setw(2) << timezone_min
- << " (" << local_timezone << ')';
- return ss.str();
- }
- UNREACHABLE();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement