clock
std::chrono::system_clock它表示当前的系统时钟,系统中的所有进程使用now()得到的时间是一致的。每一个clock类中都有确定的time_point,duration,Rep,period类型。
函数:
|
函数
|
功能
|
|
now()
|
当前时间time_point
|
|
to_time_t()
|
time_point转换成time_t秒
|
|
from_time_t()
|
从time_t转换成time_point
|
int testTime_point(){
chrono::system_clock::time_point tp_epoch;
chrono::time_point<chrono::system_clock,chrono::seconds> tp_seconds (chrono::duration<int>(1));
chrono::system_clock::time_point tp(tp_seconds);
cout << "1 second since system_clock epoch = ";
cout << tp.time_since_epoch().count();
cout << "system clock period." << endl;
time_t tt = chrono::system_clock::to_time_t(tp);
cout << "time_point tp is:" << ctime(&tt) << endl;
return 0;
参考文献:
ctime
-
类型和结构体
-
struct tm
struct tm
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
# ifdef __USE_MISC
long int tm_gmtoff;
const char *tm_zone;
# else
long int __tm_gmtoff;
const char *__tm_zone;
# endif
-
clock_t:其实就是long int的别名,用来保存时钟计数。它的大小和CLOCK_PER_SEC有关,每个计算机的CLOCK_PER_SEC有区别。
-
time_t:通常将其实现为一个整数值,该整数值表示自UTC(1970年1月1日)00:00小时以来经过的秒数.
-
时间函数
-
clock
返回程序消耗的处理器时间。返回值类型为clock_t,它的大小与CLOCK_PER_SEC有关,一般通过clock来计算程序的实际处理时间,在调用程序前后分别调用clock()函数,计算两者的差值。要想得到消耗的实际时间需要将结果除以CLOCK_PER_SEC.
例子:
#include <stdio.h>
#include <time.h>
#include <math.h>
using namespace std;
int testCtimeClock(){
clock_t t;
int f;
t = clock();
printf("Calculating...\n");
int freq = 999998;
for(int i = 2;i <= 999999;++i){
for(int j = sqrt(i);j > 1;--j){
if(i%j == 0){
--freq;
break;
printf("The number of primes lower than 1000000 is:%d\n",freq);
t = clock() - t;
printf("It took me %d clicks(%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
return 0;
int main(int argc,char *argv[]){
testCtimeClock();
return 0;
-
time
获取当前时间time_t的值。该函数返回该值,并且入座参数不是空指针,则将此值设置为传入的指针参数的值。返回的值是相对于UTC1970年1月1日00:00::00以来的秒数。
#include <stdio.h>
#include <time.h>
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);
seconds = difftime(timer,mktime(&y2k));
printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
return 0;
-
mktime
将tm结构转换为time_t。
#include <stdio.h>
#include <time.h>
int main ()
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
const char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
printf ("Enter year: "); fflush(stdout); scanf ("%d",&year);
printf ("Enter month: "); fflush(stdout); scanf ("%d",&month);
printf ("Enter day: "); fflush(stdout); scanf ("%d",&day);
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
mktime ( timeinfo );
printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
return 0;
-
difftime
计算两个时间之间的差值(一秒为单位)。
函数定义为:
extern double difftime (time_t __time1, time_t __time0)
例子:
int testCtimeDifftime(){
time_t now;
struct tm newyear;
double seconds;
time(&now);
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;
-
asctime
将结构体tm转换为字符串,形式为:Www Mmm dd hh:mm:ss yyyy
asctime函数可以看作如下:
char* asctime(const struct tm *timeptr)
static const char wday_name[][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
static const char mon_name[][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
static char result[26];
sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
wday_name[timeptr->tm_wday],
mon_name[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
1900 +
timeptr->tm_year);
return result;
例子:
#include <stdio.h>
#include <time.h>
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值转换为字符串,形式为Www Mmm dd hh:mm:ss yyyy
此功能等同于:
asctime(localtime(timer));
例子:
#include <stdio.h>
#include <time.h>
int main ()
time_t rawtime;
time (&rawtime);
printf ("The current local time is: %s", ctime (&rawtime));
return 0;
-
struct tm * gmtime(const time_t * timer);
将time_t转换为tm作为UTC时间
例子:
#include <stdio.h>
#include <time.h>
#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;
-
struct tm * localtime(const time_t * timer);
将time_t转换为本地时间,使用timer指向的值来填充tm结构体,其值代表相应的时间,以当地时区表示。
例子:
#include <stdio.h>
#include <time.h>
int main ()
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));
return 0;
-
size_t strftime (char ptr, size_t maxsize, const char format,const struct tm* timeptr );**
将时间格式化为字符串,将格式化的内容复制到ptr中,将其格式化说明符扩展为表示timeptr中描述的时间的相应值,并限制为maxsize个字符。如果生成的C字符串的长度(包括终止的空字符)不超过maxsize,则该函数返回复制到ptr的字符总数(不包括终止的空字符)。否则,它返回零,并且ptr指向的数组的内容是不确定的。
参数说明
| 参数 | 解释 |
|---|
| ptr | 指向将目标字符串复制到的目标数组的指针 |
| maxsize | 要复制到ptr的最大字符数,包括终止的null字符 |
| format | 字符串,包含常规字符和特殊格式说明符的任意组合。这些格式说明符由函数替换为相应的值,以表示timeptr中指定的时间。它们都以百分号(%)开头 |
| timeptr | tm结构体指针 |
格式说明
| 说明符 | 取而代之 | 例 |
|---|
%a | 工作日缩写名称* | Thu |
%A | 工作日全名* | Thursday |
%b | 月份缩写名称* | Aug |
%B | 月份全名* | August |
%c | 日期和时间表示* | Thu Aug 23 14:55:02 2001 |
%C | 年份除以100,并截断为整数(00-99) | 20 |
%d | 每月的某天,零填充(01-31) | 23 |
%D | 短MM/DD/YY日期,相当于%m/%d/%y | 08/23/01 |
%e | 每月的某天,以空格填充(1-31) | 23 |
%F | 短YYYY-MM-DD日期,相当于%Y-%m-%d | 2001-08-23 |
%g | 以周为基础的年份,后两位数字(00-99) | 01 |
%G | 基于周的年份 | 2001 |
%h | 缩写的月份名称*(与相同%b) | Aug |
%H | 24小时制小时(00-23) | 14 |
%I | 12小时制小时(01-12) | 02 |
%j | 一年中的一天(001-366) | 235 |
%m | 以十进制数表示的月份(01-12) | 08 |
%M | 分钟(00-59) | 55 |
%n | 换行符('\n') | `` |
%p | AM或PM指定 | PM |
%r | 12小时制* | 02:55:02 pm |
%R | 24小时HH:MM制,相当于%H:%M | 14:55 |
%S | 秒(00-61) | 02 |
%t | 水平制表符('\t') | `` |
%T | ISO 8601时间格式(HH:MM:SS),等效于%H:%M:%S | 14:55:02 |
%u | ISO 8601工作日为数字,星期一为1(1-7) | 4 |
%U | 以第一个星期日为第一个星期的第一天的星期数(00-53) | 33 |
%V | ISO 8601周编号(01-53) | 34 |
%w | 工作日为小数,星期日为0(0-6) | 4 |
%W | 第一个星期一作为第一个星期的第一天的星期数(00-53) | 34 |
%x | 日期表示* | 08/23/01 |
%X | 时间表示* | 14:55:02 |
%y | 年,最后两位数字(00-99) | 01 |
%Y | 年 | 2001 |
%z | ISO 8601与时区UTC的偏移量(1分钟= 1,1小时= 100) 如果无法确定时区,则没有字符 | +100 |
%Z | 时区名称或缩写* 如果无法确定时区,则没有字符 | CDT |
%% | 一个%标志 | % |
例子:
#include <stdio.h>
#include <time.h>
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;
炎热的夏日,KC 非常的不爽。他宁可忍受北极的寒冷,也不愿忍受厦门的夏天。最近,他开始研究天气的变化。他希望用研究的结果预测未来的天气。
经历千辛万苦,他收集了连续 N(1≤N≤106) 的最高气温数据。
现在,他想知道最高气温一直上升的最长连续天数。
输入输出格式
输入格式:
第 1 行:一个整数 N 。1≤N≤106
第 2 行:N个空格隔开的整数,表示连续 N 天的最高气温。0≤ ...
文章目录一. 需要用到的相关知识1. 日期类实现2. 蔡勒公式二. 日期类题目1、淘宝网店2、一周中的第几天3、美国节日4、计算日期到天数转换
一. 需要用到的相关知识
1. 日期类实现
主要是通过operator++()来计算两个日期之间相差的天数。具体实现看下面这篇博客:日期类模拟实现。
2. 蔡勒公式
作用是通过输入的年月日算出今天是周几。
公式:W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1 (其中[ ]为取整符号)
W是所求日期的星期数;
如果求得的数大于7,可以直接对
该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include#define N 13main(){int y,m,D,q,t=0,i,day=0,a=0,Day,n,k,O[N]={0,31,29,31,30,31,30,31,31,30,31,30,31},p[N]={0,31,28,31,30,31,30,31,31,30,31,30,31};//y是年,m是月,D是日,q计算周几,t,i,k...
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calenda...
日期和时间是编程中非常常用的功能。本文是对C++11到C++17中相关编程接口的介绍。介绍C++中可以使用的日期时间API主要分为两类:C-style 日期时间库,位于<ctime&...