Debian Codegraph @

.cargo/vendor/iana-time-zone-haiku/src/implementation.cc

raw
1 #include <cstddef>
2
3 #ifdef __HAIKU__
4
5 #include <cstring>
6
7 #include <Errors.h>
8 #include <LocaleRoster.h>
9 #include <String.h>
10 #include <TimeZone.h>
11
12 extern "C" {
13
14 size_t iana_time_zone_haiku_get_tz(char *buf, size_t buf_size) {
15 try {
16 static_assert(sizeof(char) == sizeof(uint8_t), "Illegal char size");
17
18 if (buf_size == 0) {
19 return 0;
20 }
21
22 // `BLocaleRoster::Default()` returns a reference to a statically allocated object.
23 // https://github.com/haiku/haiku/blob/8f16317/src/kits/locale/LocaleRoster.cpp#L143-L147
24 BLocaleRoster *locale_roster(BLocaleRoster::Default());
25 if (!locale_roster) {
26 return 0;
27 }
28
29 BTimeZone tz(NULL, NULL);
30 if (locale_roster->GetDefaultTimeZone(&tz) != B_OK) {
31 return 0;
32 }
33
34 BString bname(tz.ID());
35 int32_t ilength(bname.Length());
36 if (ilength <= 0) {
37 return 0;
38 }
39
40 size_t length(ilength);
41 if (length > buf_size) {
42 return 0;
43 }
44
45 // BString::String() returns a borrowed string.
46 // https://www.haiku-os.org/docs/api/classBString.html#ae4fe78b06c8e3310093b80305e14ba87
47 const char *sname(bname.String());
48 if (!sname) {
49 return 0;
50 }
51
52 std::memcpy(buf, sname, length);
53 return length;
54 } catch (...) {
55 return 0;
56 }
57 }
58 } // extern "C"
59
60 #else
61
62 extern "C" {
63
64 size_t iana_time_zone_haiku_get_tz(char *buf, size_t buf_size) { return 0; }
65 } // extern "C"
66
67 #endif