easymacro/docs/en/docs/tools/datetime.md

179 lines
2.1 KiB
Markdown
Raw Normal View History

2023-12-17 23:57:30 -06:00
2023-12-29 16:36:40 -06:00
!!! tip "Attention"
2023-12-17 23:57:30 -06:00
2023-12-29 16:36:40 -06:00
Start date in Calc and Python are different.
2023-12-17 23:57:30 -06:00
<br>
### **today**
2023-12-29 16:36:40 -06:00
Get current date.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
app.msgbox(d.today)
```
<br>
### **now**
2023-12-29 16:36:40 -06:00
Get current date and time.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
app.msgbox(d.now)
```
<br>
### **time**
2023-12-29 16:36:40 -06:00
Get current time.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
app.msgbox(d.now.time())
```
<br>
### **epoch**
2023-12-29 16:36:40 -06:00
Get [Unix time][1]
2023-12-17 23:57:30 -06:00
```py
d = app.dates
app.msgbox(d.epoch)
```
<br>
### **date**
2023-12-29 16:36:40 -06:00
Get a date.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
date = d.date(1974, 1, 15)
app.msgbox(date)
```
<br>
### **time**
2023-12-29 16:36:40 -06:00
Get a time.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
time = d.time(10, 20, 15)
app.msgbox(time)
```
<br>
### **datetime**
2023-12-29 16:36:40 -06:00
Get date and time.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
dt = d.datetime(1974, 1, 15, 10, 11, 12)
app.msgbox(dt)
```
<br>
### **str_to_date**
2023-12-29 16:36:40 -06:00
String to date. Look this [excellent info][2]
2023-12-17 23:57:30 -06:00
```py
d = app.dates
2023-12-29 16:36:40 -06:00
str_date = '1974-01-15'
template = '%Y-%m-%d'
obj_date = d.str_to_date(str_date, template)
app.msgbox(obj_date)
app.msgbox(type(obj_date))
2023-12-17 23:57:30 -06:00
```
2023-12-29 16:36:40 -06:00
Get a valid date for insert in a Calc cell.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
2023-12-29 16:36:40 -06:00
str_date = '1974-01-15'
template = '%Y-%m-%d'
obj_date = d.str_to_date(str_date, template, True)
app.msgbox(obj_date)
app.msgbox(type(obj_date))
2023-12-17 23:57:30 -06:00
```
<br>
### **calc_to_date**
2023-12-29 16:36:40 -06:00
Converts a value to Python date, for example, the initial date set in Calc.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
2023-12-29 16:36:40 -06:00
value = 0
obj_date = d.calc_to_date(value)
app.msgbox(obj_date)
app.msgbox(type(obj_date))
2023-12-17 23:57:30 -06:00
```
<br>
### **sleep**
2023-12-29 16:36:40 -06:00
Pause execution for X seconds.
2023-12-17 23:57:30 -06:00
2023-12-29 16:36:40 -06:00
!!! tip inline end "Attention"
2023-12-17 23:57:30 -06:00
2023-12-29 16:36:40 -06:00
The pause is blocking.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
app.sleep(3)
2023-12-29 16:36:40 -06:00
app.msgbox('End')
2023-12-17 23:57:30 -06:00
```
<br>
2023-12-29 16:36:40 -06:00
### **start** and **end**
2023-12-17 23:57:30 -06:00
2023-12-29 16:36:40 -06:00
Measure time in seconds.
2023-12-17 23:57:30 -06:00
```py
d = app.dates
d.start()
app.sleep(5)
seconds = d.end()
app.msgbox(seconds)
```
2023-12-29 16:36:40 -06:00
Get timedelta instead of seconds
2023-12-17 23:57:30 -06:00
```py
d = app.dates
d.start()
app.sleep(5)
td = d.end(False)
app.msgbox(td)
```
2023-12-29 16:36:40 -06:00
[1]: https://en.wikipedia.org/wiki/Unix_time
2023-12-17 23:57:30 -06:00
[2]: https://strftime.org