在C中将包含本地时间的字符串转换为UTC

发布于 2024-08-11 07:38:54 字数 512 浏览 6 评论 0原文

我有一个包含本地日期/时间的字符串,我需要将其转换为 time_t 值(以 UTC 为单位) - 我一直在尝试这样做:

char* date = "2009/09/01/00";
struct tm cal = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
strptime(date, "%Y/%m/%d/%H", &cal);
time_t t = mktime(&cal);

但是我返回的 time_t 值是我期望的值,如果该字符串是被解析为 UTC 而不是本地时间。也许我误解了 strptime 应该做什么,但在 9 月 1 日我的时区(英国),我们使用 BST(即 UTC + 1 小时),所以我希望最终得到的值比 UTC 早 1 小时。

有没有办法将字符串解释为本地时间,自动考虑到该日期有效的 UTC 偏移量?请注意,我需要 time_t 值而不是 struct tm,在上面的示例中,我希望 time_t 值对应于 2009-09-01 01:00:00 GMT

I have a string containing a local date/time and I need to convert it to a time_t value (in UTC) - I've been trying this:

char* date = "2009/09/01/00";
struct tm cal = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
strptime(date, "%Y/%m/%d/%H", &cal);
time_t t = mktime(&cal);

but the time_t value I get back is the value that I would expect if the string was being parsed as UTC not local time. Maybe I have misunderstood what strptime is supposed to do, but in my timezone (UK) on the 1st September we are using BST (ie UTC + 1 hour) so I would expect the value I end up with to be 1 hour ahead of UTC.

Is there a way to interpret the string as localtime, automatically taking into account the UTC offset that would have been in effect on that date? Note that I need the time_t value not a struct tm, in the example above I want the time_t value to correspond to 2009-09-01 01:00:00 GMT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-08-18 07:38:54

您可以使用 mktime 来解释本地时区的 struct tm 。执行此操作时,请小心设置 tm_isdst 标志。 0 代表夏季,1 代表冬季,-1 则让 mktime 计算出来。下面是一些示例代码:

void main()
{
    char* date = "2009/09/01/00";
    struct tm cal = {};
    // Read string into struct tm
    strptime(date, "%Y/%m/%d/%H", &cal);
    // Tell mktime to figure out the daylight saving time
    cal.tm_isdst = -1;
    printf("%20s: %s", "Before mktime", asctime(&cal));
    // Convert struct tm to time_t
    time_t t = mktime(&cal);
    // Convert time_t to localtime
    struct tm localcal = *localtime(&t);
    printf("%20s: %s", "Local time", asctime(&localcal));
    printf("%20s: %i\n", "Local DST", localcal.tm_isdst);
    // Convert time_t to GMT
    struct tm gmcal = *gmtime(&t);
    printf("%20s: %s", "GM time", asctime(&gmcal));
    printf("%20s: %i\n", "GM DST", gmcal.tm_isdst);
}

打印结果(我住在 GMT+1,现在是冬天):

   Before mktime: Tue Sep  1 00:00:00 2009
      Local time: Tue Sep  1 00:00:00 2009
       Local DST: 1
         GM time: Mon Aug 31 22:00:00 2009
          GM DST: 0

看起来 mktime 根据当前夏令时转换 9 月的日期。现在是十一月,所以实际上有一个小时的休息时间。我还没有找到纠正这个问题的方法。

You can use mktime to interpret a struct tm in the local timezone. When you do so, be careful to set the tm_isdst flag. It's 0 for summertime, 1 for wintertime, and to -1 to have mktime figure it out. Here's some example code:

void main()
{
    char* date = "2009/09/01/00";
    struct tm cal = {};
    // Read string into struct tm
    strptime(date, "%Y/%m/%d/%H", &cal);
    // Tell mktime to figure out the daylight saving time
    cal.tm_isdst = -1;
    printf("%20s: %s", "Before mktime", asctime(&cal));
    // Convert struct tm to time_t
    time_t t = mktime(&cal);
    // Convert time_t to localtime
    struct tm localcal = *localtime(&t);
    printf("%20s: %s", "Local time", asctime(&localcal));
    printf("%20s: %i\n", "Local DST", localcal.tm_isdst);
    // Convert time_t to GMT
    struct tm gmcal = *gmtime(&t);
    printf("%20s: %s", "GM time", asctime(&gmcal));
    printf("%20s: %i\n", "GM DST", gmcal.tm_isdst);
}

This prints (I live in GMT+1, and it's wintertime now):

   Before mktime: Tue Sep  1 00:00:00 2009
      Local time: Tue Sep  1 00:00:00 2009
       Local DST: 1
         GM time: Mon Aug 31 22:00:00 2009
          GM DST: 0

It looks like mktime converts a date in September based on the current daylight savings time. It's November now, so it's actually one hour off. I haven't found a way to correct that.

深海少女心 2024-08-18 07:38:54

这是我的版本,使用 tm_gmtoff。希望图书馆能够遵守夏令时......

#define _BSD_SOURCE
#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>

int gmtoffset(void) {
  struct tm *dummy;
  time_t t = 0;
  dummy = localtime(&t);
  return dummy->tm_gmtoff; /* _BSD_SOURCE */
}

int main(void) {
  int off;
  const char *date = "2009/09/01/00";
  struct tm cal = {0};
  time_t t;

  off = gmtoffset();

  strptime(date, "%Y/%m/%d/%H", &cal); /* _XOPEN_SOURCE */
  t = mktime(&cal);
  printf("t     --> %s", ctime(&t)); /* ctime includes a final '\n' */
  t -= off;
  printf("t-off --> %s", ctime(&t));
  return 0;
}
$ /usr/bin/gcc ptime.c
$ ./a.out
t     --> Tue Sep  1 01:00:00 2009
t-off --> Tue Sep  1 00:00:00 2009

Here's my version, using tm_gmtoff. Hopefully, the library takes care of daylight savings time ...

#define _BSD_SOURCE
#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>

int gmtoffset(void) {
  struct tm *dummy;
  time_t t = 0;
  dummy = localtime(&t);
  return dummy->tm_gmtoff; /* _BSD_SOURCE */
}

int main(void) {
  int off;
  const char *date = "2009/09/01/00";
  struct tm cal = {0};
  time_t t;

  off = gmtoffset();

  strptime(date, "%Y/%m/%d/%H", &cal); /* _XOPEN_SOURCE */
  t = mktime(&cal);
  printf("t     --> %s", ctime(&t)); /* ctime includes a final '\n' */
  t -= off;
  printf("t-off --> %s", ctime(&t));
  return 0;
}
$ /usr/bin/gcc ptime.c
$ ./a.out
t     --> Tue Sep  1 01:00:00 2009
t-off --> Tue Sep  1 00:00:00 2009
伏妖词 2024-08-18 07:38:54

我想我现在已经破解了它,感谢 Andomar - 这段代码满足了我的需要,并且无论当前的 DST 状态如何,它似乎都可以工作(我更改了 PC 上的时钟来检查这一点):

#include <time.h>
#include <assert.h>

time_t parseLocalDate(char* date){
    struct tm cal = {0, 0, 0, 0, 0, 0, 0, 0, -1, 0, NULL};
    strptime(date, "%Y/%m/%d/%H", &cal);
    return mktime(&cal);
}

int main(int argc, char *argv[]){
 // DST is effect, Local Time = GMT+1
    assert(1251759600 == parseLocalDate("2009/09/01/00")); // Mon, 31 Aug 2009 23:00:00 GMT
    assert(1254351600 == parseLocalDate("2009/10/01/00")); // Wed, 30 Sep 2009 23:00:00 GMT
 // DST not in effect, Local Time = GMT
    assert(1257033600 == parseLocalDate("2009/11/01/00")); // Sun, 01 Nov 2009 00:00:00 GMT
}

I think I've cracked it now, thanks to Andomar - this code does what I need and appears to work regardless of the current DST status (I changed the clock on my PC to check this):

#include <time.h>
#include <assert.h>

time_t parseLocalDate(char* date){
    struct tm cal = {0, 0, 0, 0, 0, 0, 0, 0, -1, 0, NULL};
    strptime(date, "%Y/%m/%d/%H", &cal);
    return mktime(&cal);
}

int main(int argc, char *argv[]){
 // DST is effect, Local Time = GMT+1
    assert(1251759600 == parseLocalDate("2009/09/01/00")); // Mon, 31 Aug 2009 23:00:00 GMT
    assert(1254351600 == parseLocalDate("2009/10/01/00")); // Wed, 30 Sep 2009 23:00:00 GMT
 // DST not in effect, Local Time = GMT
    assert(1257033600 == parseLocalDate("2009/11/01/00")); // Sun, 01 Nov 2009 00:00:00 GMT
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文