Skip to content

ParseJSON global data format functionΒ #228

Closed

Description

@Fujio-Turner

πŸ” Analysis: Time Format Handling in the Codebase

Here's what I found:

1. Input Parsing: Multiple Formats β†’ Milliseconds

Location: parseTime() in data-layer.js (and duplicate in main-legacy.js)

Handles:

  • "1.5s" β†’ 1500ms
  • "500ms" β†’ 500ms
  • "100Β΅s" β†’ 0.1ms
  • "250ns" β†’ 0.000250ms

Status: βœ… STANDARDIZED - Single function in data-layer.js


2. Output Formatting: Milliseconds β†’ Display Format

Location: Formatters.formatTime() in main-legacy-cleaned.js (line 563)

Converts:

  • Milliseconds β†’ "MM:SS.mmm" format
  • 1500ms β†’ "00:01.500"
  • 65000ms β†’ "01:05.000"
  • 0ms β†’ "00:00.000"

Status: ❌ NOT IN MODULES - Still in main-legacy-cleaned.js


3. Tooltip Formatting

Location: Formatters.formatTimeTooltip() in main-legacy-cleaned.js (line 590)

Purpose: Shows original time string for precision

  • Small values: Shows "Original: 146.266Β΅s"
  • Shows original if different from formatted

Status: ❌ NOT IN MODULES


4. Chart Display Formats

Location: ChartTimeUtils / Chart.js config in main-legacy-cleaned.js

Formats:

  • "HH:mm:ss.SSS" for milliseconds
  • "HH:mm:ss" for seconds
  • "MMM dd HH:mm" for minutes/hours
  • "MMM dd" for days

Status: βœ… IN CHARTS.JS (ChartTimeUtils)


5. Table/UI Display

Location: Scattered throughout tables.js, parsers.js, main-legacy-cleaned.js

Uses:

  • .toISOString().replace('T', ' ').substring(0, 23) + 'Z' for timestamps
  • .toFixed(2) for durations in seconds
  • formatTime() for consistent MM:SS.mmm display

Status: ⚠️ INCONSISTENT - Multiple patterns


πŸ“Š Summary

βœ… Standardized (Good):

  1. parseTime() - Single function in data-layer.js converts all formats to ms
  2. parseCouchbaseDateTime() - Single function for CB datetime parsing

❌ Not Standardized (Needs Work):

  1. Formatters.formatTime() - Still in main-legacy, should be in ui-helpers.js
  2. Formatters.formatTimeTooltip() - Still in main-legacy
  3. Timestamp display - Multiple patterns (.toISOString(), .substring(0, 23), etc.)

πŸ’‘ Recommendation:

Move to ui-helpers.js:

export function formatTime(milliseconds) { ... }  // MM:SS.mmm
export function formatTimeTooltip(timeStr, ms) { ... }
export function formatTimestamp(date) { ... }  // Standardize timestamp display

This would centralize ALL time formatting in one place (ui-helpers.js for display, data-layer.js for parsing).

Activity

  1. Fujio-Turner commented on Nov 7, 2025

    @Fujio-Turner
    OwnerAuthor

    Overview

    Standardized all timestamp formatting functions across /liquid_snake/assets/js/*.js into a single centralized function in base.js.

    Changes Made

    1. New Centralized Function: formatTimestamp()

    Location: liquid_snake/assets/js/base.js

    Signature:

    formatTimestamp(dateInput, format = "YYYY-MM-DD HH:MM:SS.sssZ")

    Supported Formats:

    • "YYYY-MM-DD HH:MM:SS.sssZ" β†’ "2024-11-07 12:30:45.123Z" (default)
    • "YYYY-MM-DD HH:MM:SS" β†’ "2024-11-07 12:30:45"
    • "HH:MM:SS.sss" β†’ "12:30:45.123"
    • "HH:MM:SS" β†’ "12:30:45"
    • "locale" β†’ Browser's toLocaleString()
    • "iso" β†’ ISO 8601 format with T separator

    Features:

    • Accepts both Date objects and string inputs
    • Handles Couchbase datetime format (space-separated) automatically
    • Returns original input on parse failure (graceful fallback)
    • Logs warnings for invalid dates
    • Validates date before formatting

    2. Files Updated

    base.js

    • βœ… Added formatTimestamp() function (lines 389-458)
    • βœ… Exported function for ES6 modules
    • βœ… Exposed globally via window.formatTimestamp for legacy compatibility

    main-legacy.js

    • βœ… Replaced: convertedDate.toISOString().replace('T', ' ').substring(0, 23) + 'Z'
    • βœ… With: formatTimestamp(convertedDate, "YYYY-MM-DD HH:MM:SS.sssZ")
    • βœ… Location: Line 3051 (requestTime column getValue function)

    report.js

    • βœ… Replaced: new Date().toLocaleString() (2 occurrences)
    • βœ… With: formatTimestamp(new Date(), "locale")
    • βœ… Locations: Lines 94, 110
    • βœ… Added import: formatTimestamp from base.js

    3. Duplicate Patterns Eliminated

    Before:

    // Pattern 1 (main-legacy.js)
    date.toISOString().replace('T', ' ').substring(0, 23) + 'Z'
    
    // Pattern 2 (report.js)
    new Date().toLocaleString()
    
    // Pattern 3 (charts.js, data-layer.js - still used for parsing)
    dateTimeStr.replace(" ", "T")

    After:

    // Unified approach
    formatTimestamp(date, "YYYY-MM-DD HH:MM:SS.sssZ")
    formatTimestamp(date, "locale")

    4. Preserved Functions

    These functions remain unchanged as they serve different purposes:

    • charts.js:

      • getChartDate() - Handles timezone conversion (different purpose)
      • convertToTimezone() - Timezone-specific date manipulation
    • data-layer.js:

      • parseCouchbaseDateTime() - Parses Couchbase format β†’ Date object (parsing, not formatting)

    Usage Examples

    // ES6 module import
    import { formatTimestamp } from './base.js';
    
    // Basic usage
    formatTimestamp("2024-11-07 12:30:45.123Z");  // "2024-11-07 12:30:45.123Z"
    
    // Custom format
    formatTimestamp(new Date(), "HH:MM:SS");      // "12:30:45"
    
    // Locale format
    formatTimestamp(new Date(), "locale");        // "11/7/2024, 12:30:45 PM"
    
    // Date object
    const d = new Date();
    formatTimestamp(d, "YYYY-MM-DD HH:MM:SS");    // "2024-11-07 12:30:45"
    
    // Legacy (non-module) usage
    window.formatTimestamp(date, "iso");          // "2024-11-07T12:30:45.123Z"

    Future Enhancements

    To further standardize in the future, consider:

    1. Adding more custom format patterns as needed
    2. Consolidating parseCouchbaseDateTime() if parsing logic becomes duplicated
    3. Adding timezone-aware formatting (integration with getChartDate())

    Real Data Format Analysis

    Analyzed actual Couchbase data from sample/ directory:

    Timestamp Formats Found:

    1. requestTime - ISO 8601 with timezone offset:

      • "2025-08-26T14:06:02.129-05:00" βœ… Handled
      • "2025-08-15T00:01:00.000Z" βœ… Handled
    2. #planPreparedTime (in plan JSON) - ISO 8601 UTC:

      • "2025-07-13T10:15:22.124Z" βœ… Handled
    3. phaseTimes - Duration strings (NOT timestamps):

      • "15.110ms", "8.901Β΅s", "32.222ms" ⚠️ NOT converted (these are durations, not dates)
      • These should use separate duration parsing (already exists in codebase)

    Compatibility Verification:

    // Test with real data samples
    formatTimestamp("2025-08-26T14:06:02.129-05:00", "YYYY-MM-DD HH:MM:SS.sssZ");
    // βœ… Output: "2025-08-26 19:06:02.129Z" (converted to UTC)
    
    formatTimestamp("2025-08-15T00:01:00.000Z", "locale");
    // βœ… Output: "8/15/2025, 12:01:00 AM" (browser locale)
    
    formatTimestamp("2025-07-13T10:15:22.124Z", "HH:MM:SS.sss");
    // βœ… Output: "10:15:22.124"

    Edge Cases Handled:

    • βœ… Timezone offsets (-05:00, +00:00)
    • βœ… UTC timestamps with Z
    • βœ… Millisecond precision
    • βœ… Date objects
    • βœ… Invalid dates return original input (graceful fallback)
    • ⚠️ Duration strings (ms, Β΅s) are NOT timestamps - handled by separate parseTime() function

    Testing

    All existing functionality preserved:

    • βœ… No diagnostics errors
    • βœ… Backward compatibility maintained
    • βœ… Global exposure for legacy code
    • βœ… ES6 module exports for modern code
    • βœ… Real Couchbase timestamp formats tested
    • βœ… Interactive test page: test_timestamp_formats.html

    Related Files

    • liquid_snake/assets/js/base.js - Core utility
    • liquid_snake/assets/js/main-legacy.js - Updated usage
    • liquid_snake/assets/js/report.js - Updated usage
    • liquid_snake/assets/js/charts.js - Parsing functions preserved
    • liquid_snake/assets/js/data-layer.js - Parsing functions preserved
  2. Fujio-Turner commented on Sep 15, 2026

    @Fujio-Turner
    OwnerAuthor

    Done on release-4.0.0-beta via #264.

    • formatTime / formatTimeTooltip moved out of main-legacy.js into app/assets/js/time-format.js (re-exported from ui-helpers.js)
    • Remaining table/insight/chart timestamp display uses formatTimestamp() from base.js
    • Removed the hoisted local formatTimestamp duplicate in the AI timeline
  3. added a commit that references this issue on Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions