chronia

isSameSecond

Overview

The isSameSecond function checks whether two dates fall within the same second, ignoring milliseconds. It provides a precise way to compare dates at second-level granularity in Chronia’s comparison API.

Signature

function isSameSecond(dateLeft: DateInput, dateRight: DateInput): boolean;

Parameters

Parameter Type Description
dateLeft DateInput The first date as a Date object, numeric timestamp, or ISO 8601 string
dateRight DateInput The second date as a Date object, numeric timestamp, or ISO 8601 string

Return Value

Type Description
boolean Returns true if both dates are in the same second, false otherwise or if either date is invalid

Description

The isSameSecond function determines whether two dates represent the same second by comparing their year, month, day, hour, minute, and second components. The millisecond component is ignored in the comparison, making it useful for second-level precision comparisons.

Specification

Returns true when:

Returns false when:

Behavior Notes

Use Cases

Usage Examples

Time Synchronization

import { isSameSecond } from "chronia";

// Check if events happened in the same second
const event1Time = new Date(2024, 5, 15, 14, 30, 45, 123);
const event2Time = new Date(2024, 5, 15, 14, 30, 45, 789);

isSameSecond(event1Time, event2Time); // Returns: true

// Different seconds
const event3Time = new Date(2024, 5, 15, 14, 30, 46, 0);
isSameSecond(event1Time, event3Time); // Returns: false

// Works with timestamps
const timestamp1 = new Date(2024, 5, 15, 14, 30, 45, 0).getTime();
const timestamp2 = new Date(2024, 5, 15, 14, 30, 45, 999).getTime();
isSameSecond(timestamp1, timestamp2); // Returns: true

Deduplication

import { isSameSecond } from "chronia";

interface LogEntry {
  timestamp: Date;
  message: string;
}

// Remove duplicate log entries from the same second
function deduplicateLogs(logs: LogEntry[]): LogEntry[] {
  const seen = new Set<number>();

  return logs.filter((log) => {
    const secondKey = Math.floor(log.timestamp.getTime() / 1000);

    if (seen.has(secondKey)) {
      return false;
    }

    seen.add(secondKey);
    return true;
  });
}

// Alternative: Check if specific entries are duplicates
function areDuplicateEntries(log1: LogEntry, log2: LogEntry): boolean {
  return (
    isSameSecond(log1.timestamp, log2.timestamp) &&
    log1.message === log2.message
  );
}

const log1 = {
  timestamp: new Date(2024, 5, 15, 14, 30, 45, 100),
  message: "Error",
};
const log2 = {
  timestamp: new Date(2024, 5, 15, 14, 30, 45, 900),
  message: "Error",
};
areDuplicateEntries(log1, log2); // Returns: true

Interval Detection

import { isSameSecond } from "chronia";

// Rate limiting: check if requests are too close
class RateLimiter {
  private lastRequest: Date | null = null;

  canMakeRequest(currentTime: Date): boolean {
    if (!this.lastRequest) {
      this.lastRequest = currentTime;
      return true;
    }

    // Allow only one request per second
    if (isSameSecond(this.lastRequest, currentTime)) {
      return false;
    }

    this.lastRequest = currentTime;
    return true;
  }
}

const limiter = new RateLimiter();
const time1 = new Date(2024, 5, 15, 14, 30, 45, 100);
const time2 = new Date(2024, 5, 15, 14, 30, 45, 500);
const time3 = new Date(2024, 5, 15, 14, 30, 46, 0);

limiter.canMakeRequest(time1); // Returns: true
limiter.canMakeRequest(time2); // Returns: false (same second)
limiter.canMakeRequest(time3); // Returns: true (different second)

Animation and Timing

import { isSameSecond } from "chronia";

// Synchronize multiple timers that should trigger together
class TimerCoordinator {
  private scheduledTime: Date;

  constructor(scheduledTime: Date) {
    this.scheduledTime = scheduledTime;
  }

  shouldTrigger(currentTime: Date): boolean {
    return isSameSecond(this.scheduledTime, currentTime);
  }
}

// Multiple animations triggered in the same second
const coordinator1 = new TimerCoordinator(new Date(2024, 5, 15, 14, 30, 45, 0));
const coordinator2 = new TimerCoordinator(
  new Date(2024, 5, 15, 14, 30, 45, 500),
);

const checkTime = new Date(2024, 5, 15, 14, 30, 45, 250);
coordinator1.shouldTrigger(checkTime); // Returns: true
coordinator2.shouldTrigger(checkTime); // Returns: true

// Different second - won't trigger
const laterTime = new Date(2024, 5, 15, 14, 30, 46, 0);
coordinator1.shouldTrigger(laterTime); // Returns: false