I'm trying to build an RFC3339 timestamp in C. How do I get the timezone offset?

回眸只為那壹抹淺笑 提交于 2019-12-11 09:07:09

问题


I'm attempting to put together an RFC3339 timestamp which will be used to write a certain entry to a database. That would be formatted as, for example, 2004-10-19 10:23:54+02, where the +02 is the offset in hours from GMT. It's this offset which is proving troublesome - I can't seem to derive this value in C.

Here is the code I'm using. When I try to build, it says the tm struct doesn't have a member named tm_gmtoff:

#include <time.h>
#include <stdio.h>
int main(void)
{
    time_t now = time(NULL);
    struct tm *tm;
    int off_sign;
    int off;
    if ((tm = localtime(&now)) == NULL) {
            return -1;
    }
    off_sign = '+';
    off = (int) tm->tm_gmtoff;
    if (tm->tm_gmtoff < 0) {
            off_sign = '-';
            off = -off;
    }
    printf("%d-%d-%dT%02d:%02d:%02d%c%02d:%02d",
    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
    tm->tm_hour, tm->tm_min, tm->tm_sec,
    off_sign, off / 3600, off % 3600);
    return 0;

}


回答1:


to build an RFC3339 timestamp in C

If a string is the goal, the easy solution is to use strftime().

  time_t now;
  time(&now);
  struct tm *p = localtime(&now);
  char buf[100];
  size_t len = strftime(buf, sizeof buf - 1, "%FT%T%z", p);
  // move last 2 digits
  if (len > 1) {
    char minute[] = { buf[len-2], buf[len-1], '\0' };
    sprintf(buf + len - 2, ":%s", minute);
  }
  printf("\"%s\"\n", buf);

Output

"2018-11-21T13:21:08-06:00"

How do I get the timezone offset?
When I try to build, it says the tm struct doesn't have a member named tm_gmtoff:

The tm_gmtoff is an optional extra member of struct tm. When available, that or some other similar implementation dependent member would be good to use.


With access to strftime() and "%z", as suggeted by @jwdonahue.
"%z" available since C99.

%z is replaced by the offset from UTC in the ISO 8601 format ‘‘−0430’’ (meaning 4 hours 30 minutes behind UTC, west of Greenwich), or by no characters if no time zone is determinable. C11 §7.27.3.5 3

int main(void) {
  time_t now;
  time(&now);
  struct tm *p = localtime(&now);
  char buf[6];
  strftime(buf, sizeof buf, "%z", p);
  int h, m;
  sscanf(buf, "%3d%d", &h, &m);
  if (h < 0)  m = -m;
  printf("Minute difference %d\n", h*60+m);
}

Output for CT

Minute difference -360

Lacking the above choices, code can deduce the timezone offset directly from localtime() and gmttime(): do a struct tm subtraction.

The below takes advantage that a difference in timestamps does not exceed 1 day near January 1st.

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

// Return difference in seconds.
long tz_offset(time_t t) {
  struct tm *p = localtime(&t);
  if (p == NULL) return INT_MIN;
  printf("%s", asctime(p));
  struct tm local = *p;
  p = gmtime(&t);
  if (p == NULL) return INT_MIN;
  printf("%s", asctime(p));
  struct tm gmt = *p;

  int day = local.tm_yday - gmt.tm_yday;
  if (local.tm_year > gmt.tm_year) {
    day = 1;
  } else if (local.tm_year < gmt.tm_year) {
    day = -1;
  }
  int hour = day*24 + (local.tm_hour - gmt.tm_hour);
  if (local.tm_isdst) {
    ; // no adjustment
  }
  long diff = (hour*60L + (local.tm_min - gmt.tm_min))*60 + (local.tm_sec - gmt.tm_sec);
  return diff;
}

int main(void) {
  time_t now;
  time(&now);
  printf("tz offset %g\n", tz_offset(now)/3600.0);

  // Check time 6-months from now, maybe different daylight setting
  struct tm *p = localtime(&now);
  p->tm_mon += 6;
  now = mktime(p);
  printf("tz offset %g\n", tz_offset(now)/3600.0);
  return 0;
}

Sample output for CT

Tue Feb 13 11:40:00 2018
Tue Feb 13 17:40:00 2018
tz offset -6
Mon Aug 13 12:40:00 2018
Mon Aug 13 17:40:00 2018
tz offset -5

To print the timezone offset per RFC3339 given hour, minute:

  // Alway print the sign and leading zero digits
  printf("%+02d:%02d", hours, abs(minute));

See also What's the difference between ISO 8601 and RFC 3339 Date Formats?



来源:https://stackoverflow.com/questions/48771851/im-trying-to-build-an-rfc3339-timestamp-in-c-how-do-i-get-the-timezone-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!