Solved

Using ctime()...

Posted on 1999-01-08
6
270 Views
Last Modified: 2010-04-15
I cannot believe this is stumping me, but after trying several times to follow the man pages and get ctime() to work I can't seem to do so... I am looking for a short snippet which illustrates using ctime() to store the ascii version of the current date/time in a string. Surely someone can help me?

Thanks,
Iccy
0
Comment
Question by:icculus
  • 4
  • 2
6 Comments
 
LVL 1

Expert Comment

by:dpuckett
ID: 1255658
Let me know if this helped you, and I'll post as ANSWER

#include <time.h>
#include <stdio.h>
void main( void )
{
   time_t ltime;
   time( &ltime );
   printf( "The time is %s\n", ctime( &ltime ) );
}
0
 
LVL 1

Expert Comment

by:dpuckett
ID: 1255659
Let me know if this helped you, and I'll post as ANSWER

Sorry, re-read your question, here is stored in stringTime.

The string result produced by ctime contains exactly 26 characters and has the form of the following example:
Wed Jan 02 02:03:55 1980\n\0

#include <time.h>
#include <stdio.h>
void main( void )
{
   time_t ltime;
   time( &ltime );
   char stringTime[26];

   sprintf(stringTime,"%s",ctime(&ltime));
   printf( "The time is %s\n", stringTime );
}

0
 
LVL 1

Author Comment

by:icculus
ID: 1255660
Thats exactly what I needed to see - and it works great! Please post as an answer and I'll grade it as soon as I see the notification...

Thanks!
Andy
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
dpuckett earned 70 total points
ID: 1255661
Glad I could help icculus :)

The string result produced by ctime contains exactly 26 characters and has the form of the following example:
Wed Jan 02 02:03:55 1980\n\0

#include <time.h>
#include <stdio.h>
void main( void )
{
   time_t ltime;
   time( &ltime );
   char stringTime[26];

   sprintf(stringTime,"%s",ctime(&ltime));
   printf( "The time is %s\n", stringTime );
}

0
 
LVL 1

Author Comment

by:icculus
ID: 1255662
Thanks again! Before seeing your new version (with the string char) I just enhanced what you had to use a char * - malloced with ample room. This'll work better though - thanks! :)

-Iccy
0
 
LVL 1

Expert Comment

by:dpuckett
ID: 1255663
No problem Andy, I wasn't sure what environment you were writing in and wanted to give you something ANSI compatable.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now