On 3/24/2016 11:54 AM, Scott Lurndal wrote:
> Robbie Hatley <see.m...@for.my.address> writes:
>>
>> I wrote a couple of functions in C++ to print out human-readable
>> local time and date strings (should be PDT in AM/PM format if
>> executed in my area) if given current time in time_t format from
>> time() in the <ctime> header. To test the functions I put them
>> in a test program (see below).
>>
>
> Frankly, I'd just use 'strftime'.
>
> char buf[MAXTIMESTRINGSIZE];
> struct tm *tm = ::localtime(&Seconds1970);
> size_t diag = ::strftime(buf, sizeof(buf), "%Y/%m/%d %h:%M %p %Z", tm);
> if (diag != 0) {
> return std::string(buf);
> } else {
> throw "Buffer overflow";
> }
>
> 2016/03/24 11:52 AM PDT
Firstly, my primary reason for posting here was to see why
g++ would allow a std::string to be initalized to 0. Hence the
title of my post.
As for your code, while I will look into strftime() to refresh
my knowledge of how it works, that's not the approach I took
for my time & date functions. I was more interested in getting
*away* from fixed-size manually-allocated arrays of char*,
and going to std::string as much as possible, especially since
these functions return std::string and will be in my personal
library and used by various C++ programs I write.
My implementation looks more like THIS:
====== BEGIN File /rhe/src/test/time-test.cpp ==================
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <ctime>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::setfill;
using std::setw;
// Get local date from time_t time:
std::string GetDate
(
time_t Seconds1970, // seconds since 00:00:00UTC on Jan 1, 1970
int Format // date format: 0=micro, 1=short, 2=long
)
{
struct tm * TP = NULL;
std::ostringstream OS = std::ostringstream ();
TP = localtime(&Seconds1970);
if (NULL == TP)
{
cerr << "ERROR in GetDate(): localtime returned NULL pointer." << endl;
exit(666);
}
if (0 == Format) // if user wants micro date format
{
OS << TP->tm_year + 1900 << "-"
<< setw(2) << setfill('0') << TP->tm_mon + 1 << "-"
<< setw(2) << setfill('0') << TP->tm_mday;
}
else if (1 == Format) // if user wants short date format
{
const char * ShortDays[7] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const char * ShortMonths[12] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
OS << ShortDays[TP->tm_wday] << " "
<< ShortMonths[TP->tm_mon] << " "
<< setw(2) << setfill('0') << TP->tm_mday << ", "
<< TP->tm_year + 1900;
}
else // For any other value of Format, return full-length date:
{
const char * LongMonths[12] =
{"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
const char * LongDays[7] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
OS << LongDays[TP->tm_wday] << " "
<< LongMonths[TP->tm_mon] << " "
<< setw(2) << setfill('0') << TP->tm_mday << ", "
<< TP->tm_year + 1900;
}
return OS.str();
}
// Get local time from time_t time:
std::string GetTime
(
time_t Seconds1970, // seconds since 00:00:00UTC on Jan 1, 1970
int Format // time format: 0=leading zero, 1=NO leading zero
)
{
struct tm * TP = NULL;
int Hour = 0;
std::string Meridien = std::string ();
std::ostringstream OS = std::ostringstream ();
TP = localtime(&Seconds1970);
if (NULL == TP)
{
cerr << "ERROR in GetTime(): localtime returned NULL pointer." << endl;
exit(666);
}
Hour = (TP->tm_hour + 11) % 12 + 1;
Meridien = (TP->tm_hour < 12) ? "AM" : "PM";
OS << setw(2) << setfill(0==Format ? '0' : ' ') << Hour << ":"
<< setw(2) << setfill('0') << TP->tm_min << ":"
<< setw(2) << setfill('0') << TP->tm_sec << Meridien;
return OS.str();
}
int main (void)
{
int i = 0;
time_t Time = 0;
std::string MicroDate = "";
std::string ShortDate = "";
std::string LongDate = "";
std::string TimeString = "";
Time = std::time(NULL);
for ( i = 0 ; i < 50 ; ++i )
{
MicroDate = GetDate(Time, 0);
ShortDate = GetDate(Time, 1);
LongDate = GetDate(Time, 2);
TimeString = GetTime(Time, 0);
cout << "MicroDate = " << MicroDate << endl;
cout << "ShortDate = " << ShortDate << endl;
cout << "LongDate = " << LongDate << endl;
cout << "TimeString = " << TimeString << endl;
Time += 3600;
cout << endl;
}
return 0;
}
====== END File /rhe/src/test/time-test.cpp ==================