博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言获取本地时间
阅读量:3946 次
发布时间:2019-05-24

本文共 2658 字,大约阅读时间需要 8 分钟。

1、源代码

#include 
#include
#include
#define Now_Time_LEN 20#define Now_Week_Num_LEN 10char 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、运行结果

在这里插入图片描述

转载地址:http://qqowi.baihongyu.com/

你可能感兴趣的文章
Android Package and Manifest File
查看>>
Creating Multiple APKs with 2+ Dimensions 创建两种以上屏幕尺寸多apk支持
查看>>
Abstracting the New APIs 抽象出新的API
查看>>
Proxying to the New APIs 代理新的API
查看>>
Creating an Implementation with Older APIs 用较早版本的APIs实现抽象类
查看>>
Using the Version-Aware Component 使用版本识别组件
查看>>
Enhancing Security with Device Management Policies 加强安全与设备管理策略 Developing for Enterprise
查看>>
Advertising without Compromising User Experience 不降低用户体验的广告
查看>>
Planning Screens and Their Relationships 规划屏幕和它们的关系
查看>>
Planning for Multiple Touchscreen Sizes 规划多个触摸屏尺寸
查看>>
Providing Descendant and Lateral Navigation 提供下一代和横向导航
查看>>
GPS 0183协议GGA、GLL、GSA、GSV、RMC、VTG解释 + 数据解析
查看>>
android如何使得电阻屏在第一次开机时自动叫起屏幕校准程序
查看>>
android如何实现:当开启图案解锁时,取消滑动解锁
查看>>
Providing Ancestral and Temporal Navigation 设计高效的应用导航
查看>>
Putting it All Together: Wireframing the Example App 把APP例子用线框图圈起来
查看>>
Implementing Lateral Navigation 实现横向导航
查看>>
Implementing Ancestral Navigation 实现原始导航
查看>>
Implementing Temporal Navigation 实现时间导航
查看>>
Responding to Touch Events 响应触摸事件
查看>>