封装三个函数显示当前的时间

/******************************************************************************************************
 * @file name:		  :通过结构体获取时间
 * @brief  		      :
 * @author 		      :wvjnuhhail@126.com
 * @date 			  :2024/06/18
 * @version 1.0 	  :V1.0
 * @property 		  :暂无
 * @note   		      :None
 * CopyRight (c)  2023-2024   wvjnuhhail@126.com   All Right Reseverd
 ******************************************************************************************************/
#include <stdio.h>
#include <time.h>
#include <unistd.h>
/*
2)封装三个函数,一个函数计算小时,一个函数计算分钟,最后一个函数计算秒,然后你在main里面显示时间。
(全局变量)
*/



/*******************************************************************************************************
 * @function name   :	getHours
 * @brief           : 获得当前的小时
 * @param           : t
 * @retval          : void
 * @date 			:2024/06/18
 * @version         :V1.0
 * @note   		    :None
 *******************************************************************************************************/
void getHours(time_t t) {
    struct tm *local = localtime(&t);
    printf("localtime is :");
    printf(": %d", local->tm_hour);
}
/*******************************************************************************************************
 * @function name   :	getMinutes
 * @brief           : 获得当前的分钟
 * @param           : t
 * @retval          : void
 * @date 			:2024/06/18
 * @version         :V1.0
 * @note   		    :None
 *******************************************************************************************************/

void getMinutes(time_t t) {
    struct tm *local = localtime(&t);
    printf(": %d", local->tm_min);
}


/*******************************************************************************************************
 * @function name   :	getSeconds
 * @brief           : 获得当前的秒数
 * @param           : t
 * @retval          : void
 * @date 			:2024/06/18
 * @version         :V1.0
 * @note   		    :None
 *******************************************************************************************************/
void getSeconds(time_t t) {
    struct tm *local = localtime(&t);
    printf(": %d\n", local->tm_sec);
}

int main() {
    while (1) {
        time_t current_time = time(NULL);

        getHours(current_time);
        getMinutes(current_time);
        getSeconds(current_time);

        sleep(1);
    }
    return 0;
}


image

posted @ 2024-06-18 21:56  WJnuHhail  阅读(2)  评论(0编辑  收藏  举报