After messing with this a bunch today, I stumbled across 2 ways to get local time. The key was using “local” for the reg string, which I did not find any documentation of, and no comments in the library defining what was possible. Just tried it, and whaazaa.
timezone.region_load("local", context.allocator)
I’m still very open to any advice on better approaches, but this is what I got so far.
1st way
region, region_ok := timezone.region_load("local", context.allocator)
defer timezone.region_destroy(region, context.allocator)
utc, utc_ok := time.time_to_datetime(time.now())
local_datetime, ldt_ok := timezone.datetime_to_tz(utc, region)
t, t_ok := time.datetime_to_time(local_datetime)
utc_offset := region.rrule.has_dst ? region.rrule.dst_offset / 60 : region.rrule.std_offset / 60
stime, stime_ok := time.time_to_rfc3339(t, int(utc_offset), allocator = context.allocator)
defer delete(stime, context.allocator)
fmt.println(stime)
2nd way
region, region_ok := timezone.region_load("local", context.allocator)
defer timezone.region_destroy(region, context.allocator)
utc_offset := region.rrule.has_dst ? region.rrule.dst_offset / 60 : region.rrule.std_offset / 60
now_local := time.time_add(time.now(), time.Duration(utc_offset) * time.Minute)
stime, ok := time.time_to_rfc3339(now_local, int(utc_offset), allocator = context.allocator)
defer delete(stime, context.allocator)
fmt.println(stime)
I then broke them down into the following procedures for future use in formatting what I want. Note the following comments for the local_datetime procedure below.
// The returned date_time will have a reference to the returned tz_region
// tz_region must stay allocated while using date_time
// when done do timezone.region_destroy(tz_region, allocator)
local_datetime :: proc(allocator := context.allocator) -> (date_time: datetime.DateTime, tz_region: ^datetime.TZ_Region, ok: bool) {
tz_region = timezone.region_load("local", allocator) or_return
utc := time.time_to_datetime(time.now()) or_return
date_time, ok = timezone.datetime_to_tz(utc, tz_region)
return
}
local_timestamp_rfc3339 :: proc(allocator := context.allocator) -> (time_stamp: string, ok: bool) {
now, utc_offset := local_now() or_return
return time.time_to_rfc3339(now, int(utc_offset), allocator = allocator)
}
// utc_offset is in minutes
local_now :: proc() -> (now: time.Time, utc_offset: i64, ok: bool) {
region := timezone.region_load("local", context.allocator) or_return
defer timezone.region_destroy(region, context.allocator)
utc_offset = region.rrule.has_dst ? region.rrule.dst_offset / 60 : region.rrule.std_offset / 60
now = time.time_add(time.now(), time.Duration(utc_offset) * time.Minute)
return now, utc_offset, true
}