-
2021-05-20 17:02:13
1 使用time_t time( time_t * timer ) 精确到秒
函数名: time
头文件:time.h
函数原型:time_t time(time_t * timer)
功能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒
clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。
对clock函数定义如下:clock_t clock(void) ;
简单而言,就是该程序从启动到函数调用占用CPU的时间。这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型,在time.h文件中
include
#include
#include
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double duration;
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while( i-- );
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds ", duration );
system("pause");
}
上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。
3 计算时间差使用double difftime( time_t timer1, time_t timer0 )
4 使用DWORD GetTickCount() 精确到毫秒
GetTickCount(),这个是windows里面常用来计算程序运行时间的函数;
DWORD dwStart = GetTickCount();
//这里运行你的程序代码
DWORD dwEnd = GetTickCount();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位
这个函数只精确到55ms,1个tick就是55ms。
5 要获取高精度时间,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
获取系统的计数器的频率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
获取计数器的值
然后用两次计数器的差除以Frequency就得到时间。
更多相关内容 -
Android获取系统时间以及网络时间
2020-09-02 00:37:10主要为大家详细介绍了Android获取系统时间以及网络时间的方法,感兴趣的小伙伴们可以参考一下 -
JS获取系统日期并输出多种格式
2020-06-06 00:57:17JS获取系统日期并输出多种格式 -
sql server 获取系统时间的方法
2020-12-15 02:44:07Sql Server 中一个非常强大的日期格式化函数: 获得当前系统时间,GETDATE(): 2008年01月08日 星期二 14:59 Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2008 10:57AM Select CONVERT(varchar(100), GETDATE... -
LINUX 下C++ 获取系统时间和设置时间
2021-04-23 17:21:02LINUX 下C++ 获取系统时间和设置时间,是个类,已经测试通过,需要用管理员用户 -
C#如何获取系统日期时间
2021-03-16 03:30:48摘要:C#源码,系统相关,日期时间 C#如何获取系统日期时间,C#获取当前时间并出现日期选择器可以改变时间,C#获取当前时间并可以调整时间。 -
C++获取当前系统时间的方法总结
2021-01-01 01:39:23本文实例讲述了C++获取当前系统时间的方法。分享给大家供大家参考。具体如下: 方案— 优点:仅使用C标准库;缺点:只能精确到秒级 #include #include int main( void ) { time_t t = time(0); char tmp[64]; ... -
Android开发获取当前系统日期和时间功能示例
2020-08-26 02:32:14主要介绍了Android开发获取当前系统日期和时间功能,结合实例形式分析了Android布局、事件响应、监听以及时间获取相关操作技巧,需要的朋友可以参考下 -
获取系统日期,星期,时间显示
2017-04-05 15:16:37获取系统日期,星期,时间显示 -
sntp.zip(ESP32获取系统时间)
2019-07-31 17:02:30简单的DEMO,在官方的里程上简单的整理一下和加了一些备注。请看清楚描述才考虑下载不? -
易语言获取系统时间到毫秒
2020-07-21 15:20:34易语言获取系统时间到毫秒源码,获取系统时间到毫秒,系统时间到毫秒 -
Android获取系统时间的多种方法
2021-05-26 13:49:47Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现。现总结如下:方法一:void getTime1(){long time=System.currentTimeMillis();//long now = android....Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现。
现总结如下:
方法一:
void getTime1(){
long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=new Date(time);
String t1=format.format(d1);
Log.e("msg", t1);
}
方法二:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String t=format.format(new Date());
Log.e("msg", t);
方法三:
void getTime3(){
Calendar calendar = Calendar.getInstance();
String created = calendar.get(Calendar.YEAR) + "年"
+ (calendar.get(Calendar.MONTH)+1) + "月"//从0计算
+ calendar.get(Calendar.DAY_OF_MONTH) + "日"
+ calendar.get(Calendar.HOUR_OF_DAY) + "时"
+ calendar.get(Calendar.MINUTE) + "分"+calendar.get(Calendar.SECOND)+"s";
Log.e("msg", created);
}
方法四:
void getTime4(){
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second;
Log.e("msg", time);
}
获取星期日期:
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
String today = null;
if (day == 2) {
today = "Monday";
} else if (day == 3) {
today = "Tuesday";
} else if (day == 4) {
today = "Wednesday";
} else if (day == 5) {
today = "Thursday";
} else if (day == 6) {
today = "Friday";
} else if (day == 7) {
today = "Saturday";
} else if (day == 1) {
today = "Sunday";
}
System.out.println("Today is:- " + today);
最后说一下日期格式化,日期格式化通常使用SimpleDateFormat类实现,其中的日期格式不能够自己随意定义,主要有以下几种形式:
SimpleDateFormat f1= new SimpleDateFormat(); //其中没有些格式化参数,我们使用默认的日期格式。
System.out.println(f.formate(new Date()));
代码输出的日期格式为:12-3-22 下午4:36
SimpleDateFormat f4= new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");
//可根据不同样式请求显示不同日期格式,要显示星期可以添加E参数
System.out.println(f4.format(new Date()));
//代码输出的日期格式为:今天是2012年03月22日 星期四 16点46分
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("Date to String "+formater.format(new Date()));
//相近的常用形式还有 yyMMdd hh:mm:ss yyyy-MM-dd hh:mm:ss dd-MM-yyyy hh:mm:ss
应有的时候通常还会需要把具体日期转换为毫秒或者Timestamp形式,如下:
文本 - > Timestamp,日期 -> Timestamp
Timestamp t ;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try ...{
t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime());
} catch (ParseException e) ...{
e.printStackTrace();
}
Timestamp t ;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
t = new Timestamp(new Date().getTime());
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
-
cmd下获取当前系统时间的bat
2020-09-17 17:18:53编写Windows批处理时经常会需要使用到日期和时间作为文件名,每次备份都可以使用不同名字,所以是非常重要的 -
mysql 获取系统时间
2021-01-19 01:56:23**####mysql数据库中获取系统时间**#获取当前系统时间 2020-01-08 15:13:19select SYSDATE() from BIZ_BILL_DETAIL t;#获取当前系统时间,年月日2020-01-08select current_date() from BIZ_BILL_DETAIL t;#获取当前...**####mysql数据库中获取系统时间**
#获取当前系统时间 2020-01-08 15:13:19
select SYSDATE() from BIZ_BILL_DETAIL t;
#获取当前系统时间,年月日2020-01-08
select current_date() from BIZ_BILL_DETAIL t;
#获取当前系统年份
select year(current_date()) from BIZ_BILL_DETAIL t;
#获取当前系统月份
select month(current_date()) from BIZ_BILL_DETAIL t;
#获取当前系统日
select day(current_date()) from BIZ_BILL_DETAIL t;
#获取当前系统时间 15:16:11
select time(SYSDATE()) from BIZ_BILL_DETAIL t;
#获取当前系统小时
select HOUR(SYSDATE()) from BIZ_BILL_DETAIL t;
#获取当前系统分钟
select MINUTE(SYSDATE()) from BIZ_BILL_DETAIL t;
#获取当前系统秒
select SECOND(SYSDATE()) from BIZ_BILL_DETAIL t;
#获取当前系统毫秒
select MICROSECOND(SYSDATE()) from BIZ_BILL_DETAIL t;
**##mysql数据库中获取系统时间**
select CURDATE() from BIZ_BILL_DETAIL t;
#利用to_days函数
select TO_DAYS(NOW()-TO_DAYS('19930908')) from BIZ_BILL_DETAIL t;
#利用datediff函数
select DATEDIFF(NOW(),'19930908') from BIZ_BILL_DETAIL t;
#获取当月最后一天
select LAST_DAY(CURDATE()) from BIZ_BILL_DETAIL t;
#获取本月第一天
select date_add(CURDATE(),INTERVAL-day(CURDATE())+1 day) from BIZ_BILL_DETAIL t;
#获取下个月的第一天
select date_add(CURDATE()-day(CURDATE())+1,INTERVAL 1 MONTH) from BIZ_BILL_DETAIL t;
#获取当前月的天数
select DATEDIFF(DATE_ADD(CURDATE()-day(CURDATE())+1,INTERVAL 1 month),DATE_ADD(CURDATE(),INTERVAL-day(CURDATE())+1 day)) from BIZ_BILL_DETAIL t;
#获取当前系统time
select CURTIME() from BIZ_BILL_DETAIL t;
#-获取当前系统 日期+时间
#sysdate()、current_timestamp()、now()、localtime()、localtimestamp()
select current_timestamp() from BIZ_BILL_DETAIL t;
**Mysql数据库中计算两个日期之间的时间差**
1、利用TO_DAYS函数
select to_days(now()) - to_days('19930908')
2、利用DATEDIFF函数
select datediff(now(),'19930908')
参数1 - 参数2 等于间隔天数
3、利用TIMESTAMPDIFF函数
计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() 实现。
函数 TimeStampDiff() 是MySQL本身提供的可以计算两个时间间隔的函数,语法为:
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差。其中unit单位有如下几种,分别是:FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR 。该参数具体释义如下:
FRAC_SECOND 表示间隔是毫秒SECOND 秒MINUTE 分钟HOUR 小时DAY 天WEEK 星期MONTH 月QUARTER 季度YEAR 年
例如:
#计算两日期之间相差多少周
select timestampdiff(week,'2011-09-30','2015-05-04');
#计算两日期之间相差多少天
**Mysql数据库中计算两个日期之间的时间差**
TIMESTAMPDIFF,(如果当期时间和之前时间的分钟数相比较。大于1天,即等于1;小于1天,则等于0)
select TIMESTAMPDIFF(DAY,'2016-11-16 10:13:42',NOW());
DATEDIFF,(只按2016-11-16计算,不会加小时分钟数,按天计算)
select DATEDIFF(NOW(),'2016-11-16 17:10:52');
**mysql分别获取已有数据date中的年月日(单个提取)**
select YEAR(DATE) from test; //获取年
select day(DATE) from test; //获取日
select month(DATE) from test; //获取月
select DATE(CURDATE()) //获取日期
select CONCAT(YEAR(CURDATE()),'-','01') //获取本年第一月
**mysql分别获取已有数据date中的年月日(单个提取)**
select left(signDate,7) as date from tablename
select date_format(日期字段,'%Y-%m') as '日期' from 表
-
VC++ 获取系统时间的方法汇总
2021-01-20 05:28:17//获取系统日期 str=tm.Format(现在时间是%Y年%m月%d日 %X); MessageBox(str,NULL,MB_OK); a,从CTimet中提取年月日时分秒 CTime t = CTime::GetCurrentTime(); int d=t.GetDay(); //获得几号 int y=t.GetYear... -
asp.net获取系统当前时间的方法详解
2020-09-02 03:40:31主要介绍了asp.net获取系统当前时间的方法,较为详细的分析了C#日期与时间操作所涉及的相关函数与使用技巧,需要的朋友可以参考下 -
高性能获取系统时间
2018-07-22 02:01:47高并发场景下System.currentTimeMillis()的性能问题的优化 -
MySQL 获取系统时间/系统日期/日期时间的函数
2020-07-29 15:26:261.获取当前日期+时间 now(); — —> select now(); //或者 :select now() as Systemtime; sysdate(); — —> select sysdate(); //或者 :select sysdate() as Systemtime; 这两个函数都是获取当前日期+...一、获取当前系统日期和时间
(一)now(),返回当前的系统日期和时间
now()
函数以YYYY-MM-DD HH:MM:SS
返回系统当前的日期时间,可以直接存到DATETIME
字段中。下面的sysdate()
函数也是如此。mysql> select now(); +---------------------+ | now() | +---------------------+ | 2021-06-04 08:24:51 | +---------------------+ 1 row in set (0.00 sec)
(二)sysdate(),返回当前的系统日期和时间
mysql> select sysdate(); +---------------------+ | sysdate() | +---------------------+ | 2021-06-04 08:27:35 | +---------------------+ 1 row in set (0.00 sec)
now()
和sysdate()
这两个函数都是获取当前日期+时间,不同之处在于:now()
在执行开始时值就得到了,sysdate()
在函数执行时动态得到值。(三)current_timestamp(),返回当前的系统日期和时间
mysql> select current_timestamp(); +---------------------+ | current_timestamp() | +---------------------+ | 2021-06-04 14:12:37 | +---------------------+ 1 row in set (0.00 sec)
(四)localtime(),返回当前的系统日期和时间
mysql> select localtime(); +---------------------+ | localtime() | +---------------------+ | 2021-06-04 14:13:54 | +---------------------+ 1 row in set (0.00 sec)
二、获取当前的系统日期
(一)curdate(),返回当前的系统日期
curdate()
以YYYY-MM-DD
的格式返回今天的日期,可以直接存到DATE
字段中。下面函数两个也是如此。mysql> select curdate(); +------------+ | curdate() | +------------+ | 2021-06-04 | +------------+ 1 row in set (0.00 sec)
(二)current_date(),返回当前的系统日期
mysql> select current_date(); +----------------+ | current_date() | +----------------+ | 2021-06-04 | +----------------+ 1 row in set (0.00 sec)
(三)current_date,返回当前的系统日期
mysql> select current_date; +--------------+ | current_date | +--------------+ | 2021-06-04 | +--------------+ 1 row in set (0.00 sec)
三、获取当前的系统时间
(一)curtime(),返回当前的系统时间
curtime()
以HH:MM:SS
的格式返回当前的时间,可以直接存到TIME
字段中。以下两个函数也是如此。
举例如下:mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 10:55:13 | +-----------+ 1 row in set (0.03 sec)
(二)current_time(),返回当前的系统时间
(三)current_time,返回当前的系统时间
四、获取 UTC 时间
协调世界时,又称世界统一时间,世界标准时间,国际协调时间,简称 UTC。
(一)utc_date(),获取 UTC 日期
mysql> select utc_date(); +------------+ | utc_date() | +------------+ | 2021-06-04 | +------------+ 1 row in set (0.15 sec)
(二)utc_time(),获取 UTC 时间
mysql> select utc_time(); +------------+ | utc_time() | +------------+ | 06:47:26 | +------------+ 1 row in set (0.00 sec)
(三)utc_timestamp(),获取 UTC 的日期和时间
mysql> select utc_timestamp(); +---------------------+ | utc_timestamp() | +---------------------+ | 2021-06-04 07:04:35 | +---------------------+ 1 row in set (0.00 sec)
五、日期和时间显示格式说明
编程狮(w3cschool.cn) 值 含义 秒 %S、%s 两位数字形式的秒( 00,01, ..., 59) 分 %I、%i 两位数字形式的分( 00,01, ..., 59) 小时 %H 24小时制,两位数形式小时(00,01, ...,23) %h 12小时制,两位数形式小时(00,01, ...,12) %k 24小时制,数形式小时(0,1, ...,23) %l 12小时制,数形式小时(0,1, ...,12) %T 24小时制,时间形式(HH:mm:ss) %r 12小时制,时间形式(hh:mm:ss AM 或 PM) %p AM上午或PM下午 周 %W 一周中每一天的名称(Sunday,Monday, ...,Saturday) %a 一周中每一天名称的缩写(Sun,Mon, ...,Sat) %w 以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday) %U 数字表示周数,星期天为周中第一天 %u 数字表示周数,星期一为周中第一天 天 %d 两位数字表示月中天数(01,02, ...,31) %e 数字表示月中天数(1,2, ...,31) %D 英文后缀表示月中天数(1st,2nd,3rd ...) %j 以三位数字表示年中天数(001,002, ...,366) 月 %M 英文月名(January,February, ...,December) %b 英文缩写月名(Jan,Feb, ...,Dec) %m 两位数字表示月份(01,02, ...,12) %c 数字表示月份(1,2, ...,12) 年 %Y 四位数字表示的年份(2015,2016...) %y 两位数字表示的年份(15,16...) 文字输出 %文字 直接输出文字内容 -
kettle获取系统时间
2021-01-27 03:56:15Date.prototype.Format = function (fmt) { //author: ...--new 当前时间对象 2代表2天 24*60*60*1000代表2天时间的ms数 --即当天减去2天时间ms数 得到前天的时间 --------------------- 作者:果木 来源:CSDN 原文:... -
Android系统当前时间日期的获取
2015-11-17 18:04:13整理出来的获取Android系统当前时间日期,看log -
windows平台获取系统时间
2021-12-15 14:02:27windows平台获取系统时间 windows平台获取系统日期 windows平台获取系统时间,含毫秒数 -
WPF获取系统时间
2014-10-17 12:45:05WPF获取系统时间,另外还有WPF复杂属性问题,事例:解决渐变色问题 -
Android 获取系统日期时间的两种方式
2020-10-18 13:10:56一、java.util.Date private void getDateTime1() { //DateFormat dateInstance = ...//系统预置 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(Syste -
Java获取系统时间的几种方法
2021-02-12 09:30:20//系统日期从0开始算起 day=c.get(Calendar.DAY_OF_MONTH); System.out.println(year+""+month+" "+day); } public static void main(String args[]){ NewClass ne =new NewClass(); ne.time(); } 分享到: 2012-06-... -
SQL获取当前时间(日期)
2018-05-22 17:07:40--获取当前日期(如:yyyymmdd) select CONVERT (nvarchar(12),GETDATE(),112) -
C语言获取系统时间的几种方式
2021-05-22 15:26:05核心提示:C语言中如何获取时间?精度如何?1使用time_ttime(time_t*timer)精确到秒2使用clock_tclock()得到的是CPU时间精确到1/CLOCKS_PER_SEC秒3计算时间差使用doubledifftime(...C语言中如何获取时间?精度如何?... -
C#获取Windows系统的安装日期和启动时间
2021-05-09 05:39:30摘要:C#源码,系统相关,安装日期,启动时间 C#获取Windows系统的安装日期和启动时间,日期格式没有设置,有需要的可以自己设置日期时间的格式吧。 -
获取系统时间的几种方法
2021-08-26 14:22:35该方法用于在命令提示符中获取系统当前时间信息(只有时间,没有日期),并提示重新输入时间信息修改当前时间信息 2.利用API函数GetLocalTime 该方法获取当前日期和时间:注意(需要包含windows.h头文件) ...