【问题标题】:Date/Time format conversion to 4-byte long [closed]日期/时间格式转换为 4 字节长 [关闭]
【发布时间】:2015-04-09 15:31:36
【问题描述】:

我正在使用一个输出时间的程序,例如“下午 2:30”,我喜欢在其中添加日期(例如“04/09/2015 14:30”)并最终将其转换为 4 字节长。如果我用 C++ 或 C# 编写代码,我该怎么做?谢谢。

【问题讨论】:

  • 通常 Stack Overflow 不适合转储需求,但 现有代码的特定问题...

标签: c# c++ date datetime time


【解决方案1】:

在 C++14 中,使用 this library,您可以轻松地将其存储为 std::chrono::system_clock::time_point

using namespace date;
using namespace std::chrono;
system_clock::time_point tp = sys_days(apr/9/2015) + 14h + 30min;

system_clock::time_point 可能存储为 8 字节有符号整数,而不是 4 字节整数。如果 4 字节问题真的很重要,您可以像这样创建 system_clock::time_point 的兄弟姐妹:

using TimePoint = time_point<system_clock, duration<int32_t, ratio<60>>>;
TimePoint tp = sys_days(apr/9/2015) + 14h + 30min;

TimePoint 仍可与system_clock::time_point 互操作。

【讨论】:

    【解决方案2】:

    在 C# 中试试这个。

    Date.Add(new TimeSpan(0, 0, 0))

    TimeOfDay.Add(new TimepSpan(0,0,0))

    【讨论】:

      【解决方案3】:

      我想通了。很简单:

      string time = "2:30pm";
      DateTime dt = DateTime.Parse(time).ToUniversalTime();
      DateTime orig = DateTime(1970, 1, 1, 0, 0, 0, 0);
      long ldt = (long)((dt - orig).TotalSeconds);
      

      就是这样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多