Skip to content

Commit 4aab9ed

Browse files
authored
feat(datetime): add convenience comparison and manipulation methods (#1450)
1 parent c6f62f4 commit 4aab9ed

File tree

7 files changed

+1204
-111
lines changed

7 files changed

+1204
-111
lines changed

packages/datetime/src/DateTime.php

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ public static function now(?Timezone $timezone = null): DateTime
120120
/**
121121
* Creates a DateTime instance for a specific time on the current day within the specified timezone.
122122
*
123-
* This method facilitates the creation of a {@see DateTime} object representing a precise time on today's date. It is
124-
* particularly useful when you need to set a specific time of day for the current date in a given timezone. The
125-
* method combines the current date context with a specific time, offering a convenient way to specify times such
126-
* as "today at 14:00" in code.
127-
*
128123
* The time components (hours, minutes, seconds, nanoseconds) must be within their valid ranges. The method
129124
* enforces these constraints and throws an {@see Exception\InvalidArgumentException} if any component is out of bounds.
130125
*
@@ -287,7 +282,7 @@ public static function parse(NativeDateTimeInterface|TemporalInterface|string|in
287282
/**
288283
* Parses a date and time string into an instance of {@see Tempest\DateTime\DateTime} using a specific format pattern, with optional customization for timezone and locale.
289284
*
290-
* This method is specifically designed for cases where a custom format pattern is used to parse the input string.
285+
* This method is specifically designed for cases where a custom format pattern is used to parse the input string.
291286
* It allows for precise control over the parsing process by specifying the exact format pattern that matches the input string.
292287
* Additionally, the method supports specifying a timezone and locale for parsing, enabling accurate interpretation of locale-specific formats.
293288
*
@@ -504,70 +499,6 @@ public function withTime(int $hours, int $minutes, int $seconds = 0, int $nanose
504499
);
505500
}
506501

507-
/**
508-
* Returns a new instance set to midnight of the same day.
509-
*/
510-
public function startOfDay(): static
511-
{
512-
return $this->withTime(0, 0, 0, 0);
513-
}
514-
515-
/**
516-
* Returns a new instance set to the end of the day.
517-
*/
518-
public function endOfDay(): static
519-
{
520-
return $this->withTime(23, 59, 59, 999_999_999);
521-
}
522-
523-
/**
524-
* Returns a new instance set to the start of the week.
525-
*/
526-
public function startOfWeek(): static
527-
{
528-
return $this->withDay($this->day - ($this->getWeekday()->value - 1))->startOfDay();
529-
}
530-
531-
/**
532-
* Returns a new instance set to the end of the week.
533-
*/
534-
public function endOfWeek(): static
535-
{
536-
return $this->withDay($this->getDay() + (7 - $this->getWeekday()->value))->endOfDay();
537-
}
538-
539-
/**
540-
* Returns a new instance set to the start of the month.
541-
*/
542-
public function startOfMonth(): static
543-
{
544-
return $this->withDay(1)->startOfDay();
545-
}
546-
547-
/**
548-
* Returns a new instance set to the end of the month.
549-
*/
550-
public function endOfMonth(): static
551-
{
552-
return $this->withDay(Month::from($this->month)->getDaysForYear($this->year))->endOfDay();
553-
}
554-
555-
/**
556-
* Returns a new instance set to the start of the year.
557-
*/
558-
public function startOfYear(): static
559-
{
560-
return $this->withDate($this->getYear(), 1, 1)->startOfDay();
561-
}
562-
563-
/**
564-
* Returns a new instance set to the end of the year.
565-
*/
566-
public function endOfYear(): static
567-
{
568-
return $this->withDate($this->getYear(), 12, Month::DECEMBER->getDaysForYear($this->getYear()))->endOfDay();
569-
}
570-
571502
#[\Override]
572503
public function jsonSerialize(): array
573504
{

0 commit comments

Comments
 (0)