473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple question (itoa, localtime)

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(&currentTi me);
systime = localtime(&curr entTime);

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(currentD ate, day);
strcat(currentD ate, ".");
strcat(currentD ate, month);
strcat(currentD ate, ".");
/** QUESTION: Is there something simpler than that to concatenate strings ?
**/
<snip>

Thank you !

Brad
Nov 13 '05 #1
2 5028
bear in mind I am no expert
comments inline

"Raskolniko w" <ra*********@fr eesurf.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(&currentTi me);
systime = localtime(&curr entTime);

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(currentD ate, day);
strcat(currentD ate, ".");
strcat(currentD ate, month);
strcat(currentD ate, ".");
/** QUESTION: Is there something simpler than that to concatenate strings ? **/


yes strftime() for one way

hth
cw
Nov 13 '05 #2
"Raskolniko w" <ra*********@fr eesurf.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(&currentTi me);
systime = localtime(&curr entTime);

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(currentD ate, day);
strcat(currentD ate, ".");
strcat(currentD ate, month);
strcat(currentD ate, ".");
/** 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(&currentTi me);
systime = localtime(&curr entTime);

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

return 0;
}

What was your question?
--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3701
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward thing to look for): -- John Lenton (john@grulic.org.ar) -- Random fortune: Los cementerios están llenos de valientes.
10
1340
by: Sean McIlroy | last post by:
Can anybody help me make sense of the fact that the following script doesn't work? It's so simple I can't imagine what I'm missing. Any help will be much appreciated. Peace, STM ## ALARM CLOCK: from time import sleep,time,localtime
7
12849
by: news.hku.hk | last post by:
Excuse me, i write the following function to add comma for integers but the unix server said: In function `class string comma(int)': implicit declaration of function `int itoa(...)' ________________________________ string comma(int a){ char to_string; string s_a = itoa(a, to_string, 10);
2
6677
by: Sona | last post by:
Hi, I have a char* that holds an ascii character in its first element (at least I think that's what it holds because when I print it, it prints weird characters). I need to convert this into an integer and so I thought the following might work: char *somevar ... // somevar holds a value in somevar and is null terminated i.e. somevar = '\0'
29
20900
by: pete | last post by:
I wrote a version of itoa yesterday. Features: 1 No implementation defined arithmetic. All of the division and modulus division is done on positive values only. 2 No object is assumed capable of representing the magnitude of INT_MIN. 3 The string is constructed in place without reversal. 4 No features from standard library are used.
11
3598
by: rayw | last post by:
I'm pretty new to C, although I did do some years ago now. I've been told that itoa is no longer a standard function, and that the ato... functions - although in the std - are not recommended. So, I was wondering what was wrong with both itoa and atoi etc (and what's replaced them). Many thanks
24
8192
by: Mark | last post by:
hi, all i want is a simple function that takes an int, and returns a char* so i tried char * itoa(int i) { char buff; return _itoa(i,buff,10); }
7
39549
by: silverburgh.meryl | last post by:
Hi, Can you please tell me where I can find itoa()? I try to compile the following example, but I get the following error: .../t.cpp:20:2: warning: no newline at end of file .../t.cpp: In function 'int main()': .../t.cpp:13: error: 'itoa' was not declared in this scope /* itoa example */
1
4365
by: OleMacGeezer | last post by:
Hello Everyone, I am a brand new Python programmer with barely a month of experience under my belt. Here are my specs: Mac OSX Panther 10.3.9 Jython 2.1 implementation with Hermes BBS python module installed
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.