C++ Converting a time string to seconds from the epoch

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

I have a string with the following format:

2010-11-04T23:23:01Z

The Z indicates that the time is UTC.
I would rather store this as a epoch time to make comparison easy.

What is the recomended method for doing this?

Currently (after a quck search) the simplist algorithm is:

1:  2: Use mktime() to convert struct_tm to epoch time.  // Problem here is that mktime uses local time not UTC time. 

回答1:

This is ISO8601 format. You can use strptime function to parse it with %FT%T%z argument. It is not a part of the C++ Standard though you can use open source implementation of it (this, for instance).



回答2:

Using C++11 functionality we can now use streams to parse times:

The iomanip std::get_time will convert a string based on a set of format parameters and convert them into a struct tz object.

You can then use std::mktime() to convert this into an epoch value.

#include  #include  #include  #include   int main() {     std::tm t = {};     std::istringstream ss("2010-11-04T23:23:01Z");      if (ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%S"))     {         std::cout 


回答3:

You can use a function such as strptime to convert a string to a struct tm, instead of parsing it manually.



回答4:

It's not an exact dup but you will find @Cubbi's answer from here useful, I wager. This specifically assumes UTC input.

Boost also support direct conversion from ISO 8601 via boost::posix_time::from_iso_string which calls boost::date_time::parse_iso_time, here again you would just strip the trailing 'Z' and treat the TZ as implicit UTC.

#include  #include   namespace bt = boost::posix_time;  const std::locale formats[] = { std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")), std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")), std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")), std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))}; const size_t formats_n = sizeof(formats)/sizeof(formats[0]);  std::time_t pt_to_time_t(const bt::ptime& pt) {     bt::ptime timet_start(boost::gregorian::date(1970,1,1));     bt::time_duration diff = pt - timet_start;     return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;  } void seconds_from_epoch(const std::string& s) {     bt::ptime pt;     for(size_t i=0; i> pt;         if(pt != bt::ptime()) break;     }     std::cout 


回答5:

Problem here is that mktime uses local time not UTC time.

Linux provides timegm which is what you want (i.e. mktime for UTC time).

Here is my solution, which I forced to only accept "Zulu" (Z timezone). Note that strptime doesn't actually seem to parse the time zone correctly, even though glib seems to have some support for that. That is why I just throw an exception if the string doesn't end in 'Z'.

static double EpochTime(const std::string& iso8601Time) {     struct tm t;     if (iso8601Time.back() != 'Z') throw PBException("Non Zulu 8601 timezone not supported");     char* ptr = strptime(iso8601Time.c_str(), "%FT%T", &t);     if( ptr == nullptr)     {         throw PBException("strptime failed, can't parse " + iso8601Time);     }     double t2 = timegm(&t); // UTC     if (*ptr)     {         double fraction = atof(ptr);         t2 += fraction;     }     return t2; } 


回答6:

New answer to an old question. Rationale for new answer: In case you want to use types to solve a problem like this.

In addition to C++11/C++14, you'll need this free, open source date/time library:

#include "tz.h" #include  #include   int main() {     std::istringstream is("2010-11-04T23:23:01Z");     is.exceptions(std::ios::failbit);     date::sys_seconds tp;     date::parse(is, "%FT%TZ", tp);     std::cout 

This program outputs:

seconds from epoch is 1288912981s 

If the parse fails in any way, an exception will be thrown. If you would rather not throw exceptions, don't is.exceptions(std::ios::failbit);, but instead check for is.fail().



回答7:

You could utilize the boost::date_time and write a small manual parser (probably regexp-based) for your strings.



回答8:

Problem here is that mktime uses local time not UTC time.

How about just computing the time difference between UTC and local time, then adding it to the value returned by mktime?

time_t local = time(NULL),        utc   = mktime(gmtime(&local)); int    diff  = utc - local; 


回答9:

What's wrong with strptime() ?

And on Linux, you even get the 'seconds east of UTC' field relieving you from any need to parse:

#define _XOPEN_SOURCE #include  #include   int main(void) {      const char *timestr = "2010-11-04T23:23:01Z";      struct tm t;     strptime(timestr, "%Y-%m-%dT%H:%M:%SZ", &t);      char buf[128];     strftime(buf, sizeof(buf), "%d %b %Y %H:%M:%S", &t);      std::cout  " 

which for me yields

/tmp$ g++ -o my my.cpp  /tmp$ ./my 2010-11-04T23:23:01Z -> 04 Nov 2010 23:23:01 Seconds east of UTC 140085769590024 


回答10:

X/Open provides a global timezone variable which indicates the number of seconds that local time is behind UTC. You can use this to adjust the output of mktime():

#define _XOPEN_SOURCE #include  #include   /* 2010-11-04T23:23:01Z */ time_t zulu_time(const char *time_str) {     struct tm tm = { 0 };      if (!strptime(time_str, "%Y-%m-%dT%H:%M:%SZ", &tm))         return (time_t)-1;      return mktime(&tm) - timezone; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!