2021-01-13 15:42:09 +08:00
|
|
|
## 日期时间扩展函数
|
2021-01-12 14:29:47 +08:00
|
|
|
|
|
|
|
### 1.date\_time\_create
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 创建日期对象。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
date_time_create() => object
|
|
|
|
```
|
|
|
|
|
|
|
|
日期对象有下列属性:
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
* year 年 (可读写)
|
|
|
|
* month 月 (可读写)
|
|
|
|
* day 日 (可读写)
|
|
|
|
* hour 时 (可读写)
|
|
|
|
* minute 分 (可读写)
|
|
|
|
* second 秒 (可读写)
|
|
|
|
* wday 周几 (可只读)
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
2022-08-23 10:12:36 +08:00
|
|
|
print(dt.year, "-", dt.month, "-", dt.day, " ", dt.hour, ":", dt.minute, ":", dt.second, "(", dt.wday, ")");
|
|
|
|
```
|
2021-01-13 15:42:09 +08:00
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 2.date\_time\_to\_time
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 转成相对于 00:00:00 UTC on 1 January 1970 的秒数。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
date_time_to_time(dt) => uint64_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
|
|
|
var time = date_time_to_time(dt);
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 3.date\_time\_from\_time
|
2022-08-23 10:12:36 +08:00
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 将相对于 00:00:00 UTC on 1 January 1970 的秒数转成成 date time。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
date_time_from_time(dt, time) => bool
|
|
|
|
```
|
2021-01-12 14:29:47 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
|
|
|
var time = date_time_to_time(dt);
|
2022-08-23 10:12:36 +08:00
|
|
|
assert(date_time_from_time(dt, time));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 4.date\_time\_set
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 把 date time 中的时间设置为系统时间。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
date_time_set(dt) => bool
|
|
|
|
```
|
|
|
|
|
|
|
|
> 需要有设置系统时间的权限。
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
2022-08-23 10:12:36 +08:00
|
|
|
dt.year = 2022;
|
|
|
|
dt.month = 10;
|
|
|
|
dt.day = 1;
|
|
|
|
dt.hour = 2;
|
|
|
|
dt.minute = 3;
|
|
|
|
dt.second = 4;
|
|
|
|
assert(date_time_set(dt));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 5.time_now/time_now_s
|
2021-01-12 14:29:47 +08:00
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 获取当前时间戳函数 (s)。
|
|
|
|
----------------------------
|
2021-01-12 14:29:47 +08:00
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-09-09 15:10:12 +08:00
|
|
|
time_now() => uint64_t
|
2022-08-23 10:12:36 +08:00
|
|
|
time_now_s() => uint64_t
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = time_now();
|
|
|
|
var b = time_now_s();
|
2021-01-13 15:42:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
### 6.time\_now\_ms
|
|
|
|
|
|
|
|
> 获取当前时间戳函数 (ms)。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
time_now_ms() => uint64_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = time_now_ms();
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
### 7.time_now_us
|
|
|
|
|
|
|
|
> 获取当前时间戳函数 (us)。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
time_now_us() => uint64_t
|
|
|
|
```
|
2021-01-12 14:29:47 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = time_now_us();
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 8.is\_leap\_year
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 判断指定年份是否是闰年。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
is_leap_year(year) => bool
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
2022-08-23 10:12:36 +08:00
|
|
|
print(is_leap_year(dt.year));
|
|
|
|
```
|
2021-01-13 15:42:09 +08:00
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 9.get\_days\_of\_month
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
> 获取指定年月的天数。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-01-13 15:42:09 +08:00
|
|
|
get_days_of_month(year, month) => uint32_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
2022-08-23 10:12:36 +08:00
|
|
|
print(get_days_of_month(dt.year, dt.month));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 10.date\_time\_set\_prop
|
|
|
|
|
|
|
|
> 设置日期对象中的属性。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
date_time_set_prop(dt, name, value) => bool
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
2022-08-23 10:12:36 +08:00
|
|
|
date_time_set_prop(dt, "year", 2012);
|
|
|
|
```
|
|
|
|
|
|
|
|
### 11.date\_time\_get\_prop
|
|
|
|
|
|
|
|
> 获取日期对象中的属性。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
date_time_get_prop(dt, name) => value
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create();
|
|
|
|
var year = date_time_get_prop(dt, "year");
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 更多示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var dt = date_time_create()
|
2021-01-12 14:29:47 +08:00
|
|
|
print(dt.year, "-", dt.month, "-", dt.day, " ", dt.hour, ":", dt.minute, ":", dt.second, "(", dt.wday, ")")
|
|
|
|
|
|
|
|
dt.year = 2022
|
|
|
|
dt.month = 10
|
|
|
|
dt.day = 1
|
|
|
|
dt.hour = 2
|
|
|
|
dt.minute = 3
|
|
|
|
dt.second = 4
|
|
|
|
assert(dt.year == 2022)
|
|
|
|
assert(dt.month == 10)
|
|
|
|
assert(dt.day == 1)
|
|
|
|
assert(dt.hour == 2)
|
|
|
|
assert(dt.minute == 3)
|
|
|
|
assert(dt.second == 4)
|
|
|
|
print(dt.year, "-", dt.month, "-", dt.day, " ", dt.hour, ":", dt.minute, ":", dt.second, "(", dt.wday, ")")
|
|
|
|
|
|
|
|
assert(date_time_to_time(dt) == 1664560984)
|
|
|
|
assert(date_time_from_time(dt, 1664560985))
|
|
|
|
|
|
|
|
assert(dt.year == 2022)
|
|
|
|
assert(dt.month == 10)
|
|
|
|
assert(dt.day == 1)
|
|
|
|
assert(dt.hour == 2)
|
|
|
|
assert(dt.minute == 3)
|
|
|
|
assert(dt.second == 5)
|
|
|
|
|
|
|
|
print(time_now_us())
|
|
|
|
print(time_now_ms())
|
|
|
|
print(time_now_s())
|
|
|
|
```
|