chore(examples): adopt merged stdlib time APIs (unix_now / write_http_header / update_http_header)#98
Merged
Conversation
…_header / update_http_header) vlang/v#27641 (time.unix_now) and vlang/v#27639 (time.write_http_header, time.update_http_header) merged. Both were motivated by this repo's cached `Date:` header examples (vlang/v#27638), so those examples can now drop their hand-rolled implementations and call the stdlib directly: - async_date_timerfd: rebuild_at() is now a thin time.update_http_header wrapper (was hand-rolled put2/rebuild_at + wkday_names/month_names + fn C.time); rebuild() reads the clock via time.unix_now(). - efficient_date: C.time() -> time.unix_now() (drops #include <time.h> and the C decl); push_to_http_header retained (now allocation-free upstream). - date_header: double-buffer refresh() -> time.utc().write_http_header() into the inactive buffer; seed() lays the static "Date: "/CRLF frame into both buffers once. - auth: two time.utc().unix() -> time.unix_now() (JWT expiry check + minting). Tests: - async_date_timerfd/main_test.v: still drives the rebuild_at(now) seam (restored as the stdlib wrapper) through every rollover vs the vlib formatter oracle. - date_header/main_test.v: add the now-required seed() calls; replace the test that called a non-existent handle() (broken at HEAD -- this example uses an inline request_handler closure) with one that composes the response via the shared status_head/resp_tail consts and date_line(). No change on the wire; net -20 lines. The write_http_header / update_http_header integration (offset 6, dst_len 31) was verified byte-for-byte against a freshly formatted line across second/minute/hour/day/leap-day/backward-jump rollovers. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
3e0cb3f to
908782e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
vlang/v#27641 (
time.unix_now) and vlang/v#27639 (time.write_http_header/time.update_http_header) have merged. Both were motivated by this repo's cachedDate:header examples (vlang/v#27638), so the examples can now drop their hand-rolled implementations and call the stdlib directly.No change on the wire; net −20 lines (the timerfd example alone sheds ~35 lines of hand-rolled formatting).
What changed
examples/async_date_timerfd/main.vput2/rebuild_atinternals /wkday_names/month_names/fn C.time.rebuild_at(now)is now a thintime.update_http_header(&line[6], 31, last, now)wrapper;rebuild()reads the clock viatime.unix_now().examples/efficient_date/main.vC.time()→time.unix_now()(drops#include <time.h>+ the C decl).push_to_http_headerretained — it's allocation-free upstream now.examples/date_header/src/main.vrefresh()→time.utc().write_http_header(&buf[6], 31)into the inactive buffer; newseed()lays the static"Date: "/ CRLF frame into both buffers once.examples/auth/src/main.vtime.utc().unix()→time.unix_now()(JWT expiry check + minting — the exact TTL use case, ~1216 ns → ~2 ns).Tests
async_date_timerfd/main_test.v— keeps driving therebuild_at(now)seam (restored as the stdlib wrapper) through every rollover vs the vlib formatter oracle.date_header/src/main_test.v— added the now-requiredseed()calls; replacedtest_handler_includes_date_header, which called a non-existenthandle()(broken at HEAD — this example uses an inlinerequest_handlerclosure), withtest_response_includes_date_headerthat composes the response via the sharedstatus_head/resp_tailconsts anddate_line().Verification
v fmt -verify: clean on all 6 files.v test examples/auth/src→ 1 passed;v test examples/date_header/src→ 1 passed.async_date_timerfd/efficient_datetypecheck cleanly throughmain()on macOS; only the pre-existing Linux-only.epollbackend blocks a full Darwin build (needs Linux CI to run).write_http_header/update_http_headerintegration (offset 6,dst_len31) is byte-for-byte correct vs a freshly-formatted line across second / minute / hour / day / leap-day (2000, 2024) / epoch / year-end / backward-jump cases, plusdst_len < 29refusal and static-frame integrity.Note
Reviewer heads-up:
date_header's handler test was latently broken onmain— it referenced ahandle()the closure-based example never defined. This suggests thosesrc/-layout example tests aren't currently exercised in CI (V'ssrc/virtual-module deprecation makesv test examples/date_header/error unless you target.../src). Fixed here while in the neighborhood.🤖 Generated with Claude Code