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

孤街浪徒 提交于 2019-12-13 14:07:45

问题


I want to convert a string in the format of "20160907-05:00:54.123" into milliseconds. I know that strptime is not available in Windows and I want to run my program in both windows and linux. I can't use third party libraries as well. I can tokenize the string and convert it. But is there a more elegant way like using the strptime to do so?


回答1:


Given the format of your string, it is fairly easy to parse it as follows (although a regex or get_time might be 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 the time since the epoch can be done with mktime:

mktime(&t) + sec * 1000

Note that the fractional seconds need to be handled differently - unfortunately, tm has only integer seconds.

(See the full code here.)


Edit

As Mine and Panagiotis Kanavos correctly note in the comments, Visual C++ apparently supports get_time for quite a while, and it's much shorter with it (note that the fractional seconds need to be handled the same way, though).




回答2:


What about std::sscanf?

#include <iostream>
#include <cstring>

int main() {
    const char *str_time = "20160907-05:00:54.123";
    unsigned int year, month, day, hour, minute, second, miliseconds;

    if (std::sscanf(str_time, "%4u%2u%2u-%2u:%2u:%2u.%3u", &year, &month,
               &day, &hour, &minute, &second,&miliseconds) != 7)
    {
        std::cout << "Parse failed" << std::endl;
    } 
    else
    {
        std::cout << year << month << day << "-" << hour << ":" 
                  << minute << ":" << second << "." << miliseconds
                  << std::endl;
    }
}

Output (ideone): 201697-5:0:54.123.

However, you should make sure the input is valid (for example, day can be in the range of [0,99]).




回答3:


Too bad about no 3rd party libraries, because here is one (MIT license) that is just a single header, runs on linux and Windows, and handles the milliseconds seamlessly:

#include "date.h"
#include <iostream>
#include <sstream>

int
main()
{
    date::sys_time<std::chrono::milliseconds> tp;
    std::istringstream in{"20160907-05:00:54.123"};
    date::parse(in, "%Y%m%d-%T", tp);
    std::cout << tp.time_since_epoch().count() << '\n';
}

This outputs:

1473224454123

Error checking is done for you. The stream will fail() if the date is invalid.

date::sys_time<std::chrono::milliseconds> is a type alias for std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>. I.e. it is from the family of system_clock::time_point, just milliseconds precision.

Fully documented:

https://howardhinnant.github.io/date/date.html

Doesn't get much more elegant than this.



来源:https://stackoverflow.com/questions/39371527/convert-date-time-to-milliseconds-c-cross-platform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!