SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 7th, 2003, 08:09 AM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Getting formatted (UNIX) system time and date

Hello!

I need to get the system's date and time in the dd-mm-yyy and hh:mm:ss format and write it on to a string (so it can be written onto a structure's member and later on, to a file). I figured something out like this:

char sRelTempo[30], pts=':', space=' ', hif='-';
time_t agora;
struct tm *relTempo;

time(&agora);
relTempo = localtime(&agora);
sprintf(sRelTempo,"%d",relTempo->tm_mday);
sprintf(sRelTempo,"%c",hif);
sprintf(sRelTempo,"%d",relTempo->tm_mon); etc...

but it's quite a huge chunk of code and it won't give me days and seconds in the format specified. So, in "The C Programming Language" I saw the strftime function that, I guess, will do this for me. I just can't figure out how to use it (and what parameters to provide and what will it return); can someone post an example, please? So, how can I solve my problem, if this function doesn't do what I need?

Thanx in advance!

Reply With Quote
  #2  
Old May 7th, 2003, 10:34 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
Yes, it looks like strftime() should do the job for you. Remember, you can combine the format specifiers to customize your own string.

For that matter, your example can combined into one statement:
Code:
sprintf(sRelTempo,"%d%c%d",relTempo->tm_mday,hif,relTempo->tm_mon); 

or better:
Code:
sprintf(sRelTempo,"%d-%d",relTempo->tm_mday,relTempo->tm_mon); 


Look up the format specifiers in your help/manpage system. If none of them exactly matches what you want, then create it; eg, for your "dd-mm-yyyy hh:mm:ss" string:
Code:
strftime(sRelTempo,29,"%d-%m-%Y %T",relTempo); 

Comparing a few different listings of the format specifiers, I found that not all the help-file/man-page listings were complete. So do a Google search on strftime and play with it until you get what you want.

Reply With Quote
  #3  
Old May 7th, 2003, 10:41 AM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm pretty sure that
strftime(sRelTempo,29,"%d-%m-%Y %T",relTempo);
will do the job, I just didn't know how to use it properly, it kept giving errors I couldn't solve. By the way, what (or how) will the function return? I still have to put the return into a string->structure->file

And thanx a lot so far!

Reply With Quote
  #4  
Old May 7th, 2003, 10:49 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
How were you using it and what errors did it give you?

As for what strftime returns, it's the number of characters in the string that was generated.

Says Visual C++6 Help:
Quote:
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );

size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );

Return Value

strftime returns the number of characters placed in strDest if the total number of resulting characters, including the terminating null, is not more than maxsize. wcsftime returns the corresponding number of wide characters. Otherwise, the functions return 0, and the contents of strDest is indeterminate.


Says the man page on Red Hat Linux 7:
Quote:
RETURN VALUE
The strftime() function returns the number of characters
placed in the array s, not including the terminating NUL
character, provided the string, including the terminating
NUL, fits. Otherwise, it returns 0, and the contents of
the array is undefined. (Thus at least since libc 4.4.4;
very old versions of libc, such as libc 4.4.1, would
return max if the array was too small.)

Note that the return value 0 does not necessarily indicate
an error; for example, in many locales %p yields an empty
string.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Getting formatted (UNIX) system time and date


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway