getting the time (y,m,d,h,m,s)

I did not find a nice way for getting year month day etc…
so I wrote my one function:

int get_date(int option)
{
int value =0;
char thetime[32] = {0};
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );

switch (option)
{
	case 1: /*YEAR*/
	  		strftime(thetime, sizeof thetime, "%Y", timeinfo);
			break;
	case 2: /*MONTH*/
			strftime(thetime, sizeof thetime, "%m", timeinfo);
			break;
	case 3: /*DAY*/
			strftime(thetime, sizeof thetime, "%d", timeinfo);
			break;
	case 4: /*HOUR*/
			strftime(thetime, sizeof thetime, "%I", timeinfo);
			break;
	case 5: /*MINUTES*/
			strftime(thetime, sizeof thetime, "%M", timeinfo);
			break;
	case 6: /*SECONDS*/
			strftime(thetime, sizeof thetime, "%S", timeinfo);
			break;	
}
value = atoi(thetime);
return value;

}

nice and easy :slight_smile:

by the way its quite odd that you cant just get this values
from timeinfo like timeinfo.tm_hour cause it trow:

request for member `tm_hour’ in something not a structure or union :confused:

Huh???

It’s timeinfo->tm_hour not timeinfo.tm_hour.

:smiley: yeah that way it works. thx.

Do anyone know is there a way to set the time other then entering new value of seconds from 1970. But rather buy entering y:m:d h:m:s:ms ?

I don’t know what the situation is with QNX 6, but there has been an unlocaltime() routine floating around that does just that. It’s actually not very hard to write if you think about it. The only complications are leap year and don’t leap leap year. A good programming exercise if you’ve never done it.

Sure, stick the values into the struct tm and then call mktime.

I prefer to use the DateTime object from the C++ Poco library.

thx mario and maschoen for help.

mario to late ;] I took advice of maschoen and made myself a programming exercise and
wrote it on my own.

If some one would like to see the code let me know :slight_smile: