By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

simple question (itoa, localtime)

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
bear in mind I am no expert
comments inline

"Raskolnikow" <ra*********@freesurf.ch> wrote in message
news:bf************@ID-126543.news.uni-berlin.de...
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
Nov 13 '05 #2

P: n/a
"Raskolnikow" <ra*********@freesurf.ch> wrote (22 Jul 2003) in
news:bf************@ID-126543.news.uni-berlin.de / comp.lang.c:
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?
--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.