栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

获取时间 结构体 字符串终结总结

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取时间 结构体 字符串终结总结

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);
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号