0

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.

stu
  • 8,461
  • 18
  • 74
  • 112
  • You really don't want to match on abbreviation. Consider that `CST` has 5 different meanings, `IST` has 3 different meanings, etc. Also, you should tag your question with a language ([tag:C], I assume). And it's unclear what inputs you actually have because [struct `tm`](http://www.cplusplus.com/reference/ctime/tm/) doesn't have any offset from GMT. Please clarify. – Matt Johnson-Pint Apr 30 '16 at 04:24
  • 1
    mine does: https://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html But you're right this is not platform independant. – stu May 01 '16 at 10:33
  • I knew matching on abbreviation was a bad idea, but I wanted to get something working to test with so I went with that for now. This is why I'm asking this question. – stu May 01 '16 at 10:38
  • Ok, so I'm looking at [what little docs there are for libical](https://raw.githubusercontent.com/libical/libical/master/doc/UsingLibical.txt), and in 5.3.1 it describes the `icaltimetype`, which only has `is_utc` to distinguish between local and utc time, there's no other time zone information in this structure, so I don't understand your question on how you want to get local time zone into an `icaltimetype`. Perhaps you just want to load the local *time*, not the zone? If so, I think you could use one of the functions in 5.3.2 of the same doc, such as `icaltime_from_timet`. – Matt Johnson-Pint May 01 '16 at 18:57
  • icaltimetype has a const icaltimezone *zone; in it. You can set it when creating an icaltimetype when you call icaltime_from_timet_with_zone – stu May 02 '16 at 13:02
  • I'm starting to think the better way to go is just do everything in utc, although at some point I'm going to have to display the time and then I'll have to covert to the local timezone, but maybe I can do that with the clib functions by getting a time_t out of libical first. – stu May 02 '16 at 13:04
  • Also see [gregjesl | brutezone](https://github.com/gregjesl/brutezone) on GitHub. – jww Jul 05 '19 at 05:45

1 Answers1

0

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.

Community
  • 1
  • 1
stu
  • 8,461
  • 18
  • 74
  • 112