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

来源:互联网 发布:打开淘宝商品链接 编辑:程序博客网 时间:2024/04/27 21:37

//代码

/* 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

原创粉丝点击