Simplest way to get current time in current timezone using boost::date_time?

前端 未结 3 1117
误落风尘
误落风尘 2021-01-04 09:41

If I do date +%H-%M-%S on the commandline (Debian/Lenny), I get a user-friendly (not UTC, not DST-less, the time a normal person has on their wristwatch) time p

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 10:24

    While this is not using boost::date_time it's relatively easy with boost::locale, which is quite more adapted for this task. As your need is simply getting a formatted time from the current locale.

    IMHO boost::date_time should be used when you deal with softwares like gantt/planning computations, were you have alot of date_time arithmetic. But simply for using time and doing some arithmetic on it, you will faster success with boost::locale.

    #include 
    #include 
    
    using namespace boost;
    
    int main(int argc, char **argv) {
       locale::generator gen;
       std::locale::global(gen(""));
    
       locale::date_time now;
       std::cout.imbue(std::locale());       
       std::cout << locale::as::ftime("%H-%M-%S") << now << std::endl;
    
       return 0;
    }
    

    Right now it should output : 15-45-48. :)

提交回复
热议问题