C/C++获取本地时间常见方法 - gavanwanggw - 博客园
namespace myf{
typedef struct
{
int year;
int mon;
int day;
int hour;
int min;
int sec;
}DatePackage;
class TimeModule {
public:
TimeModule();
~TimeModule();
std::string GetCurrentOptTimeString();
DatePackage GetCurrnetOptTimePackage();
void DeviceSyncTime(std::string time);
};
}// myf
namespace myf {
TimeModule::TimeModule() {
};
TimeModule::~TimeModule() {
};
std::string TimeModule::GetCurrentOptTimeString()
{
struct timeval now_time;
gettimeofday(&now_time, NULL);
time_t second = now_time.tv_sec;
tm *temp = localtime(&second);
char time_str[32] = {NULL};
sprintf(time_str,"%04d-%02d-%02d% 02d:%02d:%02d",temp->tm_year + 1900,
temp->tm_mon + 1,temp->tm_mday, temp->tm_hour, temp->tm_min, temp->tm_sec);
std::string time_opt_str(time_str);
return time_str;
}
DatePackage TimeModule::GetCurrnetOptTimePackage()
{
struct timeval now_time;
gettimeofday(&now_time, NULL);
time_t second = now_time.tv_sec;
tm *temp = localtime(&second);
char time_str[32] = {NULL};
DatePackage date_opt_package;
date_opt_package.year = temp->tm_year + 1900;
date_opt_package.mon = temp->tm_mon + 1;
date_opt_package.day = temp->tm_mday;
date_opt_package.hour = temp->tm_hour;
date_opt_package.min = temp->tm_min;
date_opt_package.sec = temp->tm_sec;
return date_opt_package;
}
void TimeModule::DeviceSyncTime(std::string time)
{
if (time == "") {
pr_dbg("device boot time is null.n");
}
pr_err("time is %s", time.c_str());
std::string date_sync_cmd = "date -s "" + time + """;
system(date_sync_cmd.c_str());
}
} // myf
void XXX::NoDisturbSetProcess()
{
is_no_disturb_ = false;
while(status() == kThreadRunning)
{
undisturbed_info_ = system_settings_ins_->GetUndisturbedInfo();
if (undisturbed_info_.enable) {
date_package_ = time_module_ptr_->GetCurrnetOptTimePackage();
if (undisturbed_info_.end_day == 0)
{
// 勿扰结束时间在当天
if (is_no_disturb_ == false) {
if (date_package_.hour < undisturbed_info_.end_hour) {
if (date_package_.hour > undisturbed_info_.start_hour) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
} else if (date_package_.hour == undisturbed_info_.start_hour) {
if (date_package_.min >= undisturbed_info_.start_min) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
}
}
} else if (date_package_.hour == undisturbed_info_.end_hour) {
if (date_package_.min < undisturbed_info_.end_min)
{
if (date_package_.hour == undisturbed_info_.start_hour) {
if (date_package_.min >= undisturbed_info_.start_min) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
}
}
}
}
} else {
if (date_package_.hour == undisturbed_info_.end_hour) {
if (date_package_.min == undisturbed_info_.end_min) {
pr_dbg("exit nodisturb mode.n");
is_no_disturb_ = false;
DeviceSoundInit();
}
}
}
} else {
// 勿扰结束时间在第二天
if (is_no_disturb_ == false) {
if (date_package_.hour == undisturbed_info_.start_hour) {
if (date_package_.min >= undisturbed_info_.start_min) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
}
} else if (date_package_.hour > undisturbed_info_.start_hour) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
}
if (date_package_.hour < undisturbed_info_.end_hour) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
} else if (date_package_.hour == undisturbed_info_.end_hour) {
if (date_package_.min < undisturbed_info_.end_min) {
pr_dbg("enter nodisturb mode.n");
is_no_disturb_ = true;
audio_service_ptr_->SetSoundVolume(0);
}
}
} else {
if (date_package_.hour == undisturbed_info_.end_hour) {
if (date_package_.min == undisturbed_info_.end_min) {
pr_dbg("exit nodisturb mode.n");
is_no_disturb_ = false;
DeviceSoundInit();
}
}
}
}
}
sleep(60);
}
}



