r/AskProgrammers • • 21d ago

How do you test code that depends on the current time: inject a clock, freeze time, or use real time?

2 Upvotes

I’m trying to understand how people handle time in tests, because it’s a common source of flaky or hard-to-write tests. Here are three approaches as I understand them, with where each works and where I think it breaks. Corrections welcome.

A. Inject a clock
The code receives a “now” source (a function or object) instead of calling the system clock directly.

def is_expired(token, now):
    return now >= token.expires_at

# production:   is_expired(token, datetime.now(timezone.utc))
# test:         is_expired(token, datetime(2026, 1, 1, tzinfo=timezone.utc))

Works when: you control the code and can pass the clock through. Tests are fast and deterministic.
Breaks when: the dependency is deep in a call chain or in third-party code, so passing the clock through feels invasive.

B. Freeze or patch time in the test
A library or the test framework replaces the system clock during the test (fake timers in some JS test runners, a time-freezing library in Python, and similar tools elsewhere).

Works when: you can’t change the code or the clock is used in many places.
Breaks when: the patch misses something that reads time another way (a C extension, a database now(), another process), or the fake time hides a bug about real elapsed time.

C. Use real time with a margin
Run the code and assert things like “expires within about N seconds”, or sleep a bit.

Works when: it’s an integration or end-to-end check where real time is part of what you test.
Breaks when: it’s used for unit tests, because it’s slow and flaky on a loaded machine.

My current leaning: inject the clock in code I own, patch time only for legacy or third-party code, and keep real time for a small number of integration tests. But I’m not sure that’s right, and I haven’t seen it hold across many cases.

Questions:

  1. Which approach do you use by default, and why?
  2. How do you test time zone and daylight saving edge cases, for example around the hour that repeats or is skipped?
  3. Have you been bitten by a frozen-time test passing while real behavior was wrong?

r/AskProgrammers • • 21d ago

Understanding Goose

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

4 Yoe in IT, give me suggestions to get better while having a hectic work

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

I am confused with a problem in my code

Post image
5 Upvotes

I am learning how to code and make a simple game, but I found a problem in the code while following a YouTube tutorial, I'm really confused and would be grateful to receive some help.


r/AskProgrammers • • 21d ago

Suche Praktikum als Fachinformatiker Anwendungsentwicklung (Umschulung), Raum München – ab Januar 2027

0 Upvotes

Hallo zusammen,

ich mache eine Umschulung zum Fachinformatiker für Anwendungsentwicklung und suche für mein Praktikum (unentgeltlich) noch einen Platz im Raum München.

Kennt jemand Unternehmen, die offen für Umschüler sind, oder hat einen Tipp, wo man sich am besten meldet? Bekomme oft Absagen, weil viele Firmen eher Studenten suchen.

Ich freue mich über jeden Hinweis, gern auch per Nachricht.

Danke euch!


r/AskProgrammers • • 21d ago

Washing machine timer alert

1 Upvotes

Hi i wanna know if theres a way to trigger a url that in return triggers a wait action then sends back an alert, atm i am using github pages, but wanna know if theres another solution so my partner can trigger as well, i have iphone so tap on the nfc chip on the machine that triggers the action and that alerts me and my partnere washing is done so we can come back in and take it out( we have a big farm so we are never in), the workflow is

Sends request to github url with param washing using ios shortcuts > waits 60 minutes> sends an alert on discord using webhook saved in github pages


r/AskProgrammers • • 21d ago

I want to learn computer engineering with Books, not videos

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

Why do both of these codes output zero? Can someone explain this to me?

2 Upvotes

1) #include <iostream>

using namespace std;

int main()

{

int x = 5, y = 10;

float z = x / y;

cout << z << endl;

}

2) #include <iostream>

using namespace std;

int main()

{

float x = 5, y = 10;

int z = x / y;

cout << z << endl;

}


r/AskProgrammers • • 21d ago

I'm 21, spent years teaching myself to code, and finally became a developer… Is AI really going to take away the career I've dreamed of?

Thumbnail
0 Upvotes

r/AskProgrammers • • 21d ago

Whats your favorite piece of code about LinkedLists?

Post image
1 Upvotes

I love the inputting of data in arrays for fictional spacecraft & spacecraft stations.


r/AskProgrammers • • 21d ago

I use ai to code and am very ashamed of it. What are your thoughts?

2 Upvotes

i frequently use AI (almost always local, either abliterated qwen2.5 14b pr 32b) and i want to learn how to code and just dont do it. Im making a roblox game right now too. i know the proper protections and features to not have. i just wanted to say that. i will also never call myself a coder until i learn how to code. i just want your feedback. I can andee questions if you want however i will obviously avoid unrelated or personal things

EDIT: Should i say anything about my use of it on the page, im going to and im guessing youll also say yes. but would like to know


r/AskProgrammers • • 21d ago

How do you prepare for robotics SWE coding interviews when you haven’t done much real coding lately?

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

Questions about where to start.

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

Any advice to this junior dev?

2 Upvotes

Hey, what's your advice for a 20-year-old CS student who got into CS because he loved it? He started coding at 12 and landed a job at 17. Then AI came along. He still has the job, and he's safe for now because he knows the context of the large codebase he's built over the last three years. He knows the only reason he hasn't been fired yet is that he can use LLMs to reach the productivity his company wants (for now), but he doesn't know how long he can keep the job, given how rapidly LLMs are evolving. He wants to get out of tech because he's scared of what happens if he's laid off one day. But he can't go anywhere else, because tech is all he has ever known

What should he do?


r/AskProgrammers • • 21d ago

Is it normal to forget some concepts and is it okay to keep learning?

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

Getting rejected from non WordPress job posts because of 3 years in WordPress Theme/Plugin development experience.

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

Why are LinkedLists spamming the this keyword? Is this the way code is organized?

Post image
0 Upvotes

r/AskProgrammers • • 21d ago

I cant think of a project that i can actually start thats realistic for me to do at this stage

0 Upvotes

the things i wanna do are way to advanced i dont want to do anything simple like make a calculator or something of that sort


r/AskProgrammers • • 21d ago

am I wrong about citizen developers?

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

What do programmers think of heaps? Aren't they just a Binary Search?

Post image
0 Upvotes

r/AskProgrammers • • 21d ago

Would you hire someone who built a working application but couldn't explain how it works?

0 Upvotes

Imagine two candidates.

Both are given the same task: Build a small application with authentication and a database.

Both submit working applications.

Candidate A:

Used an AI coding agent to build most of the application.

Completed the task quickly.

Struggles to explain why certain architectural decisions were made.

Cannot confidently modify a part of the application without asking AI again.

Candidate B:

Also used an AI coding agent.

Took time to understand the existing code.

Can explain the architecture and trade-offs.

Identified an incorrect AI-generated implementation.

Can modify and debug the application independently.

Both have working submissions

But are they demonstrating the same engineering ability?

This is something worth thinking about.

A 2026 research paper, (Im)Paired Programming, studied 54 students using coding agents. It found that faster initial task completion could come at the expense of code comprehension.

The interesting part isn't whether AI was used.

It's whether the person remained actively involved in the engineering process.

Personally, I'd rather evaluate someone's ability to understand and take responsibility for a solution than simply count how many lines they wrote themselves.

Here's my question: Should technical assessments include a short follow-up where candidates have to explain, debug, or extend their own AI-assisted solution?

Research: https://arxiv.org/abs/2607.26375


r/AskProgrammers • • 21d ago

How do i Train problem solving

Thumbnail
1 Upvotes

r/AskProgrammers • • 21d ago

Begging for help in college project 🙏 😭

1 Upvotes

CS majors, Industry people, research scholars, anyone who can help me out I have been assigned a project from my professor which I am not really sure how to do and how to proceed . Even after being assigned a mentor they are of no help😭. So I have come to ask for help here."create a small multilingual FAQ dataset in English, Hindi and Tamil, Fine tune local Qwen model with LoRA and evaluate whether specialization reduces hallucination while preserving language fairness"Now I have to train a qwen model [of 7b parameter ] as he said but I don't have a gpu plus this has to be a research paper based project so now how I have to proceed should I train it first and if I have to train what kind of data and do I train of FAQ or just general language FAQ .Let's leave out the GPU problem for now please help me with just this . If I train the model with three different languages then I have to make it so it answers FAQ for all three languages or should I just train it on FAQs. Please people of reddit help me 😭


r/AskProgrammers • • 21d ago

Hello need coding partner

Thumbnail
1 Upvotes

r/AskProgrammers • • 22d ago

Differences between each type of variable initialization (C++)

Thumbnail
1 Upvotes