#include <stdio.h>
#include <time.h>
void Main ()
{
time_t Rawtime;
struct TM * TIMEINFO;
Time (&rawtime);
Timeinfo = LocalTime (&rawtime);
printf ("\007the Date/time is:%s", Asctime (Timeinfo));
Exit (0);
}
=================
#include <time.h>--Required Time function header file
time_t--Time type (time.h definition)
struct TM--time structure, time.h is defined as follows:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int Tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
Time (&rawtime); --acquisition time, in seconds, from January 1, 1970, deposited in Rawtime
LocalTime (&rawtime); --Convert to local time, TM time structure
Asctime ()--Convert to standard ASCII time format:
Day of the Week: minute: Second year
=========================================
The format you want can be output like this:
printf ("%4d-%02d-%02d%02d:%02d:%02d\n", 1900+timeinfo->tm_year, 1+timeinfo->tm_mon,
TIMEINFO->TM_MDAY,TIMEINFO->TM_HOUR,TIMEINFO->TM_MIN,TIMEINFO->TM_SEC);
is direct printing tm,tm_year from 1900, so add 1900,
Month Tm_mon, calculated from 0, so add 1
Example
/* times. C illustrates various time and date functions including:
* Time _ftime CTime asctime
* LocalTime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void Main ()
{
Char tmpbuf[128], ampm[] = "AM";
time_t Ltime;
struct _TIMEB tstruct;
struct TM *today, *gmt, Xmas = {0, 0, 12, 25, 11, 93};
/* Set time zone from TZ environment variable. If TZ is not set,
* The operating system is queried to obtain the default value
* for the variable.
*/
_tzset ();
/* Display operating System-style date and time. */
_strtime (TMPBUF);
printf ("OS time:\t\t\t\t%s\n", tmpbuf);
_strdate (TMPBUF);
printf ("OS date:\t\t\t\t%s\n", tmpbuf);
/* Get Unix-style time and display as number and string. */
Time (<ime);
printf ("Time in seconds since UTC 1/1/70:\t%ld\n", ltime);
printf ("UNIX Time and date:\t\t\t%s", CTime (<ime));
/* Display UTC. */
GMT = Gmtime (<ime);
printf ("Coordinated Universal time:\t\t%s", Asctime (GMT));
/* Convert to TIME structure and adjust for PM if necessary. */
Today = LocalTime (<ime);
if (Today->tm_hour > 12)
{
strcpy (AMPM, "PM");
Today->tm_hour-= 12;
}
if (Today->tm_hour = = 0)/* Adjust if Midnight hour. */
Today->tm_hour = 12;
/* Note How pointer addition are used to skip the first 11
* characters and printf are used to trim off terminating
* characters.
*/
printf ("12-hour time:\t\t\t\t%.8s%s\n",
Asctime (today) + one, ampm);
/* Print additional time information. */
_ftime (&tstruct);
printf ("Plus milliseconds:\t\t\t%u\n", TSTRUCT.MILLITM);
printf ("Zone difference in seconds from utc:\t%u\n",
Tstruct.timezone);
printf ("Time zone name:\t\t\t\t%s\n", _tzname[0]);
printf ("Daylight savings:\t\t\t%s\n",
Tstruct.dstflag? "YES": "NO");
/* Make time for noon on Christmas, 1993. */
if (Mktime (&xmas)! = (time_t)-1)
printf ("christmas\t\t\t\t%s\n", Asctime (&xmas));
/* Use time structure to build a customized time string. */
Today = LocalTime (<ime);
/* Use Strftime to build a customized time string. */
Strftime (TMPBUF, 128,
"Today is%A, day%d of%B in the year%y.\n", today);
printf (TMPBUF);
}
Output
OS time:21:51:03
OS date:05/03/94
Time in seconds since UTC 1/1/70:768027063
UNIX time and Date:tue May 03 21:51:03 1994
Coordinated Universal time:wed May 04 04:51:03 1994
12-hour time:09:51:03 PM
Plus milliseconds:279
Zone difference in seconds from utc:480
Time Zone Name:
Daylight Savings:yes
Christmas Sat 25 12:00:00 1993
Today is Tuesday, day at the year 1994.
Use C to get current system time (RPM)