dev-notes/Python/Standard Library/time_datetime.md

65 lines
4.2 KiB
Markdown
Raw Normal View History

2021-09-20 19:35:32 +02:00
# Time & Datetime
2021-01-31 11:05:37 +01:00
## Time
```py
# epoch: tempo in secondi trascorso (in UNIX parte da 01-010-1970)
import time # UNIX time
variabile = time.time() # restituisce il tempo (In secondi) trascorso da 01-01-1970
2021-09-20 19:35:32 +02:00
variabile = time.ctime(epochseconds) # trasforma l'epoca in data
2021-01-31 11:05:37 +01:00
var = time.perf_counter() # ritorna il tempo di esecuzione attuale
# tempo di esecuzione = tempo inizio - tempo fine
```
### time.srtfrime() format
| Format | Data |
|--------|------------------------------------------------------------------------------------------------------------|
2021-09-20 19:35:32 +02:00
| `%a` | Locale's abbreviated weekday name. |
| `%A` | Locale's full weekday name. |
| `%b` | Locale's abbreviated month name. |
| `%B` | Locale's full month name. |
| `%c` | Locale's appropriate date and time representation. |
2021-01-31 11:05:37 +01:00
| `%d` | Day of the month as a decimal number `[01,31]`. |
| `%H` | Hour (24-hour clock) as a decimal number `[00,23]`. |
| `%I` | Hour (12-hour clock) as a decimal number `[01,12]`. |
| `%j` | Day of the year as a decimal number `[001,366]`. |
| `%m` | Month as a decimal number `[01,12]`. |
| `%M` | Minute as a decimal number `[00,59]`. |
2021-09-20 19:35:32 +02:00
| `%p` | Locale's equivalent of either AM or PM. |
2021-01-31 11:05:37 +01:00
| `%S` | Second as a decimal number `[00,61]`. |
| `%U` | Week number of the year (Sunday as the first day of the week) as a decimal number `[00,53]`. |
| `%w` | Weekday as a decimal number `[0(Sunday),6]`. |
| `%W` | Week number of the year (Monday as the first day of the week) as a decimal number `[00,53]`. |
2021-09-20 19:35:32 +02:00
| `%x` | Locale's appropriate date representation. |
| `%X` | Locale's appropriate time representation. |
2021-01-31 11:05:37 +01:00
| `%y` | Year without century as a decimal number `[00,99]`. |
| `%Y` | Year with century as a decimal number. |
| `%z` | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM |
| `%Z` | Time zone name (no characters if no time zone exists). |
| `%%` | A literal `%` character. |
## Datetime
```py
import datetime
today = datetime.date.today() # restituisce data corrente
today = datetime.datetime.today() # restituisce la data e l'ora corrente
# esempio di formattazione
2021-09-20 19:35:32 +02:00
print('Current Date: {}-{}-{}' .format(today.day, today.month, today.year))
2021-01-31 11:05:37 +01:00
print('Current Time: {}:{}.{}' .format(today.hour, today.minute, today.second))
var_1 = datetime.date(anno, mese, giorno) # crea oggetto data
var_2 = datetime.time(ora, minuti, secondi, micro-secondi) # crea oggetto tempo
dt = datetime.combine(var_1, var_2) # combina gli oggetti data e tempo in un unico oggetto
2021-09-20 19:35:32 +02:00
date_1 = datetime.date('year', 'month', 'day')
2021-01-31 11:05:37 +01:00
date_2 = date_1.replace(year = 'new_year')
#DATETIME ARITHMETIC
date_1 - date_2 # -> datetime.timedelta(num_of_days)
datetime.timedelta # durata esprimente differenza tra due oggetti date, time o datetime
```