【问题标题】:parse "strange" date format解析“奇怪”的日期格式
【发布时间】:2011-01-22 17:48:52
【问题描述】:

您好,我无法以这种格式解析日期:

1295716379

我不知道是什么日期格式。

此字符串的人类可读值为:

22. 1. 2011, 18.12

我也不知道这种格式是牛仔编码器格式还是一些“标准”。

如果可以将顶部的字符串解析为人类可读的格式,例如 C#、Java、C++。

谢谢

【问题讨论】:

    标签: parsing date format


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    这是一个 UNIX 纪元时间戳。

    在 C# 中将其转换为 DateTime 的示例:

    DateTime ToDateTime(int seconds)
    {
        DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return epoch.ToLocalTime().AddSeconds(seconds);
    }
    

    这会将其转换为当地时间。

    【讨论】:

      【解决方案3】:

      已验证,是unix时间戳。

      时间是Sat Jan 22 17:12:59 2011 UTC。

      看起来你有一个本地时间值,你的时区是 UTC+1。

      在 C/C++ 中:

      #define  _USE_32BIT_TIME_T
      #include <stdio.h>
      #include <time.h>
      #include <stdlib.h>
      
      int main()
      {
          int i = atoi("1295716379");
          time_t t = (time_t)i;
          puts(ctime( &t ));
          tm t_tm = *gmtime(&t);
          puts(asctime( &t_tm ));
          return 0;
      }
      

      输出:

      Sun Jan 23 02:12:59 2011
      
      Sat Jan 22 17:12:59 2011
      

      注意gmtime返回UTC时间值,localtime返回本地时间值。

      PS:我生活在 UTC+9 时区

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多