154 lines
1.8 KiB
Markdown
154 lines
1.8 KiB
Markdown
+++
|
|
title = "Fechas y tiempo"
|
|
weight = 2
|
|
+++
|
|
|
|
|
|
{{% notice tip %}}
|
|
La fecha inicial en Calc y en Python es diferente.
|
|
{{% /notice %}}
|
|
|
|
|
|
### today
|
|
|
|
Obtener la fecha de hoy.
|
|
|
|
```python
|
|
d = app.dates
|
|
app.msgbox(d.today)
|
|
```
|
|
|
|
|
|
### now
|
|
|
|
Obtener la fecha y hora actuales.
|
|
|
|
```python
|
|
d = app.dates
|
|
app.msgbox(d.now)
|
|
```
|
|
|
|
|
|
### time
|
|
|
|
Obtener la hora actual.
|
|
|
|
```python
|
|
d = app.dates
|
|
app.msgbox(d.now.time())
|
|
```
|
|
|
|
|
|
### epoch
|
|
|
|
Obtener el [tiempo Unix][1]
|
|
|
|
```python
|
|
d = app.dates
|
|
app.msgbox(d.epoch)
|
|
```
|
|
|
|
|
|
### date
|
|
|
|
Devolver una fecha
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
date = d.date(1974, 1, 15)
|
|
app.msgbox(date)
|
|
```
|
|
|
|
|
|
### time
|
|
|
|
Devolver un tiempo
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
time = d.time(10, 20, 15)
|
|
app.msgbox(time)
|
|
```
|
|
|
|
|
|
### datetime
|
|
|
|
Devolver fecha y hora
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
dt = d.datetime(1974, 1, 15, 10, 11, 12)
|
|
app.msgbox(dt)
|
|
```
|
|
|
|
### str_to_date
|
|
|
|
Convertir una cadena en fecha. Mira este [excelente recurso][2]
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
cadena = '1974-01-15'
|
|
plantilla = '%Y-%m-%d'
|
|
fecha = d.str_to_date(cadena, plantilla)
|
|
app.msgbox(fecha)
|
|
app.msgbox(type(fecha))
|
|
```
|
|
|
|
Para obtener un valor válido para establecer en una celda de Calc.
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
cadena = '1974-01-15'
|
|
plantilla = '%Y-%m-%d'
|
|
fecha = d.str_to_date(cadena, plantilla, True)
|
|
app.msgbox(fecha)
|
|
app.msgbox(type(fecha))
|
|
```
|
|
|
|
### calc_to_date
|
|
|
|
Convierte el valor de una celda en una fecha Python, por ejemplo, la fecha inicial configurada en Calc.
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
value_from_cell = 1
|
|
fecha = d.calc_to_date(value_from_cell)
|
|
app.msgbox(fecha)
|
|
app.msgbox(type(fecha))
|
|
```
|
|
|
|
|
|
### start y end
|
|
|
|
Medir tiempo en segundos.
|
|
|
|
```python
|
|
d = app.dates
|
|
|
|
d.start()
|
|
app.sleep(5)
|
|
seconds = d.end()
|
|
app.msgbox(seconds)
|
|
```
|
|
|
|
Regresar timedelta en vez de segundos.
|
|
|
|
```python
|
|
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
|