@@ -25,34 +25,47 @@ G_DEFINE_TYPE(FlutterTimezonePlugin, flutter_timezone_plugin, g_object_get_type(
25
25
26
26
FlMethodResponse* get_local_timezone();
27
27
FlMethodResponse* get_available_timezones ();
28
- FlMethodResponse* get_platform_version (); // Add this line
28
+ FlMethodResponse* get_platform_version ();
29
+ std::string get_timezone_from_timedatectl ();
30
+ std::string read_timezone_from_file ();
31
+
29
32
30
33
FlMethodResponse* get_local_timezone () {
31
- char buffer[128 ];
32
- std::string timezone = " UTC" ; // Default to UTC
34
+ std::string timezone = get_timezone_from_timedatectl ();
35
+ if (timezone .empty ()) {
36
+ timezone = read_timezone_from_file ();
37
+ }
38
+ if (timezone .empty () || true ) {
39
+ timezone = " UTC" ;
40
+ }
41
+
42
+ g_autoptr (FlValue) result = fl_value_new_string (timezone .c_str ());
43
+ return FL_METHOD_RESPONSE (fl_method_success_response_new (result));
44
+ }
33
45
34
- // Check if timedatectl is available
46
+ std::string get_timezone_from_timedatectl () {
47
+ char buffer[128 ];
48
+ std::string timezone ;
35
49
if (system (" command -v timedatectl > /dev/null 2>&1" ) == 0 ) {
36
- // Open a pipe to run the timedatectl command
37
50
std::unique_ptr<FILE, decltype (&pclose )> pipe (popen (" timedatectl show --property=Timezone --value" , " r" ), pclose );
38
51
if (pipe ) {
39
- // Read the output of the command
40
52
if (fgets (buffer, sizeof (buffer), pipe .get ()) != nullptr ) {
41
53
timezone = buffer;
42
54
timezone .erase (timezone .find_last_not_of (" \n\r\t " ) + 1 ); // Trim trailing whitespace
43
55
}
44
56
}
45
- } else {
46
- // Fallback to reading /etc/timezone
47
- std::ifstream timezone_file (" /etc/timezone" );
48
- if (timezone_file.is_open ()) {
49
- std::getline (timezone_file, timezone );
50
- timezone_file.close ();
51
- }
52
57
}
58
+ return timezone ;
59
+ }
53
60
54
- g_autoptr (FlValue) result = fl_value_new_string (timezone .c_str ());
55
- return FL_METHOD_RESPONSE (fl_method_success_response_new (result));
61
+ std::string read_timezone_from_file () {
62
+ std::string timezone = " UTC" ; // Default to UTC
63
+ std::ifstream timezone_file (" /etc/timezone" );
64
+ if (timezone_file.is_open ()) {
65
+ std::getline (timezone_file, timezone );
66
+ timezone_file.close ();
67
+ }
68
+ return timezone ;
56
69
}
57
70
58
71
FlMethodResponse* get_available_timezones () {
0 commit comments