The clock code in most .NET apps is a static call your tests can never control.
That one fact causes more flaky time bugs than any library ever will.
DateTimeNow reads the local clock of whatever machine happens to run the code.
Deploy that same service to a server in another region and your timestamps quietly shift. Nothing throws. The data just starts lying.
Then daylight saving flips, and your "run at midnight" job fires twice or skips a day. Nobody wrote a bug. The clock moved and the code had no say in it.
Switching to UtcNow kills the region drift - one timeline for everyone.
DateTimeOffsetUtcNow goes a step further and stores the offset with the value, so you never have to guess which timezone a saved time came from.
But all three are still static calls. In a test you can't say "pretend it's 11:59 PM on the night the clock rolls back." The time is whatever the machine says right now.
That's the gap TimeProvider closes in .NET 8.
You inject it like any other dependency, then call GetUtcNow() instead of the static one.
In production it's the real system clock. In a test you hand it a FakeTimeProvider, set the exact moment you want, and even push time forward to check what happens after a delay.
The rule I follow now: no DateTimeNow near anything that makes a decision. If the logic depends on what time it is, the clock gets injected.
Make the clock a dependency, and time stops being something that happens to your code. It becomes something your tests get to decide.
Last year my son got a job offer from OpenAI in San Francisco
$450,000 total compensation
While my whole family was celebrating, I sat in my basement analyzing the offer
Something wasn't clicking for me
First of all, $450k is an impossible amount to make
Will my son be required to perform illegal activities to earn that salary?
But that's not the worst
I looked at pension contributions
"We offer a 401k match"
I had never heard of a 401k
I did some research and nearly vomited
It's a retirement account that depends on the stock market
His entire future would depend on gambling
I called the OpenAI recruiter on behalf of my son and declined the offer
Fast forward a year, my son now works at SAP making a crisp €47,000 a year
He has 32 days paid vacation and his pension is guaranteed by the state
My wife keeps saying I "threw his life away"
She could not be more wrong
I actually saved his life from being destroyed by America
@BarronSports@c_sandovalp comentó en sus programas lo prepotente que fue miguel herrera en conferencia, pero no lo vi comentar diciendo lo irrespetuoso que fue este periodista que le dijo que se fuera también en conferencia
En Costa Rica quieren “crucificar” a Miguel Herrera por perder ante Haití, selección que 9 de los 11 titulares juegan en Europa! La prensa tica trae la realidad bien alterada, imposible que el Piojo gane una carrera de caballos 🐎 montado en un burro 🫏 … #CostaRica#Ticos #seleccioncostarica🇨🇷 #PiojoHerrera #MiguelHerrera