DATA_TYPE_WEEK_STR, // 星期(string)
DATA_TYPE_DATE_STR, // 日期(string)
DATA_TYPE_WEATHER_STR, // 天气(string)
+ DATA_TYPE_TIMESTR_STR, // 时间(string)
DATA_TYPE_MAX = 0x7fff, // 数据类型的最大值
};
--- /dev/null
+
+
+#ifndef DIAL_MODEL_TimeStr_H
+#define DIAL_MODEL_TimeStr_H
+
+#include "DialBaseModel.h"
+#include <cstdint>
+#include <string>
+
+namespace OHOS {
+class DialModelTimeStr : public DialBaseModel {
+public:
+ DialModelTimeStr() {}
+ virtual ~DialModelTimeStr() {}
+ static DialModelTimeStr *GetInstance() {
+ static DialModelTimeStr instance;
+ return &instance;
+ }
+ bool GetDialTextData(DialDataType &type, std::string *&out,
+ int16_t &strNum) override;
+ void UpdateTimeStr(const std::string dialTimeStr);
+
+private:
+ struct tm tblock_;
+ std::string dialTimeStr_ = "00:00";
+};
+} // namespace OHOS
+#endif
\ No newline at end of file
--- /dev/null
+#include "DialModelTimeStr.h"
+#include "RegisterDialModel.h"
+#include "custom_runtime.h"
+#include "sys/time.h"
+#include "wearable_log.h"
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+#include <string>
+#include <time.h>
+#include <vector>
+
+REGIST_DIAL_MODULE(DialDataType::DATA_TYPE_TIMESTR_STR,
+ OHOS::DialModelTimeStr::GetInstance());
+
+namespace OHOS {
+
+bool DialModelTimeStr::GetDialTextData(DialDataType &type, std::string *&out,
+ int16_t &strNum) {
+ (void)type;
+ /*获取时间*/
+ struct timeval tv;
+ gettimeofday(&tv, NULL); // 获取当前时间
+ localtime_r(&tv.tv_sec, &tblock_);
+
+ // Use ostringstream to format the time with leading zeros
+ std::ostringstream oss;
+ oss << std::setw(2) << std::setfill('0') << tblock_.tm_hour << ":"
+ << std::setw(2) << std::setfill('0') << tblock_.tm_min;
+
+ std::string timeStr = oss.str(); // Get the formatted time string
+
+ if (!timeStr.empty()) {
+ dialTimeStr_ = timeStr;
+ out = &dialTimeStr_;
+ }
+
+ strNum = 1;
+ return true;
+}
+
+void DialModelTimeStr::UpdateTimeStr(const std::string dialTimeStr) {
+ dialTimeStr_.clear();
+ dialTimeStr_ = dialTimeStr; // 更新字符串
+ if (onChangeListener_ != nullptr) {
+ onChangeListener_->OnDialTextDataUpdate(DialDataType::DATA_TYPE_TIMESTR_STR,
+ &dialTimeStr_, 1);
+ }
+}
+} // namespace OHOS
\ No newline at end of file