Question

I want to extract parts from for example tm_sec or tm_min only, as a simple int (not array or string) how is that and possible (if it is) if something I do not need 100% correct time as of I do it for my own interests and just want ot know the way how to do it. Before tried with to do something with

time_t rawtime;
struct tm * timeinfo;
char buffer [80];

time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"Now it's %H:%M.",timeinfo);
puts (buffer);

But lack of experience stopped me, and now I have no idea what to do (just a beginner in programming)And of course sorry if there was a question like this, but I`ve tried and have not found it.

*The purpose of this is not to display only the parts I want but to edit them

Was it helpful?

Solution

Simply access the members of the struct:

printf("The tm_seconds field is %d\n", timeinfo->tm_sec);

In other words, if you have a pointer to a value of type struct tm, you can use the arrow operator (->) to access any of the members. The tm_sec member has type int, like all the other members.

You could copy the value:

int my_seconds = timeinfo->tm_sec;

and then print the copy, too:

printf("my seconds are %d\n", my_seconds);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top