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

179 lines
2.1 KiB
Markdown

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