#include <stdio.h>
#include <time.h>
struct my_tm {
int year; // 現在年份
int month; // 月份
int day; // 日期
int hour; // 二十四小時制的時
int isam; // 上午為非 0 值,下午為 0
int hourt; // 十二小時制的時
int min; // 分
int sec; // 秒
};
typedef struct my_tm My_Time;
My_Time myTimeS(time_t seconds);
int main(void)
{
time_t seconds = time(NULL);
My_Time now = myTimeS(seconds);
printf("\n\n現在時間是西元 %d 年 %d 月 %d 日", now.year, now.month, now.day);
printf("%s %d 點 %d 分 %d 秒\n\n", now.isam ? "上午" : "下午", now.hourt, now.min, now.sec);
return 0;
}
My_Time myTimeS(time_t seconds)
{
struct tm *tmPtr = localtime(&seconds);
My_Time mt;
mt.year = tmPtr->tm_year + 1900;
mt.month = tmPtr->tm_mon + 1;
mt.day = tmPtr->tm_mday;
mt.hour = tmPtr->tm_hour;
if (mt.hour < 12 ) {
mt.isam = 1;
mt.hourt = tmPtr->tm_hour;
}
else {
mt.isam = 0;
mt.hourt = tmPtr->tm_hour - 12;
}
mt.min = tmPtr->tm_min;
mt.sec = tmPtr->tm_sec;
return mt;
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:timetest2.c
功能:示範結構的使用
作者:張凱慶
時間:西元2010年7月 */ 編譯後執行,結果如下

程式說明,請繼續參考
沒有留言:
張貼留言