posix - Convert Date-Time to Milliseconds - C++ - cross platform -


i want convert string in format of "20160907-05:00:54.123" milliseconds. know strptime not available in windows , want run program in both windows , linux. can't use third party libraries well. can tokenize string , convert it. there more elegant way using strptime so?

given format of string, easy parse follows (although regex or get_time might more elegant):

tm t; t.tm_year = stoi(s.substr(0, 4)); t.tm_mon = stoi(s.substr(4, 2)); t.tm_mday = stoi(s.substr(6, 2)); t.tm_hour = stoi(s.substr(9, 2)); t.tm_min = stoi(s.substr(12, 2)); t.tm_sec = 0; double sec = stod(s.substr(15)); 

finding time since epoch can done mktime:

mktime(&t) + sec * 1000 

note fractional seconds need handled differently - unfortunately, tm has integer seconds.

(see full code here.)


edit

as mine , panagiotis kanavos correctly note in comments, visual c++ apparently supports get_time quite while, , it's shorter (note fractional seconds need handled same way, though).


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -