1. Windows下的各种时间输出
void TestTime()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt;
/* 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 );
//OS time: 23:21:07
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
//OS date: 11/17/18
/* Get UNIX-style time and display as number and string. */
//从1970年开始经过了多少秒
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
//Time in seconds since UTC 1/1/70: 1542468067
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
//UNIX time and date: Sat Nov 17 23:21:07 2018
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
//Coordinated universal time: Sat Nov 17 15:21:07 2018
/* 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 is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n", asctime( today ) + 11, ampm );
//12-hour time: 11:21:07 PM
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
//Plus milliseconds: 253
printf( "Zone difference in seconds from UTC:\t%u\n", tstruct.timezone );
//Zone difference in seconds from UTC: 4294966816
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
//Time zone name: 中国标准时间
//自定义时间
//年从1900开始计数,月从0开始,日从1开始,是不是很奇葩?
struct tm xmas = { 0, 0, 0, 1, 0, 119 };
if( mktime( &xmas ) != (time_t)-1 )
printf( "2019元旦:\t\t\t\t%s\n", asctime( &xmas ) );
//2019元旦: Tue Jan 01 00:00:00 2019
/* 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 );
//Today is Saturday, day 17 of November in the year 2018.
strftime( tmpbuf, 128, "Now is %Y-%m-%d %H:%M:%S\n", today );
printf( tmpbuf );
//Now is 2018-11-17 23:21:07
}
2. Windows下修改文件时间
void AlterFileTime()
{
HANDLE hFile = CreateFile(TEXT("1.txt"), GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
FILETIME ft;
SYSTEMTIME st = {0};
st.wYear = 2018;
st.wMonth = 11;
st.wDay = 17;
st.wHour = 23;
st.wMinute = 36;
st.wSecond = 22;
SystemTimeToFileTime(&st, &ft);
SetFileTime(hFile, NULL, NULL, &ft); //时间修改为2018-11-17 23:36:22
CloseHandle(hFile);
}