unsigned long milliseconds_since_epoch =
std::chrono::system_clock::now().time_since_epoch() /
std::chrono::milliseconds(1);
although, especially since you want platform independence, it might be better to replace unsigned long
with a type that's more likely to be large enough:
(unsigned) long long
std::(u)int64_t
std::chrono::milliseconds::rep
auto
To me, this clearly states both that you're risking loss of precision (by analogy with integer division) and that you're leaving the safety of the type system (by dividing by a typed time to give a unitless number). However, as demonstrated in the comments, some people would say that any attempt to move away from type-safety should be accompanied by a deliberate attempt to make the code look dangerous. If you need to deal with people who hold that belief, it might be simpler to use duration_cast
rather than enter into an argument about irrelevant stylistic choices:
unsigned long milliseconds_since_epoch =
std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::system_clock::now().time_since_epoch()).count();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…