|
-
August 5th, 2004, 11:49 PM
#1
how to get now() or time() into a string?
Well, I am saving files, and I would like to save them to a folder named after now) or date() and time() , so when my program starts it makes a string with this in it. The string has to be formatted so windows will allow it to be used as a folder name.
How can i make a string with the contents of Now() or similar in it?
thanks
-
August 6th, 2004, 01:47 AM
#2
Well...I assume that you are referring to the Visual Basic 'Now" function? Anyway...what about the following...
Code:
#include <sstream>
#include <windows.h>
SYSTEMTIME Time;
::GetLocalTime(&Time);
std::stringstream strsName;
strsName << std::setfill('0')
<< "MyLogfile_"
<< Time.wYear
<< std::setw(2) << Time.wMonth
<< std::setw(2) << Time.wDay
<< "_"
<< std::setw(2) << Time.wHour
<< std::setw(2) << Time.wMinute
<< std::setw(2) << Time.wSecond
<< ".log";
-
August 6th, 2004, 02:54 AM
#3
Code:
LPSYSTEMTIME lpSystemTime = NULL;
if(!SetCurrentDirectory(szDir))
{
sprintf(g_szErrorString, "Invalid trail file directory name %s - ", szDir);
return -1;
}
lpSystemTime = new SYSTEMTIME;
GetLocalTime(lpSystemTime);
//Create unique name for trail file depending on current date,
//i.e. AudTrailYYYYMMDDHHMMSS
sprintf(m_szTrailFileName, "%s%s%.4u_%.2u_%.2u_%.2u%.2u_%.2u%s", szDir, "\\AudTrail",
(UINT)lpSystemTime->wYear, (UINT)lpSystemTime->wMonth, (UINT)lpSystemTime->wDay,
(UINT)lpSystemTime->wHour, (UINT)lpSystemTime->wMinute, (UINT)lpSystemTime->wSecond, ".txt");
break;
You can use this code, only difference from Andreas is I use sprintf
Cheers
The most knowledgeable people are those who know that they know nothing.
-
August 6th, 2004, 03:28 AM
#4
If you're using MFC, here's another (easier) way:
Code:
CTime now = CTime ::GetCurrentTime();
CString strNow = now.Format("%Y%m%d%H%M%S");
-
August 8th, 2004, 04:59 PM
#5
Thank you all for the response. What I am looking for is something in this format:
08_08_2004_8_42_PM
or similar, so its easily read, and used to create a directory. I don't use the MFC as I develope under borland, so and stl or vcl way would be very appreciated. Perhapse a function that returns a std::string ?
thanks again
-
August 9th, 2004, 03:35 AM
#6
Code:
#include <sstream>
#include <string>
#include <windows.h>
std::string GetTimestamp()
{
SYSTEMTIME Time;
::GetLocalTime(&Time);
std::stringstream strsTime;
strsTime << std::setfill('0')
<< Time.wYear
<< std::setw(2) << Time.wMonth
<< std::setw(2) << Time.wDay
<< "_"
<< std::setw(2) << Time.wHour
<< std::setw(2) << Time.wMinute
<< std::setw(2) << Time.wSecond
<< ".log";
return strsTime.str();
}
The formatting can be done as you want...
-
August 9th, 2004, 05:07 PM
#7
-
September 29th, 2004, 04:39 AM
#8
Re: how to get now() or time() into a string?
hi gstercken,
Thankyou very much for suggesting such a easier method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|