time and time formatting problem

closed account (iw0XoG1T)
Two problems:
1.) The date that I am getting is:
Sun Dec 1 08:51:44 1935

2.) After the date it is inserting a new line that I do not want.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;

string get_local_time()
//function to get local time
{
    time_t rawtime;
    struct tm * timeinfo;
    timeinfo = localtime(&rawtime);
    return asctime(timeinfo);
}

void record_error(string message)
//opens or creates an error log
{
    ofstream error_log("error_log.txt",ios_base::app);
    error_log << get_local_time()
        << message << endl;
}

int main()
//The purpose of this program is to create an error routine
//that can be used with non-console programs.
try
{
    throw runtime_error("lets see");
    return 0;
}
catch(exception& e)
{
    record_error(e.what());
    return 1;
}
catch(...)
{
    return 2;
}
Where is "rawtime" initialized?
closed account (iw0XoG1T)
I could not figure out what this function did so I ignored it--guess it initializes it.
time ( &rawtime );

Thanks

any idea how I should strip the newline?
Don't output the newline (endl).
closed account (iw0XoG1T)
this is the output:
Mon Feb 15 13:08:11 2010
lets see


this is what I want:
Mon Feb 15 13:08:11 2010 lets see


if I am doing something stupid I don't see it.
closed account (iw0XoG1T)
this works, but there seems like there should be an easier way.

1
2
3
    timeinfo = localtime(&rawtime);
    string temp = asctime(timeinfo);
    return temp.substr(0,temp.size()-1);
I never realized that asctime() adds a newline, but sure enough: http://www.cplusplus.com/reference/clibrary/ctime/asctime/

strftime() lets you format it as you wish: http://www.cplusplus.com/reference/clibrary/ctime/strftime/
Topic archived. No new replies allowed.