Looking for another way.

I am looking for a better way of doing the same thing as the code below does.

Note: I am using Visual Studio 2010 but I would like to keep the code standard c++. Which is why the the first line is there.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string GetCurrTime(void);
string IntToStr(int);

int main(void)
{
	cout << "The current time is " << GetCurrTime() << ".\n\n";

	system("Pause");
}

string GetCurrTime(void)
{
	/*
	** The following gets the number of seconds since January 1, 1970,
	** and then converts it into a sturcture that holds the current time
	** in a more readable format.
	*/

	time_t RawTime;
	struct tm* FTime;

	time(&RawTime);
	FTime = localtime(&RawTime);

	/*
	** The Following takes the integer values and converts them to "string" for
	** easier storge.
	*/

	string hour = IntToStr(FTime->tm_hour);
	string min = IntToStr(FTime->tm_min);
	string sec = IntToStr(FTime->tm_sec);

	/*
	** The Following stores and prints out the time.
	*/

	string CT = hour + ":" + min + ":" + sec;

	return CT;
}

string IntToStr(int X)
{
	char Temp[3];

	itoa(X, Temp, 10);

	if(X < 10)
	{
		Temp[2] = 0;
		Temp[1] = Temp[0];
		Temp[0] = '0';
	}

	string Str = Temp;

	return Str;
}


Basically is going to be a clock with that shows the month, day, year, time, am or pm, and have the option for 12 or 24 time.
Last edited on
If you're prepared to use boost, investigate boost::posix_time
I'll check it out.

If anyone else has an idea please post.
Topic archived. No new replies allowed.