Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. auto unix_epoch_start = XXX;
  2. auto time = std::chrono::system_clock::now();
  3. auto delta = time - unix_epoc_start;
  4. auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(delta).count();
  5.  
  6. auto unix_timestamp = std::chrono::seconds(std::time(NULL));
  7.  
  8. int unix_timestamp_x_1000 = std::chrono::milliseconds(unix_timestamp).count();
  9.  
  10. std::chrono::steady_clock::now() - unix_timestamp;
  11.  
  12. // 1 Jan 1970 (no time zone)
  13. std::tm c = { 0, 0, 0, 1, 0, 70, 0, 0, -1};
  14.  
  15. // treat it as 1 Jan 1970 (your system's time zone) and get the
  16. // number of seconds since your system's epoch (leap seconds may
  17. // or may not be included)
  18. std::time_t l = std::mktime(&c);
  19.  
  20. // get a calender time for that time_point in UTC. When interpreted
  21. // as UTC this represents the same calendar date and time as the
  22. // original, but if we change the timezone to the system TZ then it
  23. // represents a time offset from the original calendar time by as
  24. // much as UTC differs from the local timezone.
  25. std::tm m = *std::gmtime(&l);
  26.  
  27. // Treat the new calendar time as offset time in the local TZ. Get
  28. // the number of seconds since the system epoch (again, leap seconds
  29. // may or may not be counted).
  30. std::time_t n = std::mktime(&m);
  31.  
  32. l -= (n-l); // subtract the difference
  33.  
  34. auto now = system_clock::now();
  35. day_point today = time_point_cast<days>(now);
  36. system_clock::time_point this_morning = today;
  37.  
  38. day_point unix_epoch = day(1)/jan/1970;
  39. days days_since_epoch = today - unix_epoch;
  40.  
  41. auto s = now - this_morning;
  42.  
  43. auto tz_offset = hours(0);
  44. int unix_timestamp = (days_since_epoch + s + tz_offset) / seconds(1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement