When-Exactly
An expressive and intuitive library for working with dates.
Rationale
People think and communicate about time in terms of years, months, weeks, days, hours, minutes, etc.
People do not think about time in terms of datetimes...
When-Exactly is a library that aims to bring more human-friendly date-time types into the hands of developers, so they can write more expressive code when working with dates.
Overview
>>> import when_exactly as wnx
>>> year = wnx.Year(2025) # the year 2025
>>> year
Year(2025)
>>> month = year.month(1) # month 1 (January) of the year
>>> month
Month(2025, 1)
>>> day = wnx.Day(2025, 12, 25) # December 25, 2025
>>> day
Day(2025, 12, 25)
>>> day.month # the month that the day is a part of
Month(2025, 12)
>>> day.week # the week that the day is a part of
Week(2025, 52)
>>> day.week.days[0:5] # all weekday (Mon thru Fri) of the week
Days([Day(2025, 12, 22), Day(2025, 12, 23), Day(2025, 12, 24), Day(2025, 12, 25), Day(2025, 12, 26)])
when_exactly package
A Python package for working with time intervals.
CustomInterval
dataclass
Bases: when_exactly.core.interval.Interval
A custom intervval.
This class serves as a base class from which custom intervals can be derived. It provides the necessary interface and methods to be implemented by subclasses.
Source code in src/when_exactly/core/custom_interval.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Day
dataclass
Bases: when_exactly.core.custom_interval.CustomInterval
Represents a single day, from midnight to midnight.
Source code in src/when_exactly/_api.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
InvalidMomentError
Bases: RuntimeError
Raised when a moment is invalid.
Source code in src/when_exactly/core/errors.py
1 2 3 4 5 6 7 8 |
|
Moment
dataclass
A Moment represents a specific point in time with year, month, day, hour, minute, and second.
The Moment
is analogous to the builtin datetime.datetime
class,
but it is immutable and provides additional functionality for date arithmetic.
Source code in src/when_exactly/core/moment.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 |
|
OrdinalDay
dataclass
Bases: when_exactly.core.custom_interval.CustomInterval
An ordinal day interval.
Source code in src/when_exactly/_api.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
Second
dataclass
Bases: when_exactly.core.custom_interval.CustomInterval
A second interval.
Source code in src/when_exactly/_api.py
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 |
|
Weekday
dataclass
Bases: when_exactly.core.custom_interval.CustomInterval
A weekday interval.
Source code in src/when_exactly/_api.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
Year
dataclass
Bases: when_exactly.core.custom_interval.CustomInterval
The Year
represents an entire year, starting from January 1 to December 31.
Creating a Year
>>> import when_exactly as wnx
>>> year = wnx.Year(2025)
>>> year
Year(2025)
>>> str(year)
'2025'
The Months of a Year
Get the Months
of a year.
>>> months = year.months
>>> len(months)
12
>>> months[0]
Month(2025, 1)
>>> months[-2:]
Months([Month(2025, 11), Month(2025, 12)])
The Weeks of a Year
Get the Weeks
of a year.
>>> weeks = year.weeks
>>> len(weeks)
52
>>> weeks[0]
Week(2025, 1)
Source code in src/when_exactly/_api.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 |
|
__init__(year)
Create a Year.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
year
|
int
|
The year to represent. |
required |
Examples:
>>> import when_exactly as wnx
>>> year = wnx.Year(2025)
>>> year
Year(2025)
>>> str(year)
'2025'
Source code in src/when_exactly/_api.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
from_moment(moment)
classmethod
Create a Year
from a Moment
.
Source code in src/when_exactly/_api.py
107 108 109 110 |
|
month(month)
Get a specific month of the year. Args: month (int): The month number (1-12).
Source code in src/when_exactly/_api.py
125 126 127 128 129 130 131 132 133 |
|
options: members: no