C语言中各种格式的时间调用

4 篇文章 0 订阅

//代码

/* 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(int argc,char** argv)
{
    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( &ltime );
    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
    printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );
 
    /* Display UTC. */
    gmt = gmtime( &ltime );
    printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
 
    /* 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 );
 
    /* 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( &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 );
}

 

结果:

OS time:                                12:32:17
OS date:                                11/25/13
Time in seconds since UTC 1/1/70:       1385353937
UNIX time and date:                     Mon Nov 25 12:32:17 2013
Coordinated universal time:             Mon Nov 25 04:32:17 2013
12-hour time:                           12:32:17 AM
Plus milliseconds:                      140
Zone difference in seconds from UTC:    4294966816
Time zone name:                         中国标准时间
Daylight savings:                       NO
Christmas                               Sat Dec 25 12:00:00 1993

Today is Monday, day 25 of November in the year 2013.
Press any key to continue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值