将 13 位小数转换为日期时间

Converting 13 decimals to datetime

我有一个关于带 GPS 的汽车的项目。我需要 return 每辆车的开始和结束时刻。

所以我们有:
time_t x, y; 因为后面我会用到它们进行改造。

我有问题。我以这种格式从外部文件数据中读取:

auto1
1439467747492 
auto1
1439467748512

...等 auto1->汽车名称;
1439467747492->汽车的时刻

我试图获得每辆车第一时刻和最后时刻的第一个位置。这是 C++ 中的代码:

 long test = momenti[choice1]/1000;
 time_t x = test;
 cout << "     Momentul initial:\n " << ctime(&x) << endl;
 long test1 = momentf[choice1] / 1000;
 time_t y = test1;
 cout << "     Momentul final:\n " << ctime(&y) << endl;

我收到每辆车的相同日期。类似于 momenti[i]=momentf[i] 我做错了什么?

#include <iostream>
#include <cstring>
#include <time.h>
using namespace std;

int main()
{
    long test = 1439467747492;
    time_t x = test;
    cout << ctime( &x )  << endl;
    return 0;
}

生产

Tue Sep 18 20:15:32 1990

It is not good. According epoch converter we should get this : GMT: Thu, 13 Aug 2015 12:09:07 GMT

以下是如何使用 C++11/14 和使用扩展 C++ <chrono> 库以处理日期的 this free, open source date library 获得此输出。

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

int
main()
{
    using namespace std::chrono;
    using namespace std;
    using namespace date;
    using time_point = std::chrono::time_point<system_clock, milliseconds>;
    auto tp = time_point{1439467747492ms};
    auto dp = floor<days>(tp);
    auto time = make_time(tp - dp);
    auto ymd = year_month_day{dp};
    cout << "GMT: " << weekday{dp} << ", " << ymd.day() << ' ' << ymd.month()
         << ' ' << ymd.year() << ' ' << time  << " GMT\n";
}

输出:

GMT: Thu, 13 Aug 2015 12:09:07.492 GMT

我为了好玩而投入了小数秒,浪费它们似乎是一种耻辱(C lib 不会给你)。如果你真的不想要它们,很容易解决:

    auto time = make_time(floor<seconds>(tp) - dp);

现在输出是:

GMT: Thu, 13 Aug 2015 12:09:07 GMT

上面的 1439467747492ms 你需要 C++14。如果你只有 C++11,你可以代替它:milliseconds{1439467747492}。如果你只有 C++03,那你就落后了 13 年,还在用 ctime。 ;-)

chrono 解决方案将为您提供更高的类型安全性、更大的灵活性和更高的性能。

If i can fix and the latitude and longitude problem would be great lol

如果您可以将纬度和经度转换为 IANA 时区名称(并且有工具可以做到这一点),我有一个 IANA timezone database parser 可以与 <chrono> 和 [= 互操作20=].