当前位置:网站首页>日期、时间库使用备注
日期、时间库使用备注
2022-06-24 06:44:00 【雾散睛明】
日期、时间库
c/c++ 标准库
clock():返回程序消耗处理器时间
示例:
/* clock example: frequency of primes */
#include <stdio.h> /* printf */
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h> /* sqrt */
int frequency_of_primes (int n) {
int i,j;
int freq=n-1;
for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {
--freq; break;}
return freq;
}
int main ()
{
clock_t t;
int f;
t = clock();
printf ("Calculating...\n");
f = frequency_of_primes (99999);
printf ("The number of primes lower than 100,000 is: %d\n",f);
t = clock() - t;
printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
return 0;
}
difftime:函数计算两个时间的差值
/* difftime example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
int main ()
{
time_t now;
struct tm newyear;
double seconds;
time(&now); /* get current time; same as: now = time(NULL) */
newyear = *localtime(&now);
newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
newyear.tm_mon = 0; newyear.tm_mday = 1;
seconds = difftime(now,mktime(&newyear));
printf ("%.f seconds since new year in the current timezone.\n", seconds);
return 0;
}
mktime:将struct tm结构转成time_t的类型。
/* mktime example: weekday calculator */
#include <stdio.h> /* printf, scanf */
#include <time.h> /* time_t, struct tm, time, mktime */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
const char * weekday[] = {
"Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
/* prompt user for date */
printf ("Enter year: "); fflush(stdout); scanf ("%d",&year);
printf ("Enter month: "); fflush(stdout); scanf ("%d",&month);
printf ("Enter day: "); fflush(stdout); scanf ("%d",&day);
/* get current timeinfo and modify it to the user's choice */
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );
printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
return 0;
}
time:获取当前时间转换成time_t 类型
/* time example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
int main ()
{
time_t timer;
struct tm y2k = {
0};
double seconds;
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
time(&timer); /* get current time; same as: timer = time(NULL) */
seconds = difftime(timer,mktime(&y2k));
printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
return 0;
}
转换:
asctime:转换struct tm结构 变为字符串。
示例:
/* asctime example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, time, localtime, asctime */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) );
return 0;
}
ctime:将time_t 转换成字符串
/* ctime example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, time, ctime */
int main ()
{
time_t rawtime;
time (&rawtime);
printf ("The current local time is: %s", ctime (&rawtime));
return 0;
}
gmtime:转换time_t 为 struct tm UTC 时间
/* gmtime example */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */
#define MST (-7)
#define UTC (0)
#define CCT (+8)
int main ()
{
time_t rawtime;
struct tm * ptm;
time ( &rawtime );
ptm = gmtime ( &rawtime );
puts ("Current time around the World:");
printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);
return 0;
}
localtime 函数将time_t 转换成本地struct tm结构。
/* localtime example */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));
return 0;
}
strftime struct tm结构格式化时间为字符串
%a Abbreviated weekday name * Thu
%A Full weekday name * Thursday
%b Abbreviated month name * Aug
%B Full month name * August
%c Date and time representation * Thu Aug 23 14:55:02 2001
%C Year divided by 100 and truncated to integer (00-99) 20
%d Day of the month, zero-padded (01-31) 23
%D Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
%e Day of the month, space-padded ( 1-31) 23
%F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23
%g Week-based year, last two digits (00-99) 01
%G Week-based year 2001
%h Abbreviated month name * (same as %b) Aug
%H Hour in 24h format (00-23) 14
%I Hour in 12h format (01-12) 02
%j Day of the year (001-366) 235
%m Month as a decimal number (01-12) 08
%M Minute (00-59) 55
%n New-line character (’\n’)
%p AM or PM designation PM
%r 12-hour clock time * 02:55:02 pm
%R 24-hour HH:MM time, equivalent to %H:%M 14:55
%S Second (00-61) 02
%t Horizontal-tab character (’\t’)
%T ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S 14:55:02
%u ISO 8601 weekday as number with Monday as 1 (1-7) 4
%U Week number with the first Sunday as the first day of week one (00-53) 33
%V ISO 8601 week number (01-53) 34
%w Weekday as a decimal number with Sunday as 0 (0-6) 4
%W Week number with the first Monday as the first day of week one (00-53) 34
%x Date representation * 08/23/01
%X Time representation * 14:55:02
%y Year, last two digits (00-99) 01
%Y Year 2001
%z ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)
If timezone cannot be determined, no characters +100
%Z Timezone name or abbreviation *
If timezone cannot be determined, no characters CDT
%% A % sign %
- The specifiers marked with an asterisk (*) are locale-dependent.
Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99. Since C99, two locale-specific modifiers can also be inserted between the percentage sign (%) and the specifier proper to request an alternative format, where applicable:
Modifier Meaning Applies to
E Uses the locale’s alternative representation %Ec %EC %Ex %EX %Ey %EY
O Uses the locale’s alternative numeric symbols %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy
示例:
/* strftime example */
#include <stdio.h> /* puts */
#include <time.h> /* time_t, struct tm, time, localtime, strftime */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);
puts (buffer);
return 0;
}
c++ 库:
chrono
boost库:
date_time库
参考:https://blog.csdn.net/u010261063/article/details/86555905
边栏推荐
- atguigu----16-自定义指令
- bjdctf_2020_babystack
- Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
- 与(&&)逻辑或(||),动态绑定结合三目运算
- Win10 build webservice
- Combine with (& &) logic or (||), dynamic binding and ternary operation
- 《canvas》之第2章 直线图形
- [image fusion] multi focus and multi spectral image fusion based on pixel saliency and wavelet transform with matlab code
- MSSQL high permission injection write horse to Chinese path
- Bjdctf 2020 Bar _ Babystack
猜你喜欢

图形技术之管线概念

MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics

bjdctf_2020_babystack
![[GUET-CTF2019]zips](/img/79/22ff5d4a3cdc3fa9e0957ccc9bad4b.png)
[GUET-CTF2019]zips

只显示两行,超出部分省略号显示

2022年PMP项目管理考试敏捷知识点(1)
![LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路](/img/16/011ba3aef1315c39526daac7e3ec89.png)
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
![[OGeek2019]babyrop](/img/74/5f93dcee9ea5a562a7fba5c17aab76.png)
[OGeek2019]babyrop
![[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer](/img/45/61258aa20cd287047c028f220b7f7a.png)
[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer
![[mrctf2020] thousand layer routine](/img/8e/d7b6e7025b87ea0f43a6123760a113.png)
[mrctf2020] thousand layer routine
随机推荐
MaxCompute远程连接,上传、下载数据文件操作
Description of module data serial number positioning area code positioning refers to GBK code
Analog display of the module taking software verifies the correctness of the module taking data, and reversely converts the bin file of the lattice array to display
More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
Tencent cloud security and privacy computing has passed the evaluation of the ICT Institute and obtained national recognition
[pointnet] matlab simulation of 3D point cloud target classification and recognition based on pointnet
10 common malware detection and analysis platforms
[wustctf2020] climb
Unity Culling 相关技术
Shell script for MySQL real-time synchronization of binlog
[WordPress website] 5 Set code highlight
UE常用控制臺命令
PNAs: Geometric renormalization reveals the self similarity of multi-scale human connectome
L2TP connection failure guide in VPN
How to realize multi protocol video capture and output in video surveillance system?
选择器(>,~,+,[])
第三方软件测试公司如何选择?2022国内软件测试机构排名
相机标定(标定目的、原理)
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode