Getting current time and date
I have been through the help files in builder but am getting no were with this,
I need to get the current system time and date at the click of a button and assign the result to a variable.
the assigning and making it happen on the click is not a problem,
it is just the functions needed to get the actuall time and date i cant track down,
Anyone point me in the right direction here?
thanks
Keith
[445 byte] By [
KeithD] at [2007-11-19 22:48:00]

# 1 Re: Getting current time and date
have a look on this . And same time i Will suggest your that a lots of Article are already present on the code guru for this Please search them and go ahead.
struct tm *newtime;
time_t aclock;
time( &aclock ); // Get time in seconds
newtime = localtime( &aclock ); // Convert time to struct tm form
char *strCTime = new char[255]; // Comment :Delete This char Pointer
strCTime = asctime(newtime);
//Now Do What ever you want to do with strCTime
delete [] strCTime ;
Thanx
# 2 Re: Getting current time and date
The string returned by asctime is static. You allocated memory, then lost the pointer to that memory, and then tried to use operator delete [] on static memory. Instead, you can use strcpy.
struct tm *newtime;
time_t aclock;
time( &aclock ); // Get time in seconds
newtime = localtime( &aclock ); // Convert time to struct tm form
char *strCTime = new char[255]; // Comment :Delete This char Pointer
strcpy(strCTime, asctime(newtime));
//Now Do What ever you want to do with strCTime
delete [] strCTime ;
# 3 Re: Getting current time and date
Did you want to point something to me. Please go ahead. and actually what's the problem here. Please let me know
Thanx
# 4 Re: Getting current time and date
Please don't take a wierd sarcastic angry tone if that's what that is (I can't tell :(). I just pointed out that the way you were doing things was incorrect. Your code:
char *strCTime = new char[255];
strCTime = asctime(newtime);
delete [] strCTime ;
Allocates memory, then loses the pointer to the allocated memory (memory leak) by reassigning it to a pointer to static memory returned by asctime(..), and then you try to delete the static memory. The simple solution, instead of reassigning the pointer, is to strcpy.
char *strCTime = new char[255];
strcpy(strCTime, asctime(newtime));
delete [] strCTime ;
No memory leak, no crash from delete[]'ing static memory.
# 5 Re: Getting current time and date
Please don't take a wierd sarcastic angry tone if that's what that is (I can't tell :(). I just pointed out that the way you were doing things was incorrect. Your code:
char *strCTime = new char[255];
strCTime = asctime(newtime);
delete [] strCTime ;
Allocates memory, then loses the pointer to the allocated memory (memory leak) by reassigning it to a pointer to static memory returned by asctime(..), and then you try to delete the static memory. The simple solution, instead of reassigning the pointer, is to strcpy.
char *strCTime = new char[255];
strcpy(strCTime, asctime(newtime));
delete [] strCTime ;
No memory leak, no crash from delete[]'ing static memory.
Are you sure That I am deleting static memory in my Program Please run the code on your system and let me know.And very politaly i asked you that where you find wired tone. anyway check out codes ones. and if really some problem is thr Please let me know.
Thanx
# 6 Re: Getting current time and date
just to encapsulate things and avoid dynamic memory check out this class
#ifndef __TIME_STAMP_H__
#define __TIME_STAMP_H__
#include <ctime>
#include <tchar.h>
#include <string>
#include <IOSTREAM>
namespace SYSTEM_AND_CPP_UTILITIES
{
const unsigned int STAMP_TYPE_UTC = 0;
const unsigned int STAMP_TYPE_LOCAL = 1;
class TIME_STAMP
{
private:
time_t Time_;
unsigned int StampType_;
public:
TIME_STAMP(const unsigned int stamp_type = STAMP_TYPE_UTC):
Time_(::time(0)),
StampType_(stamp_type)
{
}
TIME_STAMP(const time_t a_time, const unsigned int stamp_type = STAMP_TYPE_UTC):
Time_(a_time),
StampType_(stamp_type)
{
}
time_t GetTimeT() const {return Time_;}
void ToStream(std::ostream& os) const
{
std::string temp;
ToString(temp);
if('\n' == temp[temp.length() -1])
temp[temp.length() -1] = '\t';
else
temp += '\t';
os << temp;
}
void ToStream(std::wostream& os) const
{
std::wstring temp;
ToString(temp);
if(L'\n' == temp[temp.length() -1])
temp[temp.length() -1] = L'\t';
else
temp += L'\t';
os << temp;
}
void ToString(std::string& time_str) const //DEFAULT TO UTC STAMP
{
tm* tmstruct = 0;
if(STAMP_TYPE_LOCAL == StampType_)
tmstruct = localtime(&Time_);
else
tmstruct = gmtime(&Time_);
//ERROR CHECK
if(0 == tmstruct)
{
time_str = "ERROR IN TIME_STAMP";
return;
}
time_str = asctime(tmstruct);
}
void ToString(std::wstring& time_str) const //DEFAULT TO UTC STAMP
{
tm* tmstruct = 0;
if(STAMP_TYPE_LOCAL == StampType_)
tmstruct = localtime(&Time_);
else
tmstruct = gmtime(&Time_);
//ERROR CHECK
if(0 == tmstruct)
{
time_str = L"ERROR IN TIME_STAMP";
return;
}
time_str = _wasctime(tmstruct);
}
};
}//namespace SYSTEM_AND_CPP_UTILITIES
__inline std::basic_ostream<_TCHAR>& operator<<(std::basic_ostream<_TCHAR>& os, const SYSTEM_AND_CPP_UTILITIES::TIME_STAMP& ts)
{
ts.ToStream(os);
return os;
}
#endif //#ifndef __TIME_STAMP_H__
# 7 Re: Getting current time and date
hi
for date
textbox1.text=Format(DateTime.Now, "dd-MMMM-yyyy")
for time
Format(DateTime.Now, "hh:mm tt")
By rajesh_vbalan