Skip to content

DateFormat.yMd().add_Hm() does not use DateSymbols.DATETIMEFORMATS #973

Open
@Knupper

Description

@Knupper

Describe the bug
The intl package's DateFormat class does not properly utilize the DATETIMEFORMATS patterns defined in DateSymbols when combining date and time formats. Instead of using the locale-specific patterns from DATETIMEFORMATS to determine how date and time should be combined, it appears to use a hardcoded approach through methods like addPattern(). This means that locale-specific formatting rules for combining date and time are not being respected, potentially leading to incorrect or inconsistent date-time formatting across different locales.

To Reproduce

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() async {
  // Initialize date formatting for different locales
  await initializeDateFormatting('en_ISO');

  final dateTime = DateTime(2024, 1, 15, 14, 30);

  // Using DateFormat's built-in methods
  final dateFormat = DateFormat.yMd();
  final timeFormat = DateFormat.Hm();

  final combinedFormat = DateFormat.yMd().addPattern(timeFormat.pattern);
  print('Using DateFormat.addPattern: ${combinedFormat.format(dateTime)}');

  // Expecting usage/formatting
  final dateStr = dateFormat.format(dateTime);
  final timeStr = timeFormat.format(dateTime);

  // English DATETIMEFORMATS: ['{1} \'at\' {0}', '{1} \'at\' {0}', '{1}, {0}', '{1}, {0}']
  final enPattern = dateTimeSymbolMap()['en_ISO']?.DATETIMEFORMATS.last;
  final enFormatted = enPattern.replaceAll('{1}', dateStr).replaceAll('{0}', timeStr);
  print('Expected format using DATETIMEFORMATS pattern (en): $enFormatted');
}

Expected behaviour
The DateFormat class should use the DATETIMEFORMATS patterns from the locale's DateSymbols when combining date and time formats. For example:
For English: "Jan 15, 2024, 14:30" (using {1}, {0})
For French: "15 janv. 2024, 14:30" (using {1}, {0})
For Spanish: "15 ene. 2024, 14:30" (using {1}, {0})

Actual behaviour
The DateFormat class ignores the DATETIMEFORMATS patterns and uses a hardcoded approach to combine date and time formats, which may not respect locale-specific formatting rules.
Additional context
The issue is particularly noticeable when:
Working with multiple locales
Using different levels of date-time formatting (full, long, medium, short)
Trying to maintain consistent formatting across an application
Implementing custom date-time formatting that needs to respect locale rules
System info
Please run dart pub deps --style compact in your project and provide the output.

Proposed solution
The DateFormat class should be modified to:
Read the DATETIMEFORMATS patterns from the locale's DateSymbols
Use these patterns when combining date and time formats
Allow customization of which pattern to use (full, long, medium, short)
Maintain backward compatibility with existing code
This would ensure that date-time formatting respects locale-specific rules and provides more flexibility in how date and time are combined.

System info
flutter: 3.32.1
intl: ^0.20.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions