Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. // ES6 section 20.3.4.41.1 ToDateString(tv)
  2. std::string ToDateString(double time_val, DateCache* date_cache,
  3.                          ToDateStringMode mode = kDateAndTime) {
  4.   std::stringstream ss;
  5.   ss.sync_with_stdio(false); // to speed up by enabling buffering
  6.   if (std::isnan(time_val)) {
  7.     ss << "Invalid Date";
  8.     return ss.str();
  9.   }
  10.   int64_t time_ms = static_cast<int64_t>(time_val);
  11.   int64_t local_time_ms = date_cache->ToLocal(time_ms);
  12.   int year, month, day, weekday, hour, min, sec, ms;
  13.   date_cache->BreakDownTime(local_time_ms, &year, &month, &day, &weekday, &hour,
  14.                             &min, &sec, &ms);
  15.   int timezone_offset = -date_cache->TimezoneOffset(time_ms);
  16.   int timezone_hour = std::abs(timezone_offset) / 60;
  17.   int timezone_min = std::abs(timezone_offset) % 60;
  18.   const char* local_timezone = date_cache->LocalTimezone(time_ms);
  19.   switch (mode) {
  20.     case kDateOnly:
  21.       ss << kShortWeekDays[weekday] << ' ' << kShortMonths[month] << ' '
  22.           << std::setfill('0') << std::setw(2) << day << ' '
  23.           << std::setfill('0') << std::setw(4) << year;
  24.       return ss.str();
  25.     case kTimeOnly:
  26.       ss << std::setfill('0') << std::setw(2) << hour << ':'
  27.           << std::setfill('0') << std::setw(2) << min << ':'
  28.           << std::setfill('0') << std::setw(2) << sec
  29.           << " GMT" << ((timezone_offset < 0) ? '-' : '+')
  30.           << std::setfill('0') << std::setw(2) << timezone_hour
  31.           << std::setfill('0') << std::setw(2) << timezone_min
  32.           << " (" << local_timezone << ')';
  33.       return ss.str();
  34.     case kDateAndTime:
  35.       ss << kShortWeekDays[weekday] << ' ' << kShortMonths[month] << ' '
  36.           << std::setfill('0') << std::setw(2) << day << ' '
  37.           << std::setfill('0') << std::setw(4) << year << ' '
  38.           << std::setfill('0') << std::setw(2) << hour << ':'
  39.           << std::setfill('0') << std::setw(2) << min << ':'
  40.           << std::setfill('0') << std::setw(2) << sec
  41.           << " GMT" << ((timezone_offset < 0) ? '-' : '+')
  42.           << std::setfill('0') << std::setw(2) << timezone_hour
  43.           << std::setfill('0') << std::setw(2) << timezone_min
  44.           << " (" << local_timezone << ')';
  45.       return ss.str();
  46.   }
  47.   UNREACHABLE();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement