the code is valid for 30 seconds "steps", no matter when you press the button.
if you use yubico authenticator you will see that the code is grayed out almost immediatly or after more second depending on when you press it.
yubikey generate a code when button is pressed (if that mode is enabled, otherwise it keeps generating new codes as old one expires).
yubikey doesn't have a clock so the current date and is given to it by the computer.
since the clock might be inaccurate the server usually accept thre codes as valid:
the current, the old one and the following one.
so if the clock isn't perfectly in sync you will be able to login.
more info:
https://tools.ietf.org/html/rfc6238#section-4https://en.wikipedia.org/wiki/Time-base ... _Algorithm