问题
libical seems to only accept the Olsen city name to look up timezones in its database. What I've got that's portable is a struct tm which has a GMT offset and the shorthand 3-4 letter code (EST/EDT etc) but there's no function that accepts that in libical.
I have a really lousy way where I scan the tz list from libical trying to match the tznames, and that will probably work, but I am well aware this is a bad idea. But I haven't found any other way of getting the local timezone into a libical icaltimetype.
Am I missing something simple?
And by portable I mean windows, osx and linux.
This is a c program I'm writing. But I see now that tz offset is not posix, so that won't do.
回答1:
So I think the answer is that there is no answer. Nothing portable anyway.
So I have separate build paths for linux and osx, and I haven't gotten to windows yet..
The linux implementation just reads from /etc/timezone (more on that on How do I find the current system timezone?)
and the osx implementation does this.
CFTimeZoneRef tzlocal = CFTimeZoneCopyDefault();
CFStringRef me = CFTimeZoneGetName(tzlocal);
NSString *local = (__bridge NSString *)me;
const char *tzname = [local cStringUsingEncoding:NSUTF8StringEncoding];
log.debug("Looking up timezone for %s", tzname);
// this is a built-in timezone so we don't have to free it.
local_timezone = icaltimezone_get_builtin_timezone(tzname);
Not portable but at least it is correct.
来源:https://stackoverflow.com/questions/36948149/is-there-a-portable-way-to-get-the-local-system-timezone-into-a-libical-icaltime