timezone.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "timezone.h"
  2. #include <lib/toolbox/args.h>
  3. #include "../../../services/config/config.h"
  4. #include "../../../ui/scene_director.h"
  5. #include "../../cli_helpers.h"
  6. #define TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE "timezone"
  7. void totp_cli_command_timezone_docopt_commands() {
  8. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE ", " TOTP_CLI_COMMAND_TIMEZONE_ALT
  9. " Get or set current timezone\r\n");
  10. }
  11. void totp_cli_command_timezone_docopt_usage() {
  12. TOTP_CLI_PRINTF(
  13. " " TOTP_CLI_COMMAND_NAME
  14. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_TIMEZONE " | " TOTP_CLI_COMMAND_TIMEZONE_ALT) " " DOCOPT_OPTIONAL(
  15. DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE)) "\r\n");
  16. }
  17. void totp_cli_command_timezone_docopt_arguments() {
  18. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE
  19. " Timezone offset in hours to be set\r\n");
  20. }
  21. void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  22. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  23. return;
  24. }
  25. FuriString* temp_str = furi_string_alloc();
  26. if(args_read_string_and_trim(args, temp_str)) {
  27. char* strtof_endptr;
  28. float tz = strtof(furi_string_get_cstr(temp_str), &strtof_endptr);
  29. if(*strtof_endptr == 0 && tz >= -12.75f && tz <= 12.75f) {
  30. TOTP_CLI_LOCK_UI(plugin_state);
  31. plugin_state->timezone_offset = tz;
  32. if(totp_config_file_update_timezone_offset(plugin_state)) {
  33. TOTP_CLI_PRINTF_SUCCESS("Timezone is set to %f\r\n", (double)tz);
  34. } else {
  35. totp_cli_print_error_updating_config_file();
  36. }
  37. TOTP_CLI_UNLOCK_UI(plugin_state);
  38. } else {
  39. TOTP_CLI_PRINTF_ERROR("Invalid timezone offset\r\n");
  40. }
  41. } else {
  42. TOTP_CLI_PRINTF_INFO(
  43. "Current timezone offset is %f\r\n", (double)plugin_state->timezone_offset);
  44. }
  45. furi_string_free(temp_str);
  46. }