ccplusplus.com
Learn C, C++ Concepts
Tuesday, January 3, 2012
struct tm in c example
/*********************************************************************** * File : using-struct-tm.c * Author : Saurabh Gupta * Desc : struct tm in c example * Source : http://ccplusplus.com/p/c.html * Created : PM 12:12 02 January 2012 * Note : see the struct tm ***********************************************************************/ #include
#include
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 -o struct-tm-example struct-tm-example.c [sgupta@rhel55x86 c]$ ./struct-tm-example Enter Year: 2012 Enter Month: 1 Enter Day: 3 Entered Day is a Tuesday. [sgupta@rhel55x86 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment