[C++][Demo]當前時間

Demo Code

#include <stdint.h>

#define UTIL_TIME_WAY		1

typedef uint16_t	Type_Year;
typedef uint8_t		Type_Month;
typedef uint8_t		Type_Day;
typedef uint8_t		Type_Hour;
typedef uint8_t		Type_Minute;
typedef uint8_t		Type_Second;
typedef uint16_t	Type_MSecond;
typedef uint16_t	Type_USecond;
typedef uint64_t	Type_Long;

typedef struct tag_UtilDate {
	Type_Year		year;
	Type_Month		month;
	Type_Day		day;
	Type_Hour		hour;
	Type_Minute		min;
	Type_Second		sec;
	Type_MSecond	msec;
	Type_USecond	usec;
}UtilDate;

UtilDate get_current_time(void);
#include "tm.h"

#if UTIL_TIME_WAY == 1
#include <sys/timeb.h>
#elif UTIL_TIME_WAY == 2
#include <sys/time.h>
#endif

#define UTIL_YEAR_IN_CYCLE_MAX  4U
#define UTIL_MONTH_IN_YEAR_MAX  12U

static Type_Day g_day_in_month_nrml[UTIL_MONTH_IN_YEAR_MAX] = \
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static Type_Day g_day_in_month_leap[UTIL_MONTH_IN_YEAR_MAX] = \
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static Type_Year g_year_in_cycle[UTIL_YEAR_IN_CYCLE_MAX] = \
{ 365, 365, 365, 366 };

static void util_sec_to_date(UtilDate* _date, Type_Long _sec) {
    Type_Long year_count = 0U;
    Type_Long mouth_index = 0U;
    Type_Long day_count = _sec / 60U / 60U / 24U;
    Type_Day* p_mouth_arr = g_day_in_month_nrml;

	if (NULL == _date) {
		return;
	}
    _date->sec  = _sec % 60U;
    _date->min  = _sec / 60U % 60U;
    _date->hour = _sec / 60U / 60U % 24U;
    
    while (1) {
        day_count -= g_year_in_cycle[++year_count % UTIL_YEAR_IN_CYCLE_MAX];
        if (day_count <= (365U + year_count % UTIL_YEAR_IN_CYCLE_MAX)) {
            break;
        }
    }

    _date->year = 1970U + year_count;
    if ((UTIL_YEAR_IN_CYCLE_MAX - 1U) == (year_count % UTIL_YEAR_IN_CYCLE_MAX)) {
        p_mouth_arr = g_day_in_month_leap;
    }

    for (mouth_index = 0U; mouth_index < UTIL_MONTH_IN_YEAR_MAX; ++mouth_index) {
        if (day_count <= p_mouth_arr[mouth_index]) break;
        day_count -= p_mouth_arr[mouth_index];
    }

    _date->month = mouth_index + 1U;
    _date->day = day_count;
}

UtilDate get_current_time(void) {
	UtilDate date = { 0 };
#if UTIL_TIME_WAY == 1
    struct timeb t;
    ftime(&t);

    t.time -= ((time_t)t.timezone * 60);

    date.msec   = t.millitm;
    util_sec_to_date(&date, t.time);
#elif UTIL_TIME_WAY == 2
    struct timeval t;
    struct timezone zone;
    gettimeofday(&t, &zone);

    date.usec = t.tv_usec % 1000U;
    date.msec = t.tv_usec / 1000U;
    
    util_sec_to_date(&date, t.sec + (zone.tz_minuteswest * 60U));
#endif

    return date;
}
int main()
{
    UtilDate date = get_current_time();

    printf("year    = [%ld]\n", date.year);
    printf("month   = [%ld]\n", date.month);
    printf("day     = [%ld]\n", date.day);
    printf("hour    = [%ld]\n", date.hour);
    printf("min     = [%ld]\n", date.min);
    printf("sec     = [%ld]\n", date.sec);
    printf("msec    = [%ld]\n", date.msec);
    printf("usec    = [%ld]\n", date.usec);
}

Demo Result

year    = [2020]
month   = [5]
day     = [29]
hour    = [10]
min     = [35]
sec     = [6]
msec    = [647]
usec    = [0]

Summary

T.B.D.

Thanks For Watching, Have a nice day!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章