• 0

[C++] getting time and zone


Question

Hi,

I'm trying to see if I can grab the time and timezone from the system and then output the information to the user and also a modified time with hours++ and hour-- aswell as minute++ and minute--

I researched and found many tutorials and forums with helpful information but I was never able to run their sample codes using the Visual C++ 2008 compiler.

But i think that CTIME is what I'm looking for maybe the localtime() function. But what I don't really understand is how to tweak the time. Will i be able to pass the hours, minutes into variables?

Not asking for complete code (but would be greatly appreaciated) but some help in how to start my program.

I have to impliment them into classes, so I have this written up so far

class Time

{

int time

int hours;

int minutes;

public:

void setTime(int, int);

void returnTime(int, int);

string printTime();

void incrementHour();

void incrementMinute();

timeClass();

{

cout << "The time has been adjusted" << endl;

}

~timeClass();

};

class extTime: Time

{

string time_zone;

public:

void setTime(int, int);

void returnTime(int, int);

string printTime();

void incrementHour();

void incrementMinute();

string printTimeZone();

later on i'll just pass the information into the main() function. But right now I just a little nudge in the right direction.

Thanks

}

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

ok well i've gotten this far, so maybe from what i've done so far someone can help me out

int main()

{

time_t t;

int hour, minutes;

t = time(NULL);

hour = localtime(&t)->tm_hour;

minutes = localtime(&t)->tm_min;

printf("%d:%d" "\n", hour, minutes);

//printf("%d:%d" "\n", hour++, minute++);

system("pause");

return 0;

};

Link to comment
Share on other sites

  • 0

Ok time for an answer. Basically googled and found some code that outputs current time and timezone. This compiles fine under Visual Studio 2008.

This is really C code, if all you've seen is OO you might not be familiar with this style.

#include &lt;stdio.h&gt;
#include &lt;time.h&gt;
#include &lt;string.h&gt;
#include &lt;stdarg.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;

char tzarray[13][3] = {"00", "01", "02", "03", "04", "05", "06", \
"07", "08", "09", "10", "11", "12"};
char tzmin[][3] = {"00", "30"};

int main(void) {
	long dtime = 0;
	short diffhour = 0;
	char diffmin[3] = {"00"};
	char *strtm, *strzone;
	time_t tm1, tm2;
	struct tm *t1, *t2;
	strtm = (char*)malloc(120 * sizeof(char));
	strzone = (char*)malloc(50 * sizeof(char));
	tm1 = time(NULL);
	t2 = gmtime(&amp;tm1);
	tm2 = mktime(t2);
	t1 = localtime(&amp;tm1);
	dtime = (long)(tm1 - tm2);
/* Print local time as a string */
	strftime(strtm, 100, "%a %b %d %Y %H:%M:%S GMT", t1);
	if (dtime &gt;= 0 )	
		strcat(strtm, "+");
	else 
		strcat(strtm, "-");
	strcat(strtm, tzarray[(short)(abs(dtime) / 3600)]);
	if (1800 == (short)(abs(dtime) % 3600))
		strcat(strtm, tzmin[1]);
	else
		strcat(strtm, tzmin[0]);
	strftime(strzone, 50, " (%Z)", t1);
	strcat(strtm, strzone);
	printf("%s\n\n\n", strtm);
	return 0;
}

Link to comment
Share on other sites

  • 0

Wow, thx

Sorry I'm currently in my C++ OOP class. So I read your reply and I got the idea of having the time just outputed in what ever raw format three is and doing the math to narrow it down to a more understandable string. than just ++i and --i!!!

thx, but i'm not familiar with C, only that C++ was build on it. But thx for the idea.

Still if there is a C++ method i can learn from i'm accepting any input from who ever has an idea. ;-)

Link to comment
Share on other sites

  • 0
Wow, thx

Sorry I'm currently in my C++ OOP class. So I read your reply and I got the idea of having the time just outputed in what ever raw format three is and doing the math to narrow it down to a more understandable string. than just ++i and --i!!!

thx, but i'm not familiar with C, only that C++ was build on it. But thx for the idea.

Still if there is a C++ method i can learn from i'm accepting any input from who ever has an idea. ;-)

Well, it is C++ code, but as you might be aware, C++ is (in theory) a superset of C, and it happens that this code is written using only C features.

Here's another, more "official" example : http://www.cplusplus.com/reference/clibrary/ctime/gmtime/

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.