Is TimeZone.current expected to work with import FoundationEssentials?

I am working on removing Foundation dependency and noticed that my logs are using incorrect TimeZone after I removed the last import. Shouldn't this still work with just FoundationEssentials?

import FoundationEssentials
print(TimeZone.current) // gmt offset 0
import Foundation
print(TimeZone.current) // the correct timezone based on /etc/localtime

Ah, the current implementation needs ICU...

I guess I'll have to fallback to libc:

let dateToFormat = Date.now
var timeZone = TimeZone.current
if timeZone.identifier == "GMT",
	case var now = time_t(dateToFormat.timeIntervalSince1970),
	case var timeinfo = tm(),
	case _ = localtime_r(&now, &timeinfo),
	let actualTimeZone = TimeZone(secondsFromGMT: timeinfo.tm_gmtoff)
{
	timeZone = actualTimeZone
}

I guess adding another fallback here would be a good first issue.

Can you share what platform you're running this code on? Looking at the implementation, TimeZone.current does have the functionality to find your current timezone preferences even without FoundationInternationalization (that's why it compiles without the import, FoundationEssentials offers the property). However, I think the value of your timezone preference may require FoundationInternationalization. I believe just FoundationEssentials processes GMT/UTC-based timezones (ex. GMT+5) but I believe you do need FoundationInternationalization loaded in your process if your preferences specify a named timezone (ex. America/Los_Angeles) since we need ICU to map the timezone names to offsets. @tinaliu does that match your expectations?

I'm running on Linux.

So the app just returns the wrong result, instead of terminating at runtime, or, even better, failing to compile / link? It's quite sad.

1 Like

(If someone wants to get timezone info on Linux other than the current one then the appropriate file in /usr/share/zoneinfo can also be parsed when available, this is the structure of that file: https://linux.die.net/man/5/tzfile )