c语言获取本地时间

1、源代码

#include <stdio.h>
#include <time.h>
#include <string.h>

#define Now_Time_LEN 20
#define Now_Week_Num_LEN 10

char nowTime[10] = {0};
char nowWeek_Num[10] = {0};

void *getNowTime(char *Now_Time, char *Now_Week_Num)
{
    int tmp_day = 0;
    int tmp_year = 0;
    int iZoneOffset = 8;
    int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    char acWeek[5] = {0};
    char acHour[3] = {0};
    char acMin[3] = {0};
    char acSec[3] = {0};

    memset(Now_Time, 0, Now_Time_LEN + 1);
    memset(Now_Week_Num, 0, Now_Week_Num_LEN + 1);

    time_t now;
    struct tm timenow;

    now = time(NULL);
    if (now <= 0 || &timenow == NULL)
    {
        return NULL;
    }
    (void *)gmtime_r(&now, &timenow);
    timenow.tm_year += 1900;
    timenow.tm_mon += 1;
    if (timenow.tm_mon == 2) // 2 月
    {
        if ((timenow.tm_year % 400 == 0) || (timenow.tm_year % 4 == 0 && timenow.tm_year % 100 != 0))
        {
            month[2] += 1;
        }
    }
    //+ timezone
    timenow.tm_hour += iZoneOffset;
    if (timenow.tm_hour >= 24)
    {
        tmp_day = 1;
        timenow.tm_hour %= 24;
    }
    timenow.tm_mday += tmp_day;
    if (timenow.tm_mday > month[timenow.tm_mon])
    {
        timenow.tm_mday = 1;
        timenow.tm_mon += 1;
    }
    if (timenow.tm_mon > 12)
    {
        tmp_year = 1;
        timenow.tm_mon = 1;
    }
    timenow.tm_year += tmp_year;

    strftime(acWeek, sizeof(acWeek), "%a", &timenow);
    strftime(acHour, sizeof(acHour), "%H", &timenow);
    strftime(acMin, sizeof(acMin), "%M", &timenow);
    strftime(acSec, sizeof(acSec), "%S", &timenow);

    if (!strcmp(acWeek, "Mon"))
    {
        strncat(Now_Week_Num, "1", 1);
    }
    else if (!strcmp(acWeek, "Tue"))
    {
        strncat(Now_Week_Num, "2", 1);
    }
    else if (!strcmp(acWeek, "Wed"))
    {
        strncat(Now_Week_Num, "3", 1);
    }
    else if (!strcmp(acWeek, "Thu"))
    {
        strncat(Now_Week_Num, "4", 1);
    }
    else if (!strcmp(acWeek, "Fri"))
    {
        strncat(Now_Week_Num, "5", 1);
    }
    else if (!strcmp(acWeek, "Sat"))
    {
        strncat(Now_Week_Num, "6", 1);
    }
    else if (!strcmp(acWeek, "Sun"))
    {
        strncat(Now_Week_Num, "7", 1);
    }
    else
    {
        printf("Now_Week_Num get erro !\n");
    }

    strncat(Now_Time, acHour, 2);
    strncat(Now_Time, ":", 1);
    strncat(Now_Time, acMin, 2);
    strncat(Now_Time, ":", 1);
    //strncat(Now_Time, "00", 2);
    strncat(Now_Time, acSec, 2);
    
    return NULL;
}
int main()
{
    getNowTime(nowTime, nowWeek_Num);
    printf("nowTime is %s !\nnowWeek_Num is %s !\n", nowTime, nowWeek_Num);
    return 0;
}

2、运行结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值