- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从服务器端(正在运行 .NET 应用程序)获取 uint64_t 值到用标准 C++ 编写的应用程序(必须在 Windows 和 Linux 上运行)。
此数字代表 Windows 文件时间 - 又名自 1601-01-01 00:00:00 UTC 纪元以来的 100 纳秒间隔。
我需要在我的应用程序中返回时间的字符串表示形式,精度为纳秒(这是我从服务器获得的值的精度),因此我必须使用 chrono 库。
由于C++的纪元是0001年1月1日到1970年1月1日,我首先需要计算从1970年到1601年的偏移量,并从我从服务器获得的数字中减去它。为了做到这一点,我首先必须将从服务器获得的值表示为 chrono::time_point,并以 100 纳秒的间隔计算纪元 1601-01-01 00:00:00 UTC,以便它和值我得到的是相同的规模。
在我获得 addjustedTimeFromServer - 这是我从服务器获得的值减去(以 chrono::time_point 形式)偏移量之后,我需要将其转换为 std::time_t 以便以精确度提取该值秒,然后从 chrono::time_point 中我需要提取fractional_seconds,这将为我提供纳秒的精度,然后我将它们连接到表示时间的字符串。
这是我的代码。它没有满足我的需要:
using FileTime = duration<int64_t, ratio<1, 10000000>>;
struct std::tm tm;
//create time point for epoch of Windows Filetime (1601-01-01 00:00:00 UTC))
std::istringstream ss("1601-01-01 00:00:00");
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
std::time_t tt = mktime(&tm);
std::chrono::system_clock::time_point offset =std::chrono::system_clock::from_time_t(tt);
//convert the offset into 100-nanosecond intervals scale
auto offset_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(offset);
auto offset_100ns = FileTime(offset_ns.time_since_epoch());
//substract the offset from i so now it starts from 1970 like the epoch of C++
auto iDuration = FileTime(static_cast<int64_t>(i));
//auto iDuration_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(iDuration); //doesn't compile - but that's the idea of what i want to do in this line
std::chrono::system_clock::time_point adjustedTime = iDuration/*iDuration_ns*/ - offset /*-offset_100ns*/; //the commented out parts are what i think is the correct thing to do (scale wise) but they don't compile
//convert the time_point into the string representation i need (extract the regular time, up to seconds, with time_t and the nanosecond part with ns.count())
nanoseconds ns = duration_cast<nanoseconds>(adjustedTime.time_since_epoch());
seconds s = duration_cast<seconds>(ns);
std::time_t t = s.count();
std::size_t fractional_seconds = ns.count() % 10000000;
std::cout << std::ctime(&t) << std::endl;
std::cout << fractional_seconds << std::endl;
代码不起作用,我不知道如何修复它。第一个问题(甚至在所有比例转换问题之前)是 mktime(&tm) 给了我一个不正确的值。由于 tm 表示 C++ 纪元之前的值,因此 mktime(&tm) 返回 -1。我需要以某种方式克服它,因为我必须计算 .NET Filetime 纪元的 time_point (1601-01-01 00:00:00 UTC),以便从我从服务器获得的值中减去它。
我将感谢有关此问题和整个程序的帮助。
P.S 我只是在这段代码中打印,但在最终版本中,我会将两个部分连接到同一个字符串(ctime(&t) 给出的部分和fractional_seconds 给出的部分)
最佳答案
Here is code to do this它实际上是由 MSVC std::lib 团队的一名成员 (Billy O'Neal) 捐赠给该网站的。
此处重复:
// filetime_duration has the same layout as FILETIME; 100ns intervals
using filetime_duration = duration<int64_t, ratio<1, 10'000'000>>;
// January 1, 1601 (NT epoch) - January 1, 1970 (Unix epoch):
constexpr duration<int64_t> nt_to_unix_epoch{INT64_C(-11644473600)};
system_clock::time_point
FILETIME_to_system_clock(FILETIME fileTime)
{
const filetime_duration asDuration{static_cast<int64_t>(
(static_cast<uint64_t>(fileTime.dwHighDateTime) << 32)
| fileTime.dwLowDateTime)};
const auto withUnixEpoch = asDuration + nt_to_unix_epoch;
return system_clock::time_point{
duration_cast<system_clock::duration>(withUnixEpoch)};
}
这会转换为 system_clock::time_point
,在 Linux 上具有纳秒精度,在 Windows 上具有 100 纳秒精度。
使用my date/time library ,可以轻松地将 system_clock::time_point
格式化为您想要的任何格式的完全精度。
此外,这里没有使用 Windows FILETIME
结构:
#include "date.h"
#include <string>
#include <iostream>
std::chrono::system_clock::time_point
FILETIME_to_system_clock(std::uint64_t fileTime)
{
using namespace std;
using namespace std::chrono;
// filetime_duration has the same layout as FILETIME; 100ns intervals
using filetime_duration =duration<int64_t, ratio<1, 10000000>>;
// January 1, 1601 (NT epoch) - January 1, 1970 (Unix epoch):
constexpr duration<int64_t> nt_to_unix_epoch{INT64_C(-11644473600)};
const filetime_duration asDuration{static_cast<int64_t>(fileTime)};
const auto withUnixEpoch = asDuration + nt_to_unix_epoch;
return system_clock::time_point{
duration_cast<system_clock::duration>(withUnixEpoch)};
}
int
main()
{
std::string s = date::format("%F %T", FILETIME_to_system_clock(131400356659154460));
std::cout << s << '\n';
}
这只是我的输出:
2017-05-23 17:54:25.915446
请注意,这仅达到微秒精度。在 Linux 上,这将格式化为纳秒精度,因为 system_clock::time_point
在该平台上具有纳秒精度。
如果不是这种情况,您可以像这样强制使用纳秒精度:
using namespace std::chrono;
std::string s = date::format("%F %T",
time_point_cast<nanoseconds>(FILETIME_to_system_clock(131400356659154460)));
我的输出:
2017-05-23 17:54:25.915446000
在此更新中,输出 chrono::time_point
的精度与 Window 的 FILETIME 相同:100-ns 精度:
#include "date.h"
#include <string>
#include <iostream>
using filetime_duration = std::chrono::duration<std::int64_t, std::ratio<1, 10000000>>;
using FileTime = std::chrono::time_point<std::chrono::system_clock, filetime_duration>;
// or more simply:
// using FileTime = date::sys_time<filetime_duration>;
FileTime
FILETIME_to_system_clock(std::uint64_t fileTime)
{
using namespace std;
using namespace std::chrono;
// filetime_duration has the same layout as FILETIME; 100ns intervals
// January 1, 1601 (NT epoch) - January 1, 1970 (Unix epoch):
constexpr seconds nt_to_unix_epoch{-11644473600};
const filetime_duration asDuration{static_cast<int64_t>(fileTime)};
return FileTime{asDuration + nt_to_unix_epoch};
}
int
main()
{
using namespace std::chrono;
std::string s = date::format("%F %T", FILETIME_to_system_clock(131400356659154461));
std::cout << s << '\n';
}
输出:
2017-05-23 17:54:25.9154461
关于c++ - 在 C++ 中将时间从一种环境转换为另一种环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44141231/
这个问题在这里已经有了答案: How to initialize var? (11 个答案) 关闭 8 年前。 我想给一个变量赋初值 null,并在下一个 if-else block 中赋值,但是编
我正在使用 TypeScript 3.8 编写 JS 和 TS 混合的代码。我写了以下行: export * as Easing from './easing'; 应该是 fair game在 Typ
我需要将 R 代码中的“/”更改为“\”。我有这样的事情: tmp <- paste(getwd(),"tmp.xls",sep="/") 所以我的 tmp是 c:/Study/tmp.xls 我希望
我有个问题。例如我有这个: id truth count 1 1 1 2 1 2 3 0 0 4 1 1 5 1 2 6 1
我正在尝试使用“IN”和“=”来查找一些 bean。我目前正在使用此代码: $ids = array(1,2,3,4); $user = 1; $things = R::find( 'thing'
是否可以在 Xcode 中部署到其他人的手机上?我没有 iPhone,但我想测试我在 friend 手机上制作的应用程序。在我支付 99 美元之前,我想确保这不会造成麻烦。 谢谢。 最佳答案 不会有任
我试图得到一个非常大的数字(超过 unsigned long long int )。所以我把它作为一个字符串,然后一个数字一个数字地转换成整数并使用它。 #include #include int
我在 Rust 中有 C 语言库的绑定(bind),但它们并不完整。 在 C 代码中,我定义了一个简化的宏,如下所示: #define MY_MACROS1(PTR) (((my_struct1
我正在努力解决这个问题。 http://jsfiddle.net/yhcqfy44/ 动画应该自动相对于 滚动到顶部每次出现滚动条时的高度。 我已经写了这个,但没有运气: var hheight =
我正在处理一个将数字作为字符串返回的 JSON API。例如 "12" ,但是,该字段值也可以是非数字的,例如:"-" . 我已将 JSON 数据解析为映射,我想将此字段提取为 elixir 中的整数
我正在尝试编写一个类,将.wav文件转换为.aiff文件作为项目的一部分。 我遇到了几个库Alvas.Audio(http://alvas.net/alvas.audio,overview.aspx)
我想在 Lucene 中将像“New York”这样的“复合词”索引为单个术语,而不是像“new”、“york”那样。这样,如果有人搜索“new place”,则包含“new york”的文档将不会匹
我希望这个解释能让我更好地了解使用宏的优点。 最佳答案 在函数中,所有参数在调用之前都会被评估。 这意味着 or 作为函数不能是惰性的,而宏可以将 or 重写为 if 语句,该语句仅在以下情况下计算分
我有一些看起来像这样的 XML foo ]]> (注意 > 登录 "> foo")和 XSLT 样式表 当我运行xsltproc stylesheet.xs
当我尝试将 Any 转换为 List 时,如下面的示例所示,我得到“Unchecked cast: Any!”到列表'警告。有没有解决此类问题的方法? val x: List = objectOfTy
我正在使用 Python 开发一个简单的爬虫。目的是创建一个 sitemap.xml。(你可以在这里找到真正的 alpha 版本:http://code.google.com/p/sitemappy/
我想知道在 VBScript 中是否可以在多行中中断 If 语句。喜欢: If (UCase(Trim(objSheet.Cells(i, a).Value)) = "YES") Or _ (UCas
for (String item : someList) { System.out.println(item); } 使用“do while”是否等效? 谢谢。 最佳答案 如果列表为空,f
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Split string with delimiters in C 在 C 中将“,”分隔的列表拆分为数组的最佳方法
我有一个如下所示的字符数组: [0, 10, 20, 30, 670] 如何将此字符串转换为整数数组? 这是我的数组 int i=0; size_t dim = 1; char* array = (c
我是一名优秀的程序员,十分优秀!