Skip to content
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

Add toISOString() methods #87

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,15 +754,15 @@ public function toNanosPart(): int
}

/**
* Serializes as a string using {@see Duration::__toString()}.
* Serializes as a string using {@see Duration::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns an ISO-8601 string representation of this duration.
* Returns the ISO 8601 representation of this duration.
*
* The format of the returned string will be PTnHnMn.nS, where n is
* the relevant hours, minutes, seconds or nanoseconds part of the duration.
Expand All @@ -772,7 +772,7 @@ public function jsonSerialize(): string
*
* Note that multiples of 24 hours are not output as days to avoid confusion with Period.
*/
public function __toString(): string
public function toISOString(): string
{
$seconds = $this->seconds;
$nanos = $this->nanos;
Expand Down Expand Up @@ -813,4 +813,12 @@ public function __toString(): string

return $string . 'S';
}

/**
* {@see Duration::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
17 changes: 14 additions & 3 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,26 @@ public function toDecimal(): string
}

/**
* Serializes as a string using {@see Instant::__toString()}.
* Serializes as a string using {@see Instant::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

public function __toString(): string
/**
* Returns the ISO 8601 representation of this instant.
*/
public function toISOString(): string
{
return (string) ZonedDateTime::ofInstant($this, TimeZone::utc());
}

/**
* {@see Instant::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
17 changes: 14 additions & 3 deletions src/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,26 @@ public function isEqualTo(Interval $that): bool
}

/**
* Serializes as a string using {@see Interval::__toString()}.
* Serializes as a string using {@see Interval::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

public function __toString(): string
/**
* Returns the ISO 8601 representation of this interval.
*/
public function toISOString(): string
{
return $this->start . '/' . $this->end;
}

/**
* {@see Interval::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
16 changes: 12 additions & 4 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,23 +747,31 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable
}

/**
* Serializes as a string using {@see LocalDate::__toString()}.
* Serializes as a string using {@see LocalDate::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this LocalDate.
* Returns the ISO 8601 representation of this date.
*/
public function __toString(): string
public function toISOString(): string
{
$pattern = ($this->year < 0 ? '%05d' : '%04d') . '-%02d-%02d';

return sprintf($pattern, $this->year, $this->month, $this->day);
}

/**
* {@see LocalDate::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Resolves the date, resolving days past the end of month.
*
Expand Down
16 changes: 12 additions & 4 deletions src/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ public function count(): int
}

/**
* Serializes as a string using {@see LocalDateRange::__toString()}.
* Serializes as a string using {@see LocalDateRange::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
Expand All @@ -260,10 +260,18 @@ public function toNativeDatePeriod(): DatePeriod
}

/**
* Returns an ISO 8601 string representation of this date range.
* Returns the ISO 8601 representation of this date range.
*/
public function __toString(): string
public function toISOString(): string
{
return $this->start . '/' . $this->end;
}

/**
* {@see LocalDateRange::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
17 changes: 14 additions & 3 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,18 +716,29 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable
}

/**
* Serializes as a string using {@see LocalDateTime::__toString()}.
* Serializes as a string using {@see LocalDateTime::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

public function __toString(): string
/**
* Returns the ISO 8601 representation of this date time.
*/
public function toISOString(): string
{
return $this->date . 'T' . $this->time;
}

/**
* {@see LocalDateTime::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns a copy of this `LocalDateTime` with the specified period added.
*
Expand Down
18 changes: 13 additions & 5 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,17 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable
}

/**
* Serializes as a string using {@see LocalTime::__toString()}.
* Serializes as a string using {@see LocalTime::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns this time as a string, such as 10:15.
* Returns the ISO 8601 representation of this time.
*
* The output will be one of the following ISO-8601 formats:
* The output will be one of the following formats:
*
* * `HH:mm`
* * `HH:mm:ss`
Expand All @@ -660,7 +660,7 @@ public function jsonSerialize(): string
* the time where the omitted parts are implied to be zero.
* The nanoseconds value, if present, can be 0 to 9 digits.
*/
public function __toString(): string
public function toISOString(): string
{
if ($this->nano === 0) {
if ($this->second === 0) {
Expand All @@ -674,4 +674,12 @@ public function __toString(): string

return sprintf('%02u:%02u:%02u.%s', $this->hour, $this->minute, $this->second, $nanos);
}

/**
* {@see LocalTime::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
17 changes: 14 additions & 3 deletions src/MonthDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,26 @@ public function atYear(int $year): LocalDate
}

/**
* Serializes as a string using {@see MonthDay::__toString()}.
* Serializes as a string using {@see MonthDay::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

public function __toString(): string
/**
* Returns the ISO 8601 representation of this month-day.
*/
public function toISOString(): string
{
return sprintf('--%02d-%02d', $this->month, $this->day);
}

/**
* {@see MonthDay::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
17 changes: 14 additions & 3 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,17 @@ public function toNativeDateInterval(): DateInterval
}

/**
* Serializes as a string using {@see Period::__toString()}.
* Serializes as a string using {@see Period::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

public function __toString(): string
/**
* Returns the ISO 8601 representation of this period.
*/
public function toISOString(): string
{
if ($this->isZero()) {
return 'P0D';
Expand All @@ -404,4 +407,12 @@ public function __toString(): string

return $string;
}

/**
* {@see Period::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
17 changes: 14 additions & 3 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,26 @@ public function toLocalDateRange(): LocalDateRange
}

/**
* Serializes as a string using {@see Year::__toString()}.
* Serializes as a string using {@see Year::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

public function __toString(): string
/**
* Returns the ISO 8601 representation of this year.
*/
public function toISOString(): string
{
return (string) $this->year;
}

/**
* {@see Year::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}
}
18 changes: 14 additions & 4 deletions src/YearMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,28 @@ public function toLocalDateRange(): LocalDateRange
}

/**
* Serializes as a string using {@see YearMonth::__toString()}.
* Serializes as a string using {@see YearMonth::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this YearMonth.
* Returns the ISO 8601 representation of this year-month.
*/
public function toISOString(): string
{
$pattern = ($this->year < 0 ? '%05d' : '%04d') . '-%02d';

return sprintf($pattern, $this->year, $this->month);
gnutix marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* {@see YearMonth::toISOString()}.
*/
public function __toString(): string
{
return sprintf('%02u-%02u', $this->year, $this->month);
return $this->toISOString();
}
}
Loading
Loading