I have a string like 2013-05-29T21:19:48Z. I'd like to convert it to the number of seconds since 1 January 1970 (the UNIX epoch), so that I can save it using just 4 bytes (or maybe 5 bytes, to avoid the year 2038 problem). How can I do that in a portable way? (My code has to run both on Linux and Windows.)
I can get the date parts out of the string, but I don't know how to figure out the number of seconds. I tried looking at the documentation of date and time utilities in C++, but I didn't find anything.
Here is the working code
string s{"2019-08-22T10:55:23.000Z"};
std::tm t{};
std::istringstream ss(s);
ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%S");
if (ss.fail()) {
throw std::runtime_error{"failed to parse time string"};
}
std::time_t time_stamp = mktime(&t);
use std::get_time if you want the c++ way - but both other options are also valid. strptime will ignore the Z at the end - and the T can be accomodated by format string %Y-%m-%dT%H:%M:%s - but you could also just put the Z at the end.
Take a look at strptime(). For a Windows alternative, see this question.
You could use boost date_time ore more specific ptime.
Use ptime time_from_string(std::string) to init your time and long total_seconds() to get the seconds of the duration.
Related
I need to get the current system time in Unix UTC format and convert it into a CString. I am trying to do this like this
CTime time = CTime::GetCurrentTime();
CString string = time.FormatGmt(L"%d");
code is running, but 'string' variable contains a wrong value, it should contain something like '1011173512', i.e. time in seconds since 1970. Does anyone has any clue why???
The function CTime::FormatGmt uses the same formatting rules as strftime.
https://msdn.microsoft.com/en-us/library/fe06s4ak.aspx
In particular, %d means "Day of month as decimal number (01 – 31)".
I suspect that you want a time_t or similar object. I suggest you want the GetTime method, which returns a _time64_t.
https://msdn.microsoft.com/en-us/library/3s7y67z7.aspx
So:
CTime time = CTime::GetCurrentTime();
CString string; string.Format(L"%lld", time.GetTime());
I´m using C++ under Linux and I confess that after reading so many posts on the theme I´m litte confused around time_t, struct tm, time, ctime and strftime.
What I need to do is to get the current system date/time, convert it to UTC and store in database. Later on I need to retrieve that date/time and print on screen in a human readable format.
I´m using Oracle (TIMESTAMP), mySQL (DATETIME) and Sqlite3 (has no datetime field, stored either on a INT or a VARCHAR()).
The most important is that I need to store date/time with current time zone considering the DST time, as when I get back the result I can recover the original time zone of the DST for correct information display.
Something like:
// Get current time
std::time_t now = std::time(0);
// Convert it to Oracle ???
? oracletime = ?;
// Convert it to mySQl ???
? mysqltime = ?;
// store it on Sqlite3
int sqlite3time = now;
// print the time
std::cout << now << std::endl;
What would be the correct conversion steps here ?
Thanks for helping.
I have a unix time stamp as follows
char timestamp[100];
strcpy(timestamp,"701729943");
time_t timeval=ctime(timestamp);
printf("Time %s",timeval);
If the check the value of the timestamp in the online unix time convertor it shows 27th march 1992, but if the check the program's output it shows feb 25,1996. How to rectify this?
You're using ctime the wrong way around: it expects a pointer to a time_t and returns a string, whereas you're passing it a string and expect it to return a time_t. Does your compiler not warn you about that?
Anyway, it is meant to be used this way:
time_t timeval = 701729943;
printf("Time %s", ctime(&timeval));
If you only have the UNIX timestamp as a string, use strtoul or atoi to so to make a time_t from it, then do this.
I want to format a date and time as a string using the format:
20130630-03:11:45.862
I can do most of this by using strftime, however there is no clear way to achieve fractional seconds on the end.
My current code is:
time_t rawtime;
time(&rawtime);
tm* timeinfo = localtime(&rawtime);
char buffer[80];
strftime(buffer, 80, "%G%m%d-%I:%M:%S", timeinfo);
This produces the value without the fractional seconds part.
However ultimately I just want to have a string version of the date in this format, and don't care what API it takes.
I'm using g++ on Linux in case it's relevant.
If you don't care about the API, you could use boost::date_time and it's time_facet.
Short example so far:
// setup facet and zone
// this facet should result like your desired format
std::string facet="%Y%m%d-%H:%M:%s";
std::string zone="UTC+00";
// create a facet
boost::local_time::local_time_facet *time_facet;
time_facet = new boost::local_time::local_time_facet;
// create a stream and imbue the facet
std::stringstream stream(std::stringstream::in | std::stringstream::out);
stream.imbue(std::locale(stream.getloc(), time_facet));
// create zone
boost::local_time::time_zone_ptr time_zone;
time_zone.reset(new boost::local_time::posix_time_zone(zone));
// write local from calculated zone in the given facet to stream
stream << boost::local_time::local_microsec_clock::local_time(time_zone);
// now you can get the string from stream
std::string my_time = stream.str();
This example is maybe incomplete, because I copied some code out of mine, but I hope you got the point.
With the facet, you can setup your format. The %s (small s with, big S without fractial) setup seconds with fractial. You can read this in the documentation facet format.
The timezone is for calculating your local machine time to the right zone.
I have inherited some MFC C++ code (it's an ActiveX OCX control running on a Windows Mobile 6.5 device) and I need to acquire the system date and time and append it as part of an existing string which gets passed via the com port to another device.
I can get the system date and time, but I can not figure out how to convert that into a string so that I can append it (via strcat.)
I've found a number of different answers on Google and Bing for what at first glance seemed like such a simple problem... :( but I don't know enough MFC C++ to adapt any of it to my needs. Any help would be greatly appreciated.
CTime t = CTime::GetCurrentTime();
CString s = t.Format( "%A, %B %d, %Y" );
char * str = (LPCTSTR) s;
Note, I believe that str is only valid while s is in scope. Probably should copy it off somewhere if you need it to be around after s is destroyed. If you are passing it to strcat() you're probably OK.
In MFC the following code is for current date in MMDDYYYY format.
CTime t = CTime::GetCurrentTime();
CString strDate = t.Format("%m%d%Y");