// C7_6.cpp : Defines the entry point for the console application. // 時間函式應用 #include "stdafx.h" #include #include #include #include int main(int argc, char* argv[]) { struct tm *newtime; char am_pm[] = "AM"; time_t long_time; time( &long_time ); /* 取得時間長整數 */ newtime = localtime( &long_time ); /* 轉換為區域時間 */ if( newtime->tm_hour > 12 ) strcpy( am_pm, "PM" ); if( newtime->tm_hour > 12 ) newtime->tm_hour -= 12; /* 轉換成 12 小時 */ if( newtime->tm_hour == 0 ) /* 若午夜則設定為 12 */ newtime->tm_hour = 12; printf( "\t目前時刻 : %s\n", asctime( newtime ) , am_pm ); return 0; }