简体   繁体   English

计算自1970/1/1以来经过了多少时间(精确到毫秒)

[英]Calculate how many time (precision to millisecond) elapsed since 1970/1/1

The request is accept a specific time and calculate how many time elapsed since 1970/01/01 00:00:00 Need taking millisecond into account! 该请求接受特定时间,并计算自1970/01/01 00:00:00起经过了多少时间!需要考虑毫秒!

For example, input a date time: 2009/08/06 14:35:19.42 The result show: 124958371942 例如,输入日期时间:2009/08/06 14:35:19.42结果显示:124958371942

As I know, time_t is only take care how many seconds elapsed since 1970/01/01 and struct tm has no millisecond attribute to set 据我所知,time_t只注意从1970/01/01开始经过了几秒钟,而struct tm没有设置毫秒属性。

I try to use mktime and get the wrong result: 1249540519ll 我尝试使用mktime并得到错误的结果:1249540519ll

struct tm t;
t.tm_year = 2009 - 1900;
t.tm_mon = 7;
t.tm_mday = 6;
t.tm_hour = 14;
t.tm_min = 35;
t.tm_sec = 19;

wprintf(L"%ull\n", mktime(&t));

What API should use for this case? 在这种情况下应使用什么API? Thanks! 谢谢!

It appears with 124958371942, OP is counting centiseconds and not milliseconds. 出现与124958371942,OP计数的是毫秒而不是毫秒。 There also is a timezone issue. 还有一个时区问题。 It appears the 1970 timestamp is UTC and the 2009 date may be local. 看来1970年的时间戳记是UTC ,而2009年的日期可能是本地时间。

Various approaches to cope with timezone differences. 各种解决时区差异的方法。

A comon approach is to assume both the 2009/08/06 14:35:19.42 and 1970/01/01 00:00:00 are universal time. 一种通用方法是假定2009/08/06 14:35:19.42和1970/01/01 00:00:00都是通用时间。 Another is that they are both local time. 另一个是他们都是当地时间。 The following assumes both local time. 以下假设两个本地时间。

#include <stdio.h>
#include <time.h>

void tt(void) {

  struct tm t = { 0 }; // important to set _all_ fields

  t.tm_year = 2009 - 1900;
  t.tm_mon = 8 - 1;
  t.tm_mday = 6;
  t.tm_hour = 14;
  t.tm_min = 35;
  t.tm_sec = 19;
  t.tm_isdst = -1;  // Unless known let code determine if it is DST or not.
  time_t t1 = mktime(&t);
  printf("%10lld\n", (long long) t1);

  t.tm_year = 1970 - 1900;
  t.tm_mon = 1 - 1;
  t.tm_mday = 1;
  t.tm_hour = 0;
  t.tm_min = 0;
  t.tm_sec = 0;
  t.tm_isdst = -1;
  time_t t0 = mktime(&t);
  printf("%10lld\n", (long long) t0);

  printf("%lf\n", difftime(t1, t0) * 1000.0);
}

// Of course these are per my timezone value
1249587319
     21600
1249565719000.000000

Some details: 一些细节:

struct tm() may have a sub-seconds field. struct tm() 可能有一个sub-seconds字段。 That is why it is important to zero the entire structure. 因此,将整个结构归零非常重要。 Only 9 fields are specified, others may exist. 仅指定了9个字段,其他字段可能存在。 Two fields, tm_wday and tm_yday do not need to be initialize before calling mktime() , but at least 7 of them should be set including tm_isdst . 在调用mktime()之前,无需初始化两个字段tm_wdaytm_yday ,但是至少应设置其中的7个字段,包括tm_isdst

difftime() is the portable way to subtract timestamps. difftime()是减去时间戳可移植方式。

When printing time_t , the value is typically the integer count of seconds since 1970 without leap seconds. 当打印time_t ,该值通常是自1970年以来没有秒数的整数秒数。 Converting to long long is a reasonable , but not absolute portable method of printing. 转换为long long是一种合理的方法 ,但不是绝对可移植的打印方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM