ccplusplus.com
Learn C, C++ Concepts
Monday, September 26, 2011
using struct tm in c
/*********************************************************************** * File : using-struct-tm.c * Author : Saurabh Gupta * Desc : using struct tm * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : PM 10:12 26 September 2011 * Note : see the struct tm ***********************************************************************/ #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm * stTimeInfo; int nYear, nMonth, nDay; char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* * take the user date */ printf ("Enter Year: "); scanf ("%d",&nYear); printf ("Enter Month: "); scanf ("%d",&nMonth); printf ("Enter Day: "); scanf ("%d",&nDay); /* * get current stTimeInfo and modify it to the user's choice */ time ( &rawtime ); stTimeInfo = localtime ( &rawtime ); stTimeInfo->tm_year = nYear - 1900; stTimeInfo->tm_mon = nMonth - 1; stTimeInfo->tm_mday = nDay; /* * call mktime: stTimeInfo->tm_wday will be set */ mktime ( stTimeInfo ); printf ("Entered Day is a %s.\n", weekday[stTimeInfo->tm_wday]); return 0; } /* * OUTPUT * [sgupta@rhel55x86 c]$ gcc using-struct-tm.c -o using-struct-tm [sgupta@rhel55x86 c]$ ./using-struct-tm Enter Year: 2011 Enter Month: 9 Enter Day: 26 Entered Day is a Monday. [sgupta@rhel55x86 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment