- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要将格式为“2011061411322100”的一些字符串时间转换为 GMT - 我的第一次尝试如下。但是,问题是时间来自另一台PC,是历史时间。所以我没有实时获取时间,所以我不能简单地从我的代码运行的盒子上的本地时间获取 GMT。
问题是,如果我的代码在时间更改期间运行,时间更改将发生在我的盒子上,但不会发生在我获取时间的远程盒子上。但是,我可以随时查询框以获取当前时间。
所以,给出更多细节:
如果在 1 点和 2 点之间发生时间变化(夏令时),我就完蛋了。我的 GMT 转换将中断。我想在 2) 之后,我需要获取当前的 Remote Box 时间,看看是否有 >58 分钟的差异,然后将其应用于转换。但我想不出一个可靠的方法来做到这一点。
string GMTConverter::strToGMT(const string& timeToConvert)
{
// Set time zone from TZ environment variable.
_tzset();
struct tm tmTime;
//2011 06 14 11 32 21 00
// (strToInt is just a wrapper for atoi)
int year = strToint(timeToConvert.substr(0, 4) );
int month = strToint(timeToConvert.substr(4, 2) );
int day = strToint(timeToConvert.substr(6, 2) );
int hour = strToint(timeToConvert.substr(8, 2) );
int min = strToint(timeToConvert.substr(10, 2) );
int sec = strToint(timeToConvert.substr(12, 2) );
cout<<"Time after parsing: "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<min<<":"<<sec<<endl;
// add to tm struct and return
tmTime.tm_hour = hour;
tmTime.tm_min = min;
tmTime.tm_sec = sec;
tmTime.tm_mday = day;
tmTime.tm_mon = (month-1);
tmTime.tm_year = (year - 1900);
cout <<"Time in TM: "<<tmTime.tm_year<<"/"<<tmTime.tm_mon<<"/"<<tmTime.tm_mday<<" "<<tmTime.tm_hour<<":"<<tmTime.tm_min<<":"<<tmTime.tm_sec<<endl;
char currDateTime[64];
// For logging
strftime(currDateTime, 63, "%c", &tmTime);
cout <<"Actual time:"<<currDateTime<<endl;
time_t remotePCTime = mktime( &tmTime );
struct tm *gmt = gmtime( &remotePCTime );
cout << "gmt = " << asctime( gmt ) << endl;
char datebuf_2[12];
char timebuf_2[13];
strftime( datebuf_2, 13, "%Y-%m-%d\0", gmt );
strftime( timebuf_2, 13, "%H:%M:%S\0", gmt );
return string(datebuf_2) + "T" + string(timebuf_2) + "." + string("000");
}
最佳答案
明显可靠的解决方案是使用 UTC(没有夏令时)作为您发送的时间戳。使用任何具有固有歧义的时间系统(每年有一个小时的重叠时间,您可以在不同的时间获得相同的时间戳)将无法采用万无一失的方法,因为信息会丢失。
如果您无法控制远程机器发送的时间格式,您只能尝试根据您已有的信息进行推断,例如,如果结束时间小于开始时间,则加一小时.如果作业花费的时间超过一小时,这又会带来歧义,但至少时间不会倒退。
关于c++ - 将历史时间转换为 GMT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6492016/
背景 之前陆续写过一些和 OpenTelemetry 相关的文章: 实战:如何优雅的从 Skywalking 切换到 OpenTelemetry 实战:如何编写一个 OpenTeleme
我很困惑PSReadLine历史在 Powershell 中跨 session 工作。我可以在 PS 版本 5.1 中看到我以前的命令历史记录自动存储在 %userprofile%\AppData\R
我有一个实体,我正在从面板中保存我们的数据库,您可以在其中执行常规操作(编辑、添加等)。不是很大,大多数时候大约有 1k 行,而且这个数字可能总是在这个左右。该实体有一些与其他实体相关的字段(例如:位
有时有人想直接在环境中更改 crx 中的内容。 这通常是环境不工作状态的原因。而且往往很难找到问题的原因。而且我认为如果 cq5 crx 有审计日志会很有帮助。像这样。 12.12.12 21:03
这个问题与可以在其他问题之一中找到的模式有关here.基本上在数据库中,我存储用户,位置,传感器等。所有这些内容都可以由用户在系统中编辑,并且可以删除。 但是-在编辑或删除项目时,我需要存储旧数据;我
我需要随时跟踪许多项目及其状态。 例子 ItemId Location DateTime State 1 Mall A 2010-02-03 07:00 on
我有这个方法来添加 fragment : public void addFragmentOnTop(Fragment fragment) { getSupportFragmentManager()
我想了解 HTML5 历史对象。这是我开始的一个简单示例。 function addDialog(){ document.getElementById('d').style.
我如何使用 HTML5 history api。我确实通过了https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history
我正在尝试找出在关系数据库中保存表的历史记录/修订的最佳方法。 我进行了一些研究和阅读,但不确定跟踪更改的最佳方式是什么。对于我的主表,我很确定我已经确定了一个修订表,以保持跟踪(见图),但我不确定是
这个问题在这里已经有了答案: Git: discover which commits ever touched a range of lines (6 个答案) 关闭 9 年前。 我一直在研究 gi
我有一个相当复杂的程序(带有 SWIG'ed C++ 代码的 Python,长期运行的服务器),它显示了不断增长的常驻内存使用量。我一直在使用常用的泄漏工具(valgrind、Pythons gc 模
我的 Git 存储库中有一行包含单词“Foo”的数百次提交。 是否有任何方法可以在上次的位置找到它的修订号? 最佳答案 这可以通过 -S 的镐 ( gitlog ) 选项来解决。 git log -
我不小心删除了一个文件(我不是他的创建者)并提交并将其推送到远程。现在我想让 git 取消删除此更改,但是当我使用 git revert #mistaken commit 时,它可以工作,但指责信息指
我使用 spyder 历史 Pane 查看我过去尝试过的命令,但最近我注意到它不会在我键入命令时更新。屏幕截图 1 显示了控制台和历史记录 Pane ,因为您可以看到历史记录中没有显示任何控制台条目。
我的应用程序使用 Camunda 7.7 运行。到目前为止,所有数据都保存在 Camunda 表 (ACT_XXX) 中——它们变得很大。所以现在我想清理表格并配置 Camunda,以便在 14 天后
我在 SVN 上有一个这样组织的旧项目: /一些/子目录/a/trunk/foo /一些/子目录/b/trunk/foo /一些/子目录/c/trunk/foo 我使用GitHub工具git-impo
我有一个通用的工作功能,为此我将使用 GNU Radio 的历史记录功能。在 block 的构造函数中,我调用了 set_history( m )。我以标准方式转换输入缓冲区: const flo
当我加载 php 页面时,我会附加一些数据。例如 MyPage.php?value=something。正如预期的那样,当我使用后退按钮来回移动时,它总是会加载附加的相同数据。我不想那样。我希望在页面
我们有一个相当大的库,我们需要定期将其导入(然后修补)到我们的代码库中。 SVN Book 似乎推荐了一个“vendor branch”方案,我们保留了“vendor drops”的补丁版本。这会起作
我是一名优秀的程序员,十分优秀!