#include "dateTime.h"
#include <string.h>
#include <stdio.h>
#if !SYSTEM_ENVIRONMENT //Linux
#include <sys/timeb.h>
#include <sys/time.h>
#else //Windows
#include <windows.h>
#endif
//Linux日期时间结构体部分参数
//struct tm
//{
// int tm_sec; //秒[0-59]
// int tm_min; //分[0-59]
// int tm_hour; //时[0-23]
// int tm_mday; //日[1-31]
// int tm_mon; //月份[0-11],0代表一月
// int tm_year; //年份,需要加上1900
// int tm_wday; //星期[0-6],0代表星期天
// int tm_yday; //从每年1月1日开始的天数[0-365],0代表1月1日
//};
/**
* @brief getCurrentDateTime 获取当前日期时间,精确到毫秒
* @param now 当前日期时间信息,不需要可传入NULL
* @param str 当前日期时间信息字符串输出,不需要可传入NULL
* @param format 当前日期时间信息字符串输出格式,不需要可传入NULL
* @return int 0:成功 -1:失败
* 格式format:yyyy--年 MM--月 dd--日 hh--时 mm--分 ss--秒 zzz--毫秒
* 示例:yyyyMMddhhmmsszzz、yyyy-MM-dd hh:mm:ss.zzz 等
*/
int getCurrentDateTime(struct dateTime *now, char *str, const char *format)
{
unsigned short msec;
char buf[24] = {0};
char *p = NULL;
struct tm *tm_now = NULL;
#if !SYSTEM_ENVIRONMENT //Linux
struct timeb t;
ftime(&t);
msec = t.millitm;
tm_now = localtime(&t.time);
#else //Windows
time_t t;
SYSTEMTIME currentTime;
time(&t);
GetSystemTime(¤tTime); //获取当前的UTC时间
msec = currentTime.wMilliseconds;
tm_now = localtime(&t);
#endif
if(!tm_now) {
return -1;
}
if(now) {
now->year = tm_now->tm_year+1900;
now->mon = tm_now->tm_mon+1;
now->mday = tm_now->tm_mday;
now->hour = tm_now->tm_hour;
now->min = tm_now->tm_min;
now->sec = tm_now->tm_sec;
now->msec = msec;
now->wday = tm_now->tm_wday;
now->yday = tm_now->tm_yday;
}
if(str) {
if(!format) {
//默认格式:yyyyMMddhhmmsszzz
sprintf(str, "%d%02d%02d%02d%02d%02d%03d", tm_now->tm_year+1900, tm_now->tm_mon+1,
tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec, msec);
}
else {
sprintf(buf, "%d%02d%02d%02d%02d%02d%03d", tm_now->tm_year+1900, tm_now->tm_mon+1,
tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec, msec);
strcpy(str, format);
if(p = strstr(str, "yyyy")) { strncpy(p, buf, 4); }
if(p = strstr(str, "MM")) { strncpy(p, buf + 4, 2); }
if(p = strstr(str, "dd")) { strncpy(p, buf + 6, 2); }
if(p = strstr(str, "hh")) { strncpy(p, buf + 8, 2); }
if(p = strstr(str, "mm")) { strncpy(p, buf + 10, 2); }
if(p = strstr(str, "ss")) { strncpy(p, buf + 12, 2); }
if(p = strstr(str, "zzz")) { strncpy(p, buf + 14, 3); }
}
}
return 0;
}
#if !SYSTEM_ENVIRONMENT //Linux
/**
* @brief getSystemTime_ms 获取当前系统时间戳,精确到毫秒
* @param
* @return unsigned long long 获取到的时间戳
*/
unsigned long long getSystemTime_ms(void)
{
struct timeb t;
ftime(&t);
return (1000*t.time + t.millitm);
}
/**
* @brief getSystemTime_us 获取当前系统时间戳,精确到微秒
* @param
* @return unsigned long long 获取到的时间戳
*/
unsigned long long getSystemTime_us(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return (1000000*t.tv_sec + t.tv_usec);
}
/**
* @brief delay_ms 延时毫秒
* @param nms 需要延时的毫秒数
* @return
*/
void delay_ms(unsigned long long nms)
{
#if 0 //方法一
unsigned long long start;
start = getSystemTime_ms();
while(getSystemTime_ms() - start < nms);
#else //方法二
struct timeb start, end;
ftime(&start);
while(1) {
ftime(&end);
if(1000*(end.time - start.time) + end.millitm - start.millitm >= nms) {
return;
}
}
#endif
}
/**
* @brief delay_us 延时微秒
* @param nus 需要延时的微秒数
* @return
*/
void delay_us(unsigned long long nus)
{
#if 0 //方法一
unsigned long long start;
start = getSystemTime_us();
while(getSystemTime_us() - start < nus);
#else //方法二
struct timeval start, end;
gettimeofday(&start, NULL);
while(1) {
gettimeofday(&end, NULL);
if(1000000*(end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec >= nus) {
return;
}
}
#endif
}
#else //Windows
/**
* @brief getSystemTime 获取系统时间
* @param currentTime 获取到的系统时间,已从UTC时间转为北京时间
* @return int 0:成功 -1:失败
*/
int getSystemTime(SYSTEMTIME *currentTime)
{
if(!currentTime) {
return -1;
}
GetSystemTime(currentTime); //获取当前的UTC时间
currentTime.wHour += 8; //中国北京时间比UTC时间早8小时,第八时区
return 0;
}
#endif
没有合适的资源?快使用搜索试试~ 我知道了~
C语言获取当前日期和时间.zip
共2个文件
c:1个
h:1个
需积分: 50 12 下载量 37 浏览量
2022-06-18
16:19:25
上传
评论
收藏 3KB ZIP 举报
温馨提示
资源说明: 使用C语言获取系统当前的日期和时间,包括Linux和Windows环境下的不同函数使用。同时,使用自己编写的获取日期和时间的函数自定义实现延时函数的功能。 包含文件: 源文件 dateTime.c 和头文件 dateTime.h。
资源详情
资源评论
资源推荐
收起资源包目录
C语言获取当前日期和时间.zip (2个子文件)
dateTime
dateTime.c 5KB
dateTime.h 1KB
共 2 条
- 1
叶落花枯
- 粉丝: 1w+
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0