时间操作相关的函数

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( &ltime );
    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( &ltime ) );    
    //UNIX time and date:                     Sat Nov 17 23:21:07 2018

    /* Display UTC. */
    gmt = gmtime( &ltime );
    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( &ltime );
    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( &ltime );

    /* 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值