使用与当前不同的语言环境构造日期

时间:2013-06-25 19:14:33

标签: c++ c

我有一个要求,其中日期时间,区域设置和夏令时输入为固定长度字符串,例如YYMMDDHHMMCY,其中下面提供了图例。

  1. YY(年)
  2. MM(月)
  3. DD(天)
  4. HH(小时)
  5. MM(分钟)
  6. 时区(C为中心,P为太平洋,M为山,E为东)
  7. 夏令时(Y如果夏令时生效,其他明智的N)
  8. 我们需要的是能够在指定时区内构建时间,然后在C / C ++中将其转换为本地时区。我们不使用Boost,是否存在可满足要求的现有功能。我知道strptime,在对数据进行一些按摩之后,我可以使用它,但我想知道是否有如上所述的函数可以让我在指定的语言环境中构造一个结构。

1 个答案:

答案 0 :(得分:1)

使用sscanf()strptime()提取大部分字段。时区字符和DST字符需要自己解码。由于您仅使用2位数年份,因此需要定义范围。以下示例使用1970-2069。使用提取的时区字符形成通常的时区名称。在致电mktime()之前,请将TZ设置为时区名称。然后,手持time_t,转换为当地时间。

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

extern time_t mktime_TZ(struct tm *tm, const char *tz);
extern time_t DecodeTimeString_time_t(const char *time_string);

void DecodeTimeString_Local(const char *time_string, struct tm *local) {
  // Various error handling not shown
  time_t t;
  t = DecodeTimeString_time_t(time_string);
  *local = *localtime(&t);
}

time_t DecodeTimeString_time_t(const char *time_string /* YYMMDDHHMMCY */) {
    struct tm tm;
    char Zone, DST;
    int result = sscanf(time_string, "%2d%2d%2d%2d%2d%[CEMP]%[NY]",
        &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &Zone, &DST);
    if (result != 7) {
      ; // handle error
    }
    // Your need to decide how to handle 2 digits years
    // Assume 70-99 is 1970-1999 and 0 to 69 is 2000-2069
    if (tm.tm_year < 70) tm.tm_year += 2000-1900;
    tm.tm_mon--;  // Assume DateString used "01" for January, etc.
    tm.tm_sec = 0;
    tm.tm_isdst = Zone == 'Y';
    const char *TZ;
    switch (Zone) {
      case 'P':  TZ = "PST8PDT"; break;  // Pacific
      case 'M':  TZ = "MST7MDT"; break;  // mountain
      case 'C':  TZ = "CST6CDT"; break;  // central
      case 'E':  TZ = "EST5EDT"; break;  // eastern
    }
    time_t t = mktime_TZ(&tm, TZ);
    return t;
    }

// Form time_t from struct tm given a TZ
time_t mktime_TZ(struct tm *tm, const char *tz) {
  time_t t;
  const char *old_tz = getenv("TZ");
  if (setenv("TZ", tz, 1 /* overwrite */)) {
    return -1; // handle error
  }
  tzset();
  t = mktime(tm);
  if (old_tz) {
    if (setenv("TZ", old_tz, 1 /* overwrite */)) {
      return -1; // handle error
    }
  }
  else {
    if (unsetenv("TZ")) {
      return -1; // handle error
    }
  }
  tzset();
  return t;
}