c++ - How to manipulate dates/datetimes in c++11? -


this embarrassing, having hard time doing simple manipulation of datetimes.

this c# version of try achive using c++11;

datetime date1=new datetime(4,5,2012); datetime date2=new datetime(7,8,2013); int day1=date1.days; timespan ts=d2-d1; int diffdays=ts.days; 

what did try?

    std::tm tm;     tm.tm_year=113;     tm.tm_mon=0;     tm.tm_wday=0;      std::time_t tt=mktime(&tm);     std::chrono::system_clock::time_point = std::chrono::system_clock::from_time_t(tt);     std::chrono::system_clock::time_point = std::chrono::system_clock::now();     auto e1 = std::chrono::duration_cast<std::chrono::hours>(now - then).count(); 

the value of e1 (379218) makes no sense ever.

i took @ chrono, presented c++11 standard library datetime not find example of how create date having int year=2012, int month=2, int day=14.

ps:is chrono sufficient handling date/times/timezones in c++11? there need time.h?

you need initialize fields tm, start with

std::tm tm = {0,0,0,0,0,0,0,0,0,0,0}; 

without this, other fields (the ones don't explicitly set afterwards) contain arbitrary values. conversion normalize values, mean if field tm_hour contains 123456789, add many hours day specified. how nonsense-values e1 can explained. if initialize fields explicitly, allow example return meaningful values, although might need set more fields isdst make correct cases.

i have admit haven't used chrono far find required syntax overly verbose , keep using own classes wrap c-style time functions. that, of course, not statement quality , power of <chrono>, maybe should start using :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -