From 86f9af1fafd60749ce32373bbfb3bcfeabb5ec4b Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Mon, 4 Nov 2024 13:10:51 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E7=A7=92=E9=92=9F=E6=B5=8B?= =?utf8?q?=E8=AF=95=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/clock/src/MainClockView.cpp | 129 +++++++++--------- 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp b/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp index 39c387a49..72edf7314 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp @@ -34,6 +34,8 @@ extern uint8_t battery_get_voltage(float *voltage); #define TIMEVIEW_UP 0 #define TIMEVIEW_DOWN 1 +#define SECONDTEST 0 +#define TIMEFORMAT 0 std::string Enweekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; std::string Cnweekdays[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; @@ -472,72 +474,75 @@ namespace OHOS } /* 更新时间 */ - void MainClockView::UpdateTime(void) - { - struct tm local_time = {0}; - time_t seconds = time(nullptr); + void MainClockView::UpdateTime(void) { + struct tm local_time = {0}; + time_t seconds = time(nullptr); - if (localtime_r(&seconds, &local_time) == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get localtime error!"); - return; - } + // Get local time + if (localtime_r(&seconds, &local_time) == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get localtime error!"); + return; + } - // 处理小时和分钟 + // Extract hour and minute uint8_t hour = local_time.tm_hour % HOURS_IN_DAY; uint8_t minute = local_time.tm_min % MINUTES_IN_HOUR; - - // 处理12小时制显示 - char timebuf[64] = {0}; - const char *phour = (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) ? (hour < HOUR_FORMAT_12 ? "AM" : "PM") : nullptr; - - if (phour) - { - if (hour > HOUR_FORMAT_12) - hour -= HOUR_FORMAT_12; - sprintf_s(timebuf, sizeof(timebuf), "%s %02d:%02d", phour, hour, minute); - } - else - { - sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute); - } - - // 更新界面上的文本 - if (timerlabel != nullptr) - timerlabel->SetText(timebuf); - - // 更新星期和日期 - std::string weekStr = Cnweekdays[local_time.tm_wday]; - std::string dateStr = std::to_string(local_time.tm_mon + 1) + "月" + - std::to_string(local_time.tm_mday) + "号"; - - dateStr = dateStr + " " + weekStr; - - // if (weekLabel != nullptr) - // weekLabel->SetText(weekStr.c_str()); - if (datelabel != nullptr) - datelabel->SetText(dateStr.c_str()); - - // 更新电量 - if (batterySetting == BATTERY_DISPLAY_NONE) - return; - - UpdateBatteryColor(); - - float voltage = 0.0F; - int power = battery_get_voltage(&voltage); - if (power == power_) - return; - - power_ = power; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress power_:%d", - power_); - if (batteryprogress != nullptr && batterylabel != nullptr) - { - batteryprogress->SetValue(power_); - std::string batteryStr = std::to_string(power_) + "%"; - batterylabel->SetText(batteryStr.c_str()); - } + char timebuf[64] = {0}; + + // Format time string based on defined macros + #if SECONDTEST + uint8_t second = local_time.tm_sec; + // uint16_t millisecond = local_time.tm_sec % 1000; + sprintf(timebuf, "%02d:%02d:%02d", hour, minute, second); + #else + sprintf(timebuf, "%02d:%02d", hour, minute); + #endif + + #if TIMEFORMAT + // Handle 12-hour format display + const char *phour = (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) ? + (hour < HOUR_FORMAT_12 ? "AM" : "PM") : nullptr; + + if (phour) { + if (hour > HOUR_FORMAT_12) + hour -= HOUR_FORMAT_12; + sprintf_s(timebuf, sizeof(timebuf), "%s %02d:%02d", phour, hour, minute); + } else { + sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute); + } + #endif + + // Update display labels + if (timerlabel != nullptr) + timerlabel->SetText(timebuf); + + // Update date and week day + std::string weekStr = Cnweekdays[local_time.tm_wday]; + std::string dateStr = std::to_string(local_time.tm_mon + 1) + "月" + + std::to_string(local_time.tm_mday) + "号 " + weekStr; + + if (datelabel != nullptr) + datelabel->SetText(dateStr.c_str()); + + // Update battery status + if (batterySetting == BATTERY_DISPLAY_NONE) + return; + + UpdateBatteryColor(); + + float voltage = 0.0F; + int power = battery_get_voltage(&voltage); + if (power == power_) + return; + + power_ = power; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress power_:%d", power_); + + if (batteryprogress != nullptr && batterylabel != nullptr) { + batteryprogress->SetValue(power_); + std::string batteryStr = std::to_string(power_) + "%"; + batterylabel->SetText(batteryStr.c_str()); + } } void MainClockView::UpdateBatteryColor(void) -- 2.17.1