api: 규격 정의
This commit is contained in:
+40
-33
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace sammo;
|
||||
|
||||
use phpDocumentor\Reflection\Types\Boolean;
|
||||
|
||||
class TimeUtil
|
||||
{
|
||||
|
||||
@@ -43,7 +46,7 @@ class TimeUtil
|
||||
/** @deprecated */
|
||||
public static function DatetimeFromMinute($date, $minute)
|
||||
{
|
||||
return date('Y-m-d H:i:s', strtotime($date) + $minute*60);
|
||||
return date('Y-m-d H:i:s', strtotime($date) + $minute * 60);
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
@@ -76,62 +79,63 @@ class TimeUtil
|
||||
return date('H:i:s', strtotime('00:00:00') + $second);
|
||||
}
|
||||
|
||||
public static function today():string
|
||||
public static function today(): string
|
||||
{
|
||||
$obj = new \DateTime();
|
||||
return $obj->format('Y-m-d');
|
||||
}
|
||||
|
||||
public static function now(bool $withFraction=false):string
|
||||
public static function now(bool $withFraction = false): string
|
||||
{
|
||||
$obj = new \DateTime();
|
||||
if(!$withFraction){
|
||||
if (!$withFraction) {
|
||||
return $obj->format('Y-m-d H:i:s');
|
||||
}
|
||||
return $obj->format('Y-m-d H:i:s.u');
|
||||
}
|
||||
|
||||
public static function nowAddDays($day, bool $withFraction=false):string
|
||||
public static function nowAddDays($day, bool $withFraction = false): string
|
||||
{
|
||||
$obj = new \DateTime();
|
||||
$obj->add(static::secondsToDateInterval($day * 3600 * 24));
|
||||
if(!$withFraction){
|
||||
if (!$withFraction) {
|
||||
return $obj->format('Y-m-d H:i:s');
|
||||
}
|
||||
return $obj->format('Y-m-d H:i:s.u');
|
||||
}
|
||||
|
||||
public static function nowAddHours($hour, bool $withFraction=false):string
|
||||
public static function nowAddHours($hour, bool $withFraction = false): string
|
||||
{
|
||||
$obj = new \DateTime();
|
||||
$obj->add(static::secondsToDateInterval($hour * 3600));
|
||||
if(!$withFraction){
|
||||
if (!$withFraction) {
|
||||
return $obj->format('Y-m-d H:i:s');
|
||||
}
|
||||
return $obj->format('Y-m-d H:i:s.u');
|
||||
}
|
||||
|
||||
public static function nowAddMinutes($minute, bool $withFraction=false):string
|
||||
public static function nowAddMinutes($minute, bool $withFraction = false): string
|
||||
{
|
||||
$obj = new \DateTime();
|
||||
$obj->add(static::secondsToDateInterval($minute * 60));
|
||||
if(!$withFraction){
|
||||
if (!$withFraction) {
|
||||
return $obj->format('Y-m-d H:i:s');
|
||||
}
|
||||
return $obj->format('Y-m-d H:i:s.u');
|
||||
}
|
||||
|
||||
public static function nowAddSeconds($second, bool $withFraction=false):string
|
||||
public static function nowAddSeconds($second, bool $withFraction = false): string
|
||||
{
|
||||
$obj = new \DateTime();
|
||||
$obj->add(static::secondsToDateInterval($second));
|
||||
if(!$withFraction){
|
||||
if (!$withFraction) {
|
||||
return $obj->format('Y-m-d H:i:s');
|
||||
}
|
||||
return $obj->format('Y-m-d H:i:s.u');
|
||||
}
|
||||
|
||||
public static function secondsToDateTime(float $fullSeconds, bool $isDateTimeImmutable=false): \DateTimeInterface{
|
||||
public static function secondsToDateTime(float $fullSeconds, bool $isDateTimeImmutable = false, bool $isUTC = false): \DateTimeInterface
|
||||
{
|
||||
$seconds = floor($fullSeconds);
|
||||
$fraction = $fullSeconds - $seconds;
|
||||
|
||||
@@ -139,70 +143,73 @@ class TimeUtil
|
||||
$interval->s = $seconds;
|
||||
$interval->f = $fraction;
|
||||
|
||||
if($isDateTimeImmutable){
|
||||
$dateTime = new \DateTimeImmutable("@0");
|
||||
if ($isDateTimeImmutable) {
|
||||
$dateTime = new \DateTimeImmutable("@0", $isUTC ? new \DateTimeZone("UTC") : null);
|
||||
return $dateTime->add($interval);
|
||||
}
|
||||
|
||||
$dateTime = new \DateTime("@0");
|
||||
$dateTime = new \DateTime("@0", $isUTC ? new \DateTimeZone("UTC") : null);
|
||||
$dateTime->add($interval);
|
||||
return $dateTime;
|
||||
}
|
||||
|
||||
public static function secondsToDateInterval(float $fullSeconds): \DateInterval{
|
||||
public static function secondsToDateInterval(float $fullSeconds): \DateInterval
|
||||
{
|
||||
$d0 = new \DateTime("@0");
|
||||
|
||||
return $d0->diff(static::secondsToDateTime($fullSeconds, true));
|
||||
}
|
||||
|
||||
public static function DateTimeToSeconds(\DateTimeInterface $dateTime): float{
|
||||
$d0 = new \DateTimeImmutable("@0");
|
||||
public static function DateTimeToSeconds(\DateTimeInterface $dateTime, bool $isUTC = false): float
|
||||
{
|
||||
$d0 = new \DateTimeImmutable("@0", $isUTC ? new \DateTimeZone("UTC") : null);
|
||||
|
||||
return static::DateIntervalToSeconds($d0->diff($dateTime));
|
||||
}
|
||||
|
||||
public static function DateIntervalToSeconds(\DateInterval $interval): float{
|
||||
if($interval->days !== FALSE){
|
||||
public static function DateIntervalToSeconds(\DateInterval $interval): float
|
||||
{
|
||||
if ($interval->days !== FALSE) {
|
||||
$days = $interval->days;
|
||||
}
|
||||
else{
|
||||
if($interval->y != 0){
|
||||
} else {
|
||||
if ($interval->y != 0) {
|
||||
throw new \InvalidArgumentException('Year argument conversion is not supported');
|
||||
}
|
||||
if($interval->m != 0){
|
||||
if ($interval->m != 0) {
|
||||
throw new \InvalidArgumentException('Month argument conversion is not supported');
|
||||
}
|
||||
$days = $interval->d;
|
||||
}
|
||||
|
||||
|
||||
$hours = $days * 24 + $interval->h;
|
||||
$minutes = $hours * 60 + $interval->i;
|
||||
$seconds = $minutes * 60 + $interval->s + $interval->f;
|
||||
|
||||
|
||||
return $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* $baseYear, $baseMonth 부터 $afterMonth 개월 이내인지. $afterMonth 포함.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public static function IsRangeMonth(int $baseYear, int $baseMonth, int $afterMonth, int $askYear, int $askMonth):bool{
|
||||
if($baseMonth < 1 || $baseMonth > 12){
|
||||
public static function IsRangeMonth(int $baseYear, int $baseMonth, int $afterMonth, int $askYear, int $askMonth): bool
|
||||
{
|
||||
if ($baseMonth < 1 || $baseMonth > 12) {
|
||||
throw new \InvalidArgumentException('개월이 올바르지 않음');
|
||||
}
|
||||
if($askMonth < 1 || $askMonth > 12){
|
||||
if ($askMonth < 1 || $askMonth > 12) {
|
||||
throw new \InvalidArgumentException('개월이 올바르지 않음');
|
||||
}
|
||||
|
||||
$minMonth = $baseYear * 12 + $baseMonth;
|
||||
if($afterMonth < 0){
|
||||
if ($afterMonth < 0) {
|
||||
$maxMonth = $minMonth;
|
||||
$minMonth = $maxMonth - $afterMonth;
|
||||
}
|
||||
|
||||
$maxMonth = $minMonth + $afterMonth;
|
||||
$askMonth = $askYear * 12 + $askMonth;
|
||||
if($askMonth < $minMonth || $maxMonth < $askMonth){
|
||||
if ($askMonth < $minMonth || $maxMonth < $askMonth) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user