Time
Time humanizing functions.
These are largely borrowed from Django's contrib.humanize
.
naturaldate(value)
Like naturalday
, but append a year for dates more than ~five months away.
Source code in .tox/docs/lib/python3.12/site-packages/humanize/time.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
naturalday(value, format='%b %d')
Return a natural day.
For date values that are tomorrow, today or yesterday compared to
present day return representing string. Otherwise, return a string
formatted according to format
.
Source code in .tox/docs/lib/python3.12/site-packages/humanize/time.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
naturaldelta(value, months=True, minimum_unit='seconds')
Return a natural representation of a timedelta or number of seconds.
This is similar to naturaltime
, but does not add tense to the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
(timedelta, int or float)
|
A timedelta or a number of seconds. |
required |
months |
bool
|
If |
True
|
minimum_unit |
str
|
The lowest unit that can be used. |
'seconds'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str or `value`
|
A natural representation of the amount of time
elapsed unless |
Raises:
Type | Description |
---|---|
OverflowError
|
If |
Examples:
Compare two timestamps in a custom local timezone::
import datetime as dt from dateutil.tz import gettz
berlin = gettz("Europe/Berlin") now = dt.datetime.now(tz=berlin) later = now + dt.timedelta(minutes=30)
assert naturaldelta(later - now) == "30 minutes"
Source code in .tox/docs/lib/python3.12/site-packages/humanize/time.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
naturaltime(value, future=False, months=True, minimum_unit='seconds', when=None)
Return a natural representation of a time in a resolution that makes sense.
This is more or less compatible with Django's naturaltime
filter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
(datetime, timedelta, int or float)
|
A |
required |
future |
bool
|
Ignored for |
False
|
months |
bool
|
If |
True
|
minimum_unit |
str
|
The lowest unit that can be used. |
'seconds'
|
when |
datetime
|
Point in time relative to which value is interpreted. Defaults to the current time in the local timezone. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A natural representation of the input in a resolution that makes sense. |
Source code in .tox/docs/lib/python3.12/site-packages/humanize/time.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
precisedelta(value, minimum_unit='seconds', suppress=(), format='%0.2f')
Return a precise representation of a timedelta.
>>> import datetime as dt
>>> from humanize.time import precisedelta
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
>>> precisedelta(delta)
'2 days, 1 hour and 33.12 seconds'
A custom format
can be specified to control how the fractional part
is represented:
>>> precisedelta(delta, format="%0.4f")
'2 days, 1 hour and 33.1230 seconds'
Instead, the minimum_unit
can be changed to have a better resolution;
the function will still readjust the unit to use the greatest of the
units that does not lose precision.
For example setting microseconds but still representing the date with milliseconds:
>>> precisedelta(delta, minimum_unit="microseconds")
'2 days, 1 hour, 33 seconds and 123 milliseconds'
If desired, some units can be suppressed: you will not see them represented and the time of the other units will be adjusted to keep representing the same timedelta:
>>> precisedelta(delta, suppress=['days'])
'49 hours and 33.12 seconds'
Note that microseconds precision is lost if the seconds and all the units below are suppressed:
>>> delta = dt.timedelta(seconds=90, microseconds=100)
>>> precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds'])
'1.50 minutes'
If the delta is too small to be represented with the minimum unit, a value of zero will be returned:
>>> delta = dt.timedelta(seconds=1)
>>> precisedelta(delta, minimum_unit="minutes")
'0.02 minutes'
>>> delta = dt.timedelta(seconds=0.1)
>>> precisedelta(delta, minimum_unit="minutes")
'0 minutes'
Source code in .tox/docs/lib/python3.12/site-packages/humanize/time.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
|