tm_isdst
mktime() 将 struct tm 描述的时间转换成时间戳,tm_isdst 只是传入的时间是否是 DST(夏令时),tm_isdst 含有:
1: 是 DST
0: 不是 DST
-1: 由 mktime() 自己去判断当前系统设置是否是 DST
注意:有些实现是判断大于0还是小于0,没有限定为 1 和 -1, 但在我的 Linux 系统中,限定为 1 和 -1 了。
同事测的传入 1 和 0 的i性能对比:
1: 1w 次耗时 2s
0: 1w 次耗时 0.02s
是否相差一小时
在我的系统中测试时,2001 年以前的时间赋值为 1 会比其他情况晚 1 小时,2001 及以后无论赋值多少都是一样。
tm_isdstmktime() 将 struct tm 描述的时间转换成时间戳,tm_isdst 只是传入的时间是否是 DST(夏令时),tm_isdst 含有:>0: 是 DST=0: 不是 DST性能同事测的传入 1 和 0 的i性能对比:1: 1w 次耗时 2s0: 1w 次耗时 0.02s
连接:http://www.educity.cn/wenda/248940.h
tm
l
mk
time
中
的
tm
_is
dst
不对
tm
结构体
中
的
tm
_is
dst
赋值,在不同机器上会出现
tm
_is
dst
默认值不同
也就是说最终,
mk
time
算出来的时候会有一个小时的差别。
#include
#include
int main(void) {
tm
mask
Time
tm
;
该时区的某些地区在 3 月的第二个星期日和 11 月的第一个星期日之间观察到夏令时。
var is
DST
= require ( "is
dst
" ) . is
DST
;
var now = new Date ( ) ;
var
dst
= is
DST
( now ) ;
console . log (
dst
? "Daylight Savings
Time
" : "Standard
Time
" ) ;
麻省理工学院
Brennan Stehling (@brennan
mk
e, @smallsharptools)
1.python
中
的时间操作
python
中
time
、date
time
两个模块均可进行时间操作;二者均可以获取时间、转换时间;
time
模块特有进行进程阻塞,date
time
模块特有计算时间差;
时间戳:指以1970年1月1日 00:00:00 UTC+00:00为基准(记为0),当前时刻距离基准的时间间隔;时间戳只表示时间间隔,与时区无关;python
中
时间戳是浮点数,表示距离基准的秒数,Java
中
时间戳是整数,表示距离基准的毫秒数,二者相差1000倍;
一 时间获取
time
直接获取的是时间戳,dat
1.函数说明
1.1描述
time
_t
mk
time
(
struct
tm
*
time
ptr) 把
time
ptr 所指向的结构转换为自 1970 年 1 月 1 日以来持续时间的秒数,发生错误时返回-1。
1.2.声明
time
_t
mk
time
(
struct
tm
*
time
ptr)
1.3.参数
struct
tm
int
tm
_sec; /* 秒 – 取值[0,59] */
int
tm
_min; /* 分 - 取值[0,59] */
long
time
zone = 0;
_VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __
time
64_t )( -1 ) ) )
* First, make sure
tm
_year is reasonably close to being in range.
if ( ((
tm
p
tm
1 = tb->
tm
_year) < _BASE_YEAR - 1) || (
tm
p
tm
1 > _MAX_YEAR64
+ 1) )
goto err_
mk
time
;
* Adjust month value so it is in the range 0 - 11. This is because
* we don't know how many days are in months 12, 13, 14, etc.
if ( (tb->
tm
_mon < 0) || (tb->
tm
_mon > 11) ) {
tm
p
tm
1 += (tb->
tm
_mon / 12);
if ( (tb->
tm
_mon %= 12) < 0 ) {
tb->
tm
_mon += 12;
tm
p
tm
1--;
* Make sure year count is still in range.
if ( (
tm
p
tm
1 < _BASE_YEAR - 1) || (
tm
p
tm
1 > _MAX_YEAR64 + 1) )
goto err_
mk
time
;
/***** HERE:
tm
p
tm
1 holds number of elapsed years *****/
* Calculate days elapsed minus one, in the given year, to the given
* month. Check for leap year and adjust if necessary.
tm
p
tm
2 = _days[tb->
tm
_mon];
if ( _IS_LEAP_YEAR(
tm
p
tm
1) && (tb->
tm
_mon > 1) )
tm
p
tm
2++;
* Calculate elapsed days since base date (midnight, 1/1/70, UTC)
* 365 days for each elapsed year since 1970, plus one more day for
* each elapsed leap year. no danger of overflow because of the range
* check (above) on
tm
p
tm
1.
tm
p
tm
3 = (
tm
p
tm
1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(
tm
p
tm
1);
* elapsed days to current month (still no possible overflow)
tm
p
tm
3 +=
tm
p
tm
2;
* elapsed days to current date.
tm
p
tm
1 =
tm
p
tm
3 + (
tm
p
tm
2 = (__
time
64_t)(tb->
tm
_mday));
/***** HERE:
tm
p
tm
1 holds number of elapsed days *****/
* Calculate elapsed hours since base date
tm
p
tm
2 =
tm
p
tm
1 * 24;
tm
p
tm
1 =
tm
p
tm
2 + (
tm
p
tm
3 = (__
time
64_t)tb->
tm
_hour);
/***** HERE:
tm
p
tm
1 holds number of elapsed hours *****/
* Calculate elapsed minutes since base date
tm
p
tm
2 =
tm
p
tm
1 * 60;
tm
p
tm
1 =
tm
p
tm
2 + (
tm
p
tm
3 = (__
time
64_t)tb->
tm
_min);
/***** HERE:
tm
p
tm
1 holds number of elapsed minutes *****/
* Calculate elapsed seconds since base date
tm
p
tm
2 =
tm
p
tm
1 * 60;
tm
p
tm
1 =
tm
p
tm
2 + (
tm
p
tm
3 = (__
time
64_t)tb->
tm
_sec);
/***** HERE:
tm
p
tm
1 holds number of elapsed seconds *****/
if ( ultflag ) {
* Adjust for
time
zone. No need to check for overflow since
* local
time
() will check its arg value
__tzset();
_ERRCHECK(_get_
dst
bias(&
dst
bias;));
_ERRCHECK(_get_
time
zone(&
time
zone;));
tm
p
tm
1 +=
time
zone;
* Convert this second count back into a
time
block
struct
ure.
* If local
time
returns NULL, return an error.
if ( _local
time
64_s(&tbtemp;, &
tm
p
tm
1;) != 0 )
goto err_
mk
time
;
* Now must compensate for
DST
. The ANSI rules are to use the
* passed-in
tm
_is
dst
flag if it is non-negative. Otherwise,
* compute if
DST
applies. Recall that tbtemp has the
time
without
*
DST
compensation, but has set
tm
_is
dst
correctly.
if ( (tb->
tm
_is
dst
> 0) || ((tb->
tm
_is
dst
< 0) &&
(tbtemp.
tm
_is
dst
> 0)) ) {
tm
p
tm
1 +=
dst
bias;
if ( _local
time
64_s(&tbtemp;, &
tm
p
tm
1;) != 0 )
goto err_
mk
time
;
else {
if ( _gm
time
64_s(&tbtemp;, &
tm
p
tm
1;) != 0)
goto err_
mk
time
;
/***** HERE:
tm
p
tm
1 holds number of elapsed seconds, adjusted *****/
/***** for local
time
if requested *****/
*tb = tbtemp;
return
tm
p
tm
1;
err_
mk
time
:
* All errors come to here
errno = EINVAL;
return (__
time
64_t)(-1);
int
tm
_sec; /* 秒–取值区间为[0,59] */
int
tm
_min; /* 分 - 取值区间为[0,59] */
int
tm
_hour; /* 时 - 取值区间为[0,23] */
int
tm
_mday; /* 一个月
中
的日期 - 取值区间为[1,31] */
int
tm
_mon; /
UNIX 时间概念
在 UNIX 系统
中
,将从 1970 年 1 月 1 日开始经过的秒数用一个整数存放,这种高效简洁的时间表示方法被称为 Unix 时间戳,向左和向右偏移都可以得到更早或者更后的时间。实际开发
中
,对日期和时间的操作非常多,基本无处不在。
时间的概念:
本地时间(locale
time
)
本地时间是在纪元时间(UTC)时间上加上时区。
格林威治时间(Greenwich Mean
Time
GMT)
世界时是最早的时间标准。在1884年,国际上将 1s 确定为全年内每日平均长度的 1/
在 ArduinoESP32核心支持库当
中
已经包含相关的获取时间的库,获取网络时间后,就可以不依赖网络,重复去获取时间,如果长时间运行,可以设置间隔时间同步NTP时间,只要访问本地时间的相关函数能正常调用,就没有问题。
调试了一天,掉坑里去了,在访问本地时间的时候,有些看是不重要的细节,往往很容易掉到坑里去。
最容易掉坑的地方!
在获取本地时间的时候,一定要先判断一
文章目录目的示例演示基础说明额外内容总结
时间是软硬件系统或设备
中
比较重要的东西,特别是需要和外部进行交互时就更加需要用到有个统一的时间了。目前来说只要能联网的设备的时间主要是从网络时间服务器(NTP )上获取的,这篇文章将对此做个简单的说明。
在Arduino core for the ESP32
中
获取网络时间是非常简单的,只要先连上网,然后就可以调用库
中
封装的方法获取网络时间了。下面是个简单的示例演示:
在这里插入代码片
https://github.com/