How do I change the default local time format in C++? - c++

I have a function:
string get_current_time()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return asctime (timeinfo);
}
which returns time in the following format:
Fri Mar 18 11:25:04 2011
How do I change it so that it is returned in the following format?
2011 03-18 11-25-04 Fri
I want to use this for log file names.

As #0A0D suggested, whilst you can't change asctime, you can use strftime to format the data contained in your time_t:
string get_current_time()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
char output[30];
strftime(output, 30, "%Y %m-%d %H-%M-%S %a", timeinfo);
return string(output);
}
(I've also dropped a copy on to IdeOne here.)

asctime() always returns in the format Www Mmm dd hh:mm:ss yyyy . It cannot be changed.
If you want to use a different format, look at strftime().

Related

Convert Date string to unix time stamp

I have a string in the form "20190930_141414" which is the date 2019 09 30 14:14:14. How can I convert this to the unix time stamp in utc?
You can use streams with get_time and mktime to convert it to a time_t object (time since the epoch).
struct std::tm lTM;
std::istringstream lTimestamp( "20190930_141414" );
lTimestamp >> std::get_time = ( &lTM, "%Y%m%d_%H%M%S" );
std::time_t lEpoch = mktime( &lTM );

MongoDB bson_date_t to local time

I appended date to the mongodb like this
bson_append_date(b,"uploadDate",(bson_date_t)1000*time(NULL));
Do remember that this will append "milliseconds since epoch UTC" and saved as 2014-06-27 06:11:56
Now i am reading it out and it is giving milliseconds (1403852029) which is exactly right. Now i want to convert it into local time. I tried to use the localtime function of C++ but did get success as the time returned by mongodb is in int64_t.
if(bson_iterator_type(&it)==BSON_DATE)
bson_date_t date_it = bson_iterator_date( &it );
where bson_date_t is typedef int64_t bson_date_t;. Can anyone tell me how i can get the local time from the milliseconds.
Getting a valid time_t that would work with localtime should be exactly the opposite of what you are doing in the forward conversion:
bson_append_date(b,"uploadDate",(bson_date_t)1000*time(NULL));
To have a workable time_t, you should do following:
time_t rawTime = (time_t)(bson_iterator_date( &it ) / 1000);
struct tm * timeinfo = localtime (&rawTime);
One more method.
bson_date_t date_it = bson_iterator_date( &it );
struct tm* ts;
time_t epoch_time_as_time_t= date_it/1000;
ts=localtime(&epoch_time_as_time_t);
strftime(upload_Date,sizeof(upload_date),"%a %Y-%m-%d %H:%M:%S %Z",ts);

Attempting to convert a time_t data type to have (dd/mm/yyyy) format C++

My question is pretty much in the title. I have a function call with a parameter of type time_t, and I need to initialize a variable to today's date, month, and year, and send it via the argument. For example,
void WebCall(time_t TodaysDate)
Where TodaysDate is the populated variable with the format DD/MM/YYYY with the slashes included. Is this possible? I can't change the data type from time_t to SYSTEMTIME or anything else. This is coded in C++. Any Ideas?
If you mean time_t, you can format it using gmtime and strftime:
time_t TodaysDate= ...;
struct tm * ptm= gmtime(&time);
char buffer[80];
strftime(buffer, 80, "%d/%m/%Y", ptm);
time_t is "unix time" and is the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. As MSN answered you can convert that to a date using gmtime, for most common purposes, UTC is synonymous with GMT. You didn't specify in the question but if you need the local date use localtime instead of gmtime. Here's a function that'll do that for you and return a std::string:
#include <time.h>
#include <string>
std::string time_to_local_date( time_t utc )
{
struct tm *now = localtime(&utc);
char buffer[80];
strftime(buffer, 80, "%d/%m/%Y", now);
return std::string(buffer);
}

simple c++ time program error

I am messing around with the time header and have encountered an error:
invalid conversion from 'tm*' to 'time_t'
You can see the where the error is in the code below all the way at the bottom when I try to use the difftime function. I know im just doing something stupid and illegal but I cant see how I can get around this. If anyone has a solution let me know. Thanks!
The program is supposed to get the current time, take time input from a user, and then find the time difference.
Code:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
struct tm * cur_timeinfo_hold;
int year, month ,day,hour,minute,second;
double dif;
/* prompt user for time to be compared*/
printf ("Enter year: "); scanf ("%d",&year);
printf ("Enter month: "); scanf ("%d",&month);
printf ("Enter day: "); scanf ("%d",&day);
printf ("Enter hour: "); scanf ("%d",&hour);
printf ("Enter minute: "); scanf ("%d",&minute);
printf ("Enter second: "); scanf ("%d",&second);
/* get current timeinfo*/
time ( &rawtime );
timeinfo = localtime ( &rawtime );
/* print it */
printf("The present time is: "); printf(asctime (timeinfo));
/* set current time into a new variable to use for difftime, since timeinfo, will be changed */
cur_timeinfo_hold = timeinfo;
/* modify current timeinfo to the user's choice */
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
timeinfo->tm_hour = hour;
timeinfo->tm_min = minute;
timeinfo->tm_sec = second;
mktime ( timeinfo );
/* and print it */
printf ("time to compare: "); printf(asctime (timeinfo));
/* find time difference */
//dif = difftime (cur_timeinfo_hold,timeinfo); //error: invalid conversion from 'tm*' to 'time_t'
return 0;
}
mktime is a function. It accepts one parameter, a tm pointer, and returns a value, a time_t. It does not transform its argument from one type into another. Therefore, after you call mktime(timeinfo), your timeinfo variable is still a tm pointer.
The difftime function expects to receive two time_t values, so it won't accept cur_timeinfo_hold or timeinfo; they're the wrong type. The first variable's value came from converting rawtime to a tm pointer with localtime, so use that for the first parameter. When you later called mktime(timeinfo), it returned a time_t value, but you ignored the return value. Assign the return value to a variable so you can use it for the second difftime parameter.
time_t info = mktime(timeinfo);
// ...
dif = difftime(rawtime, info);
It's because difftime requires the raw time_t values, not the struct tm structures.
The prototype is:
double difftime(time_t time1, time_t time0);
What you'll need to do is leave the system time as it is (no localtime performed on it) and then properly convert your user-entered information into the equivalent time_t with mktime (or timegm if working with UTC times).
Then you can use difftime to get the difference. Effectively, something like:
time_t base, other;
struct tm tms;
double delta;
time (&base); // basetime is now.
tms.blah = blah; // for all blahs.
other = mktime (&tms); // get time_t for other time.
delta = difftime (other, base); // get difference.

How to concatenate strings in the function m_stream in C++?

I'm writing a logger for my program in C++.
I have this piece of code:
void Log::Write(char* logline)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
m_stream << asctime (timeinfo) << logline << endl;
}
This is a log
Tue Oct 11 13:07:28 2011
I want a different output on a single line like this:
Tue Oct 11 13:07:28 2011 - This is a log
How can I do it?
Thank you!
As it said in docs:
The string result produced by asctime contains exactly 26 characters and has the form Wed Jan 02 02:03:55 1980\n\0
So if you don't want to write line ending symbol you could use use 'write' function specifying exact amount of characters to write like this
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
m_stream.write( asctime (timeinfo), 24 );
m_stream << " - " << logline << std::endl;
Of course in that case you should write comment explaining why '24' is used...
Your problem is asctime():
http://www.cplusplus.com/reference/clibrary/ctime/asctime/
The string is followed by a new-line character ('\n') and the terminating null-character.
As the returned string is a C string, you could replace the \n with a \0:
char * str = asctime(timeinfo);
str[strlen(str) - 1] = '\0';
AS long as you don't call ctime or asctime again, the content won't be overwritten.