Skip to content

[repo-assist] feat: add ToUnixMilliseconds, ToUnixSeconds, FromUnixMilliseconds, FromUnixSeconds#22

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-unix-epoch-helpers-51771b7-c3b05ed77da1a5f8
Draft

[repo-assist] feat: add ToUnixMilliseconds, ToUnixSeconds, FromUnixMilliseconds, FromUnixSeconds#22
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-unix-epoch-helpers-51771b7-c3b05ed77da1a5f8

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This pull request was created by Repo Assist, an automated AI assistant.

Summary

Add four Unix epoch convenience methods to DateTimeNano, complementing the existing ToUnixNanoseconds() / NanosecondsSinceEpoch:

Method Direction Return type
ToUnixMilliseconds() to unix ms long
ToUnixSeconds() to unix s long
FromUnixMilliseconds(long) from unix ms DateTimeNano
FromUnixSeconds(long) from unix s DateTimeNano

Why

Many external systems (REST APIs, databases, financial feeds) exchange timestamps in seconds or milliseconds rather than nanoseconds:

Before:

var ms  = (long)(timestamp.NanosecondsSinceEpoch / 1_000_000UL);  // to ms
var dt  = new DateTimeNano((ulong)ms * 1_000_000UL);               // from ms

After:

var ms  = timestamp.ToUnixMilliseconds();
var dt  = DateTimeNano.FromUnixMilliseconds(ms);

This makes the API symmetric: ToUnixNanoseconds() now has siblings at millisecond and second granularity, matching the conventions of DateTimeOffset.ToUnixTimeMilliseconds() / FromUnixTimeMilliseconds() in the BCL.

Implementation notes

  • Truncates sub-unit precision (consistent with DateTime.ToUnixTimeSeconds() and ToUnixTimeMilliseconds()).
  • FromUnixMilliseconds / FromUnixSeconds throw ArgumentOutOfRangeException for negative inputs (pre-epoch timestamps are not representable by DateTimeNano).
  • No new dependencies. No breaking changes.

Test Status

46 tests pass on net8.0, net9.0, and net10.0 (40 original + 6 new tests covering round-trips, truncation, and negative-input guards). Build succeeded with 0 errors.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

…omUnixSeconds

Add four Unix epoch convenience methods to complement the existing
ToUnixNanoseconds():

  - ToUnixMilliseconds() — whole milliseconds since Unix epoch (sub-ms truncated)
  - ToUnixSeconds()      — whole seconds since Unix epoch (sub-s truncated)
  - FromUnixMilliseconds(long) — factory from milliseconds since epoch
  - FromUnixSeconds(long)      — factory from seconds since epoch

These are especially useful when interoperating with REST APIs, databases,
and other systems that exchange timestamps in seconds or milliseconds.
Six new tests cover round-trips, truncation behaviour, and negative-input guards.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants