-
2022-04-23 08:13:11
直接上代码
#include <stdio.h> #include <time.h> int main(){ time_t tmpcal_t; struct tm *tmp_ptr = NULL; time(&tmpcal_t); tmp_ptr = localtime(&tmpcal_t); printf ("the time is:%d.%d.%d ", (1900+tmp_ptr->tm_year), (1+tmp_ptr->tm_mon), tmp_ptr->tm_mday); printf("%d:%d:%d\n", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec); }
更多相关内容 -
C语言获取系统时间的几种方式
2021-01-31 07:51:02在如下的库支持下,开发的系统可以很方便移植到当前大部分平台上运行而无需改动,只需在对应的平台下用你喜欢的编译器重新编译即可经典的C++库STLport-------SGISTL库的跨平台可移植版本,在以前有些编译器离符合... -
C语言获取时间,供初学者使用
2018-08-02 11:40:34linux平台,C语言获取系统时间,返回各种格式。下载后如果用windows使用,需要修改引用头文件 -
C语言获取当前日期和时间
2022-06-18 15:18:22本文介绍使用C语言获取系统当前的日期和时间,包括Linux和Windows环境下的不同函数使用。同时,使用自己编写的获取日期和时间的函数自定义实现延时函数的功能。(1)函数 void ftime(struct timeb *tp); ...一、前言
本文介绍使用C语言获取系统当前的日期和时间,包括Linux和Windows环境下的不同函数使用。同时,使用自己编写的获取日期和时间的函数自定义实现延时函数的功能。
二、相关函数介绍及代码实现
2.1、Linux下的相关函数
(1)函数 void ftime(struct timeb *tp);
头文件:#include <sys/timeb.h>
说明函数:获取当前的时间和日期。
返回值:通过参数返回一个 timeb 结构体。- timeb 结构体定义:
struct timeb { time_t time; /* 为1970-01-01至今的秒数*/ unsigned short millitm; /* 千分之一秒即毫秒 */ short timezone; /* 为时区和Greenwich相差的时间,单位为分钟 */ short dstflag; /* 为日光节约时间的修正状态,如果为非0代表启用日光节约时间修正 */ };
(2)函数 struct tm *localtime(const time_t *clock);
头文件:#include <time.h>
函数说明:把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间。
返回值:指向 tm 结构体的指针。- tm 结构体定义:
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日 };
2.2、Windows下的相关函数
(1)函数 VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);
头文件:#include <windows.h>
函数说明:获取当前系统日期和时间,是UTC时间。
返回值:通过参数返回 LPSYSTEMTIME 结构体指针。- LPSYSTEMTIME 结构体定义:
typedef struct _SYSTEMTIME { WORD wYear; //年 WORD wMonth; //月[1-12] WORD wDayOfWeek; //星期[0-6],0代表星期天 WORD wDay; //日[1-31] WORD wHour; //时[0-23] WORD wMinute; //分[0-59] WORD wSecond; //秒[0-59] WORD wMilliseconds; //毫秒[0-999] } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
(2)函数 struct tm *localtime(const time_t *clock);
与上述Linux下的描述相同。
2.3、代码实现
头文件 dateTime.h
#ifndef DATETIME_H #define DATETIME_H #include <time.h> //Windows系统时间结构体 //typedef struct _SYSTEMTIME { // WORD wYear; //年 // WORD wMonth; //月[1-12] // WORD wDayOfWeek; //星期[0-6],0代表星期天 // WORD wDay; //日[1-31] // WORD wHour; //时[0-23] // WORD wMinute; //分[0-59] // WORD wSecond; //秒[0-59] // WORD wMilliseconds; //毫秒[0-999] // } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME; //当前使用的系统环境 #define SYSTEM_ENVIRONMENT 0 //0:Linux 1:Windows //日期时间信息结构体 struct dateTime { int year; //年 int mon; //月[1-12] int mday; //日[1-31] int hour; //时[0-23] int min; //分[0-59] int sec; //秒[0-59] int msec; //毫秒[0-999] int wday; //星期[0-6],0代表星期天 int yday; //从每年1月1日开始的天数[0-365],0代表1月1日 }; //获取当前日期时间,精确到毫秒 int getCurrentDateTime(struct dateTime *now, char *str, const char *format); #if !SYSTEM_ENVIRONMENT //Linux //获取当前系统时间戳,精确到毫秒 unsigned long long getSystemTime_ms(void); //获取当前系统时间戳,精确到微秒 unsigned long long getSystemTime_us(void); //延时毫秒 void delay_ms(unsigned long long nms); //延时微秒 void delay_us(unsigned long long nus); #else //Windows //获取系统时间 int getSystemTime(SYSTEMTIME *currentTime); #endif #endif // DATETIME_H
源文件 dateTime.c
#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语言获取系统时间
2012-11-20 09:23:09c语言获取系统时间 使用函数实现c语言获取系统时间 -
c语言获取当前日期和时间
2021-09-07 23:15:20c语言获取当前日期和时间 `time_t`类型:日历时间 `time`函数:获取当前日历时间 tm 结构体:分解时间 `localtime`函数:从日历时间转换为分解时间 代码time_t
类型:日历时间typedef long time_t;time_t实际上是long型,从一个时间点(一般是1970年1月1日0时0分0秒)到当前的秒数。
time
函数:获取当前日历时间time函数可以获取当前日历时间。该函数不仅会将所求得的日历时间作为返回值返回,还会将其保存在参数所指向的变量中。
有以下三种调用方式
time(¤t)
current = time(NULL)
current = time(¤t)
tm 结构体:分解时间
表示日历时间的time _t型,是算术类型的数值,对计算机来说计算起来比较容易,但是对我们来说却不是那么直观。为此,C语言中还提供了另外一种表示时间的方法,即称为分解时间(broken-down time)的结构体数据类型tm.
struct tm{ int tm_sec; //秒(0~61) int tm_min; //分(0~59) int tm_hour; //小时(0~23) int tm_mday;//日(1~31) int tm_mon;//月份(0~11) int tm_year;//年,从1970年至今经过的年数 int tm_wday;// 星期:(0~6) int tm_yday; // 天数(0~365) int tm_isdst;//夏令时 daylight-saving time }
- 成员tm_sec 取值范围是
0~61
,而非0~59
。这是因为考虑了闰秒。
闰秒简介
localtime
函数:从日历时间转换为分解时间代码
#include <stdio.h> #include <string.h> #include <time.h> int main(void) { struct tm t; //tm结构指针 time_t now; //声明time_t类型变量 time(&now); //获取系统日期和时间 localtime_s(&t, &now); //获取当地日期和时间 //格式化输出本地时间 printf("年:%d\n", t.tm_year + 1900); printf("月:%d\n", t.tm_mon + 1); printf("日:%d\n", t.tm_mday); printf("周:%d\n", t.tm_wday); printf("一年中:%d\n", t.tm_yday); printf("时:%d\n", t.tm_hour); printf("分:%d\n", t.tm_min); printf("秒:%d\n", t.tm_sec); printf("夏令时:%d\n", t.tm_isdst); //getchar(); return 0; }
-
使用C语言获取Linux系统当前时间
2021-12-10 11:52:57C语言获取Linux系统当前时间(精确到毫秒)_Linuxer_Martin-CSDN博客_linuxc获取系统时间毫秒 c语言中获取当前时间的函数_modi000的博客-CSDN博客_c语言获取当前时间的函数 2,Convert from epoch to human-...1,精确到毫秒
gettimeofday()//可精确到微秒
clock_gettime()函数,与gettimeofday类似,但精确度到更高,可以精确到纳秒
C语言获取Linux系统当前时间(精确到毫秒)_Linuxer_Martin-CSDN博客_linuxc获取系统时间毫秒
c语言中获取当前时间的函数_modi000的博客-CSDN博客_c语言获取当前时间的函数
2,Convert from epoch to human-readable date
Epoch Converter - Unix Timestamp Converter
使用time模块 获取时间戳,毫秒__Tsun 的博客-CSDN博客_strftime 毫秒
3,use_sim_time的使用
ros:time::now()详解 - 咸鱼翻身! - 博客园
4,格式化字符串并赋给变量
1、该函数包含在stdio.h的头文件中。
2、sprintf和平时我们常用的printf函数的功能很相似。sprintf函数打印到字符串中(要注意字符串的长度要足够容纳打印的内容,否则会出现内存溢出),而printf函数打印输出到屏幕上。sprintf函数在我们完成其他数据类型转换成字符串类型的操作中应用广泛。C语言中sprintf()函数的用法_yishizuofei的博客-CSDN博客_sprintf函数的用法
#include <stdio.h> #include <time.h> #include <sys/time.h> #include <string.h> #include<iostream> using namespace std; int main() { struct timeval tv; struct timezone tz; struct tm *p; char buf[80]; string time_human; string time_h; gettimeofday(&tv, &tz); p = localtime(&tv.tv_sec); sprintf(buf,"%4d%2d%2d%2d%2d%2d.%ld", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec); time_human = buf; puts(buf); printf("Hello World\n"); time_h=time_human; cout<<time_h<<endl; printf("%s\n",time_human.c_str()); time_t rawtime; struct tm ; char buf1[80]; char buf2[80]; time(&rawtime); // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz" //ts = *localtime(&rawtime); //strftime(buf1, sizeof(buf1), "%a %Y-%m-%d %H:%M:%S %Z", &ts); p = localtime(&tv.tv_sec); strftime(buf1, sizeof(buf1), "%Y%m%d%H%M%S", p); sprintf(buf2,"%s.%ld", buf1, tv.tv_usec); printf("%s\n", buf1); puts(buf2); return 0; }
-
C语言获取时间的方法
2020-09-20 16:04:26#include<time.h> int main() { time_t timep; struct tm *p; time (&timep); p=gmtime(&... /获取当前bai秒du/ ... /获取当前分/ .../获取当前时,这里获取西方的时zhi间,刚好相差八个小时/ printf("%d\ -
linux C语言获取时间
2018-12-17 13:16:14double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0; printf("gettimeofday time: %.12f\n",span); struct timespec tpstart; struct timespec tpend; clock_gettime(CLOCK_... -
C语言:获取当前时间,并转为字符串
2021-05-20 01:15:30C语言:获取当前时间,并转为字符串,这个功能挺常用的,记录一下: #include lt;time.hgt;#include lt;stdio.hgt; int mainC语言:获取当前时间,并转为字符串,这个功能挺常用的,,记录一下:#include #include ... -
【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
2021-05-26 03:32:25一、标准C和C++都可用1、获取时间用time_ttime( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t timer0 )。精确到秒。测试程序如下:#include #include int main(){time_t start ,end ;... -
time_C语言时间获取函数_
2021-09-29 06:56:26获取系统时间,能够正确显示当前北京时间。(年月日,分时秒) -
Linux C语言获取时间 gettimeofday timeval
2019-11-19 19:49:01其参数tv是保存获取时间结果的结构体, 参数tz用于保存时区结果. timezone 参数若不使用则传入NULL即可。 timezone 结构体如下: struct timezone{ int tz_minuteswest;/*格林威治时间往西方的时差*/ int ... -
linux c 获取系统时间的方法
2021-05-13 10:53:09比如获取当前年份:/* 获取当前系统时间 暂时不使用int iyear = 0;int sysyear = 0;time_t now;struct tm *timenow;time(&now);timenow = localtime(&now);sysyear = timenow->tm_year+1900;*/获取系统... -
c语言中获取当前时间的函数
2020-04-21 14:39:36设置系统时间参考本文。 1、time()函数 #include typedef long time_t;//time_t实际是long类型数据。 time_t time( time_t * ) ; //time_t 是long类型 //返回从1970年1月1日0时0分0秒,到现在的的秒数 #include ... -
C语言获取当前的日期及时间
2018-03-18 10:36:06我们在写C语言程序的时候,有的时候会用到读取本机的时间和日期,怎么做呢?其实很简单的,下面简单说一下: C语言中读取系统时间的函数为time(),其函数原型为: #include <time.h> time_t time( time_t *... -
c语言获取当前时间
2019-03-18 16:21:43首先介绍两个函数: (1) int gettimeofday(struct timeval*tv, struct...其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果: struct timezone{ int tz_minuteswest;/*格林威治时间往西方的时差*/... -
C/C++如何获取当前系统时间的实例详解
2021-05-22 07:22:50C/C++如何获取当前系统时间的实例详解C库中与系统时间相关的函数定义在头文件中, C++定义在头文件中。一、time(time_t*)函数函数定义如下:time_t time (time_t* timer);获取系统当前日历时间 UTC 1970-01-01 00:... -
C语言获取当前的时间,秒,毫秒,纳秒
2022-05-27 16:49:22time_mil = time_sec * 1000 + time_now.tv_usec/1000; time_mic = time_now.tv_sec10001000 + time_now.tv_usec; printf(“second %ld\n”,time_sec); printf(“millisecond %ld\n”,time_mil); printf(... -
c语言获取系统时间和世界各时区时间
2021-05-21 16:03:42#include struct { int tm_min; int tm_hour; int tm_mday;... } 按格式显示冰岛时间 2009-09-24 19:26:30 按格式显 示美国凤凰城时间 2009-09-24 12:26:30 按格式显示中国北京时间 2009-09-25 03:26:30 ...... -
C例子:获取当前日期和时间
2015-08-16 20:31:36该程序是我写的博客“一起talk C栗子吧(第三十七回:C语言实例--获取当前日期和时间)”的配套程序,共享给大家使用 -
C语言获取Linux系统当前时间(精确到毫秒)
2021-06-20 04:05:30在日常工作中,有时候我们需要对时间做处理,这个时候就要能获取到系统当前时间 而且,关键时候要能精确到毫秒级别!不然精度不够!... } 以上就是C语言写的获取Linux系统当前时间!(精确到毫秒) -
c如何获取精确到毫秒的时间
2021-05-21 08:22:51使用CTime类(获取系统当前时间,精确到秒)CString str;//获取系统时间CTime tm;tm=CTime::GetCurrentTime();//获取系统日期str=tm.Format("现在时间是%Y年%m月%d日 %X");MessageBox(str,NULL,MB_OK);a,从CT... -
C语言标准库里的获取时间函数及时间格式转换详解
2021-12-17 13:55:22C语言标准库里的获取时间函数及时间格式转换详解 头文件: #include <time.h> 相关库函数(截图摘自:https://www.runoob.com/cprogramming/c-standard-library-time-h.html) 相关数据结构: struct tm ... -
C语言获取系统当前时间
2021-05-23 04:42:46函数名: time ()头文件:time.h函数原型:time_t time(time_t * timer)功 能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从UTC(Coordinated Universal Time)时间1970年1月1日00... -
C语言获取时间函数
2016-01-09 19:46:57C语言获取时间函数1.time_t time(time_t *);基本概念函数存在位置:#include 函数运行运行工具:Xcode 代码解释 time()函数获取时间; 然后换算成日历类型的时间 接着由特定tm结构的指针接收结构体 最后利用指针...