simple question (itoa, localtime)

R

Raskolnikow

Hi!

I have a very simple problem with itoa() or the localtime(...).
Sorry, if it is too simple, I don't have a proper example.
Please have a look at the comments.

struct tm *systime;
time_t currentTime;
char day[2];
char month[2];
char currentDate[6];

main(int argc, char **argv)
{
time(&currentTime);
systime = localtime(&currentTime);

itoa(systime->tm_mday, day, 10);
itoa(systime->tm_mon, month, 10);
printf("day = %s month = %s\n", day, month);
/** QUESTION: why is [day] not the current day but something arbitrary **/

strcat(currentDate, day);
strcat(currentDate, ".");
strcat(currentDate, month);
strcat(currentDate, ".");
/** QUESTION: Is there something simpler than that to concatenate strings ?
**/
<snip>

Thank you !

Brad
 
C

code_wrong

bear in mind I am no expert
comments inline

Raskolnikow said:
Hi!

I have a very simple problem with itoa() or the localtime(...).
Sorry, if it is too simple, I don't have a proper example.
Please have a look at the comments.

struct tm *systime;
time_t currentTime;

You have not allowed for the string terminating null character
try char day[3];
char day[2];

same here
char month[2];

char currentDate[6];

main(int argc, char **argv)
{
time(&currentTime);
systime = localtime(&currentTime);

You are aware that itoa() is not standard ANSI C and is therefore off topic
for this newsgroup
itoa(systime->tm_mday, day, 10);
itoa(systime->tm_mon, month, 10);

You could use strftime() to format a string in a multitude of ways for
displaying
printf("day = %s month = %s\n", day, month);
/** QUESTION: why is [day] not the current day but something arbitrary **/

not enough space allocated for null terminated string as stated above
strcat(currentDate, day);
strcat(currentDate, ".");
strcat(currentDate, month);
strcat(currentDate, ".");
/** QUESTION: Is there something simpler than that to concatenate strings ?
**/

yes strftime() for one way

hth
cw
 
M

Martin Ambuhl

Hi!

I have a very simple problem with itoa() or the localtime(...).
Sorry, if it is too simple, I don't have a proper example.
Please have a look at the comments.

Missing headers...
struct tm *systime;
time_t currentTime;
char day[2];
char month[2];
char currentDate[6];

All the above are too small.
main(int argc, char **argv)
{
time(&currentTime);
systime = localtime(&currentTime);

itoa(systime->tm_mday, day, 10);
itoa(systime->tm_mon, month, 10);
printf("day = %s month = %s\n", day, month);
/** QUESTION: why is [day] not the current day but something
arbitrary **/

Your clock is set incorrectly.
strcat(currentDate, day);
strcat(currentDate, ".");
strcat(currentDate, month);
strcat(currentDate, ".");
/** QUESTION: Is there something simpler than that to concatenate
strings ? **/
<snip>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
struct tm *systime;
time_t currentTime;
char currentDate[7];

time(&currentTime);
systime = localtime(&currentTime);

printf("day = %d month = %d\n", systime->tm_mday,
1 + systime->tm_mon);
sprintf(currentDate, "%d.%d", systime->tm_mday,
1 + systime->tm_mon);
puts(currentDate);

return 0;
}

What was your question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top