Skip to content

Implement CBOR date tags according to RFC 8943 #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 29, 2025

This PR adds comprehensive support for CBOR date tags as specified in RFC 8943 "Tags for Date". JavaScript Date objects are now automatically encoded and decoded using CBOR date tags, providing seamless date handling in CBOR serialization.

Features Added

Automatic Date Encoding/Decoding:

  • JavaScript Date objects are automatically encoded using tag 100 (days since epoch) for maximum compactness
  • Both tag 100 (integer days) and tag 1004 (RFC 3339 strings) are supported for decoding
  • Dates are seamlessly converted back to JavaScript Date objects during decoding

RFC 8943 Compliance:

  • Tag 100: Number of days since the epoch date 1970-01-01 (integer format)
  • Tag 1004: RFC 3339 full-date string format (YYYY-MM-DD)
  • Only date components are preserved (time is ignored), consistent with the specification

Robust Error Handling:

  • Invalid dates gracefully fall back to JsonPackExtension objects
  • Malformed date strings or non-numeric values are handled without throwing errors

Usage Examples

Basic Usage:

import {encode, decode} from '@jsonjoy.com/json-pack/lib/cbor';

const data = {
  event: 'Birthday',
  date: new Date(1940, 9, 9), // October 9, 1940
  participants: ['John', 'Paul']
};

const encoded = encode(data);
const decoded = decode(encoded);

console.log(decoded.date instanceof Date); // true
console.log(decoded.date.toDateString()); // "Wed Oct 09 1940"

Manual Tag Usage:

import {JsonPackExtension} from '@jsonjoy.com/json-pack';

// Using tag 1004 for RFC 3339 string format
const dateExt = new JsonPackExtension(1004, '1940-10-09');
const encoded = encode(dateExt);
const decoded = decode(encoded); // Returns Date object

Utility Functions:

import {
  dateToDaysSinceEpoch,
  daysSinceEpochToDate,
  dateToRfc3339String,
  rfc3339StringToDate
} from '@jsonjoy.com/json-pack/lib/cbor';

const days = dateToDaysSinceEpoch(new Date(1940, 9, 9)); // -10676
const date = daysSinceEpochToDate(-10676); // October 9, 1940

Implementation Details

  • Zero Breaking Changes: All existing CBOR functionality remains unchanged
  • Minimal Overhead: Date encoding uses compact tag 100 by default
  • Complete Test Coverage: 43 tests covering edge cases, RFC examples, and round-trip scenarios
  • Standards Compliant: Follows RFC 8943 specification exactly, including the John Lennon birth/death date examples

The implementation handles complex scenarios including dates in nested objects, arrays of dates, leap years, historical dates (before 1970), and future dates, ensuring robust date serialization across all use cases.

Fixes #28.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@streamich streamich marked this pull request as ready for review July 29, 2025 15:09
@Copilot Copilot AI changed the title [WIP] Implement CBOR dates Implement CBOR date tags according to RFC 8943 Jul 29, 2025
@Copilot Copilot AI requested a review from streamich July 29, 2025 15:12
Copilot finished work on behalf of streamich July 29, 2025 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement CBOR dates
2 participants