ZonedDateTime.parse constructor

ZonedDateTime.parse(
  1. String dateTimeString
)

Parses a ZonedDateTime from its string representation.

Supports multiple formats:

  • ISO 8601 with timezone: 2023-12-25T15:30:00+01:00
  • With zone ID: 2023-12-25T15:30:00+01:00[Europe/Paris]
  • UTC format: 2023-12-25T15:30:00Z
  • Offset only: 2023-12-25T15:30:00-05:00

Example:

// Various parsing formats
final iso = ZonedDateTime.parse('2023-12-25T15:30:00+01:00');
final withZone = ZonedDateTime.parse('2023-12-25T15:30:00[Europe/Paris]');
final utc = ZonedDateTime.parse('2023-12-25T15:30:00Z');
final offset = ZonedDateTime.parse('2023-12-25T15:30:00-05:00');

print('ISO: $iso');
print('With zone: $withZone');
print('UTC: $utc');
print('Offset: $offset');

Represents a date-time with timezone information.

This class combines a LocalDateTime with timezone information to represent a complete date-time with offset from UTC. Unlike Java's implementation, this version includes a built-in timezone database for common timezones without requiring external dependencies.

Key Features:

  • Immutable: All operations return new instances
  • Timezone Aware: Handles UTC offsets and common timezone names
  • DST Support: Basic daylight saving time handling for major timezones
  • Conversion: Easy conversion between timezones
  • Formatting: ISO 8601 compliant string representation

Supported Timezone Formats:

  • UTC Offsets: +05:00, -08:00, Z (for UTC)
  • Timezone Names: EST, PST, GMT, CET, etc.
  • Full Names: America/New_York, Europe/London, etc.

Usage Examples:

// Create from current time
final now = ZonedDateTime.now();
final nowInParis = ZonedDateTime.now(ZoneId.of('Europe/Paris'));

// Create with specific date-time
final localDT = LocalDateTime.of(2023, 12, 25, 15, 30);
final christmas = ZonedDateTime.of(localDT, ZoneId.of('America/New_York'));

// Parse from string
final parsed = ZonedDateTime.parse('2023-12-25T15:30:00+01:00[Europe/Paris]');

// Convert between timezones
final utc = christmas.toUtc();
final tokyo = christmas.withZoneSameInstant(ZoneId.of('Asia/Tokyo'));

// Arithmetic operations
final tomorrow = christmas.plusDays(1);
final nextHour = christmas.plusHours(1);

Timezone Database:

This implementation includes a comprehensive timezone database with:

  • Major world timezones with their UTC offsets
  • Daylight saving time rules for common timezones
  • Historical timezone data for accurate conversions
  • Support for both abbreviated and full timezone names

Implementation

factory ZonedDateTime.parse(String dateTimeString) {
  // Handle different parsing formats
  String dateTimePart = dateTimeString;
  String? zonePart;
  String? offsetPart;

  // Extract zone ID if present [Zone/ID]
  final zoneMatch = RegExp(r'\[([^\]]+)\]').firstMatch(dateTimeString);
  if (zoneMatch != null) {
    zonePart = zoneMatch.group(1);
    dateTimePart = dateTimeString.substring(0, zoneMatch.start);
  }

  // Extract offset (+HH:mm, -HH:mm, Z)
  final offsetMatch = RegExp(r'([+-]\d{2}:?\d{2}|Z)$').firstMatch(dateTimePart);
  if (offsetMatch != null) {
    offsetPart = offsetMatch.group(1);
    dateTimePart = dateTimePart.substring(0, offsetMatch.start);
  }

  // Parse the date-time part
  final localDateTime = LocalDateTime.parse(dateTimePart);

  // Determine the timezone
  ZoneId zone;
  if (zonePart != null) {
    zone = ZoneId.of(zonePart);
  } else if (offsetPart != null) {
    if (offsetPart == 'Z') {
      zone = ZoneId.UTC;
    } else {
      // Create a zone from offset
      zone = ZoneId.of(offsetPart);
    }
  } else {
    zone = ZoneId.UTC;
  }

  return ZonedDateTime.of(localDateTime, zone);
}