Time using time.h in C and C++

Demo of getting date/time info in C or C++ (without using windows.h)

Page 1 of 1

2 Replies - 7170 Views - Last Post: 24 September 2008 - 05:43 AM Rate Topic: -----

#1 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Time using time.h in C and C++

Posted 23 September 2008 - 06:58 PM

/* 
 * If you don't wish to use windows.h ....
 * Here is a 'C' (also can use 'C++') example using 'time.h' and a call to
 * strftime( bufferStr, bufsize, formatString, pointer_to_struc_tm_info )
 *
 * The convert day '01' to 1 using 'atoi' function is shown, just in case
 * you might like that also ... 
*/

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

int main ()
{
	time_t rawtime;
	time( &rawtime );
	
	struct tm * timeinfo;
	timeinfo = localtime( &rawtime ); /* to access LOCAL date/time info*/
	
	char buffer [128];
	
	strftime( buffer, 128, "Time now is %I:%M%p", timeinfo );
	printf( "%s ...\n\n", buffer );
	
	strftime( buffer, 128, "%A %B %d, day %j of year %Y, %X %Z.", timeinfo );
	printf( "%s\n\n", buffer );
	
	strftime( buffer, 128, "%Y-%m-%d %X or if you like 'am/pm' it is %I:%M:%S %p", timeinfo );
	printf( "%s\n\n", buffer );
	
	strftime( buffer, 128, "%a %b %d, %y at %I:%M:%S%p", timeinfo );
	printf( "%s\n\n", buffer );
	
	/* Or ... get the 'bits' just the way you like ... */
	
	strftime( buffer, 128, "%A %B", timeinfo );
	printf( "%s", buffer ); /* print the fullDay fullMonth*/
	
	strftime( buffer, 128, "%d", timeinfo );
	printf( " %d, ", atoi(buffer) ); /* print the day number as an integer so NO leading '0' */
   
	strftime( buffer, 128, "%Y at %I:%M:%S%p", timeinfo );
	printf( "%s ", buffer ); /* print 'YYYY at hh:mm:ssAM/PM' */
		
	getchar(); /* To keep Window open ... if needed. */
	return 0;
}

/*
%a	Abbreviated weekday name *											   Thu
%A	Full weekday name *												 Thursday
%b	Abbreviated month name *												 Aug
%B	Full month name *													 August
%c	Date and time representation *					  Thu Aug 23 14:55:02 2001
%d	Day of the month (01-31)												  23
%H	Hour in 24h format (00-23)												  14
%I	Hour in 12h format (01-12)												  02
%j	Day of the year (001-366)												 235
%m	Month as a decimal number (01-12)										  08
%M	Minute (00-59)															  55
%p	AM or PM designation													  PM
%S	Second (00-61)															  02
%U	Week number with the first Sunday as the first day of week one (00-53)	  33
%w	Weekday as a decimal number with Sunday as 0 (0-6)						  4
%W	Week number with the first Monday as the first day of week one (00-53)	  34
%x	Date representation *												08/23/01
%X	Time representation *												14:55:02
%y	Year, last two digits (00-99)											  01
%Y	Year																	2001
%Z	Timezone name or abbreviation											 CDT
%%	A % sign

	* The above specifiers whose description is marked 
	  with an asterisk (*) are locale-dependent.  
	
	See ... (ref.)
	http://www.cplusplus.com/reference/clibrary/ctime/strftime.html 
	
	Or ... (me)
	http://www.geocities.com/dwzavitz/ 
	http://securejoules.googlepages.com/
	http://developers-heaven.net/forum/index.php/topic,46.0.html

*/

 


Is This A Good Question/Topic? 0
  • +

Replies To: Time using time.h in C and C++

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,563
  • Joined: 25-January 07

Re: Time using time.h in C and C++

Posted 23 September 2008 - 11:21 PM

Is there any problem in it?
Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Time using time.h in C and C++

Posted 24 September 2008 - 05:43 AM

I think this is supposed to be a snippet?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1