diff options
| author | Marvin W <git@larma.de> | 2026-03-14 13:42:58 +0800 |
|---|---|---|
| committer | Marvin W <git@larma.de> | 2026-04-08 19:02:44 +0200 |
| commit | 64757cbe2572259ad201f4c691aeb545088ae42a (patch) | |
| tree | 6c3de38252417500ab1234687bd413e29dcc4d1b /xmpp-vala/src/module/xep/0082_date_time_profiles.vala | |
| parent | 82da21c1a67d5a50d0a4940b5e3c687929b041c2 (diff) | |
Add implementation for XEP-0202 Entity Time
Diffstat (limited to 'xmpp-vala/src/module/xep/0082_date_time_profiles.vala')
| -rw-r--r-- | xmpp-vala/src/module/xep/0082_date_time_profiles.vala | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/xmpp-vala/src/module/xep/0082_date_time_profiles.vala b/xmpp-vala/src/module/xep/0082_date_time_profiles.vala index 8b40d3ac..3237208e 100644 --- a/xmpp-vala/src/module/xep/0082_date_time_profiles.vala +++ b/xmpp-vala/src/module/xep/0082_date_time_profiles.vala @@ -1,11 +1,40 @@ namespace Xmpp.Xep.DateTimeProfiles { -public DateTime? parse_string(string time_string) { +public DateTime? parse_time(string time_string) { return new DateTime.from_iso8601(time_string, null); } -public string to_datetime(DateTime time) { +public TimeZone? parse_tzd(string tzd) { + if (tzd == "Z") return new TimeZone.utc(); + // We support parsing TZD without positive or negative sign prefix for compatibility + string unprefixed; + if (tzd.length == 5) { + unprefixed = tzd; + } else if (tzd.length == 6 && (tzd.has_prefix("-") || tzd.has_prefix("+"))) { + unprefixed = tzd.substring(1); + } else { + return null; + } + int h_offset = unprefixed.substring(0, 2).to_int(); + int m_offset = unprefixed.substring(3, 2).to_int(); + int offset = h_offset * 60 * 60 + m_offset * 60; + if (tzd.has_prefix("-")) { + offset = -offset; + } + return new TimeZone.offset(offset); +} + +public string format_time(DateTime time) { return time.to_utc().format_iso8601().to_string(); } +public string format_tzd(DateTime time) { + TimeSpan utc_offset = time.get_utc_offset(); + if (utc_offset == 0) return "Z"; + int m_offset = (int) (utc_offset / TimeSpan.MINUTE).abs() % 60; + int h_offset = (int) (utc_offset / TimeSpan.HOUR).abs(); + string prefix = utc_offset < 0 ? "-" : "+"; + return "%s%02d:%02d".printf(prefix, h_offset, m_offset); +} + } |
