[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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值