Skip to content

Instantly share code, notes, and snippets.

@AntonBikineev
Last active January 23, 2019 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonBikineev/f838cee4c997db91dc84a0ad8f9369a5 to your computer and use it in GitHub Desktop.
Save AntonBikineev/f838cee4c997db91dc84a0ad8f9369a5 to your computer and use it in GitHub Desktop.
// 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();
}
bikineev@bikineev-OptiPlex-7040:~/git/v8/out/release-perf$ perf stat ./d8 test.js
Performance counter stats for './d8 test.js':
9352,901636 task-clock (msec) # 1,001 CPUs utilized
1 774 context-switches # 0,190 K/sec
35 cpu-migrations # 0,004 K/sec
3 857 page-faults # 0,412 K/sec
34 412 867 769 cycles # 3,679 GHz
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
104 841 056 197 instructions # 3,05 insns per cycle
17 071 710 498 branches # 1825,285 M/sec
30 876 662 branch-misses # 0,18% of all branches
9,341967976 seconds time elapsed
bikineev@bikineev-OptiPlex-7040:~/git/v8/out/release-perf$ perf stat ./d8 test.js
Performance counter stats for './d8 test.js':
16384,025031 task-clock (msec) # 1,001 CPUs utilized
1 789 context-switches # 0,109 K/sec
44 cpu-migrations # 0,003 K/sec
3 854 page-faults # 0,235 K/sec
61 220 099 717 cycles # 3,737 GHz
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
155 779 362 573 instructions # 2,54 insns per cycle
27 703 189 915 branches # 1690,866 M/sec
47 186 522 branch-misses # 0,17% of all branches
16,373651309 seconds time elapsed
(function f() { var s; for (var i = 0; i < 10000000; i++) { s = new Date().toString() } return s })();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment