Reputation: 23
My program to find current system date and time is
#include<stdio.h>
#include<time.h>
struct date{
int day,month,year;
};
main()
{
time_t t;
time(&t);
printf("Today's date and time is %s",ctime(&t));
}
and i want to store this current date to structure Please give me a suggestion of that.
Upvotes: 0
Views: 220
Reputation: 137900
The standard library already has a structure like yours: struct tm
in <time.h>
.
int tm_sec; /* seconds (0 - 60) */
int tm_min; /* minutes (0 - 59) */
int tm_hour; /* hours (0 - 23) */
int tm_mday; /* day of month (1 - 31) */
int tm_mon; /* month of year (0 - 11) */
int tm_year; /* year - 1900 */
int tm_wday; /* day of week (Sunday = 0) */
int tm_yday; /* day of year (0 - 365) */
int tm_isdst; /* is summer time in effect? */
char *tm_zone; /* abbreviation of timezone name */
long tm_gmtoff; /* offset from UTC in seconds */
The library provides a global variable of type struct tm
which is filled by the functions localtime
(for your time zone) and gmtime
(for GMT time).
C11 also specifies localtime_s
and gmtime_s
which avoid the issues associated with global variables, but I don't know how widely supported they are. POSIX also specifies the similar gmtime_r
and localtime_r
.
Upvotes: 3