
DateTime::sub을 사용하여 날짜를 계산할 때, `$now`과 `$one_day_ago`의 타입이 같아 `$one_day_ago`의 날짜가 `$now`보다 이전인지 확인하는 코드가 작동을 하지 않는 문제를 해결하기 위해, `$one_day_ago`의 타입을 DateTime::Duration으로 변경하는 방법은 다음과 같습니다.
#hostingforum.kr
perl
use DateTime;
my $now = DateTime->now();
my $one_day_ago = $now->subtract(days => 1);
my $duration = $one_day_ago->subtract($now);
if ($duration->is_positive()) {
print "one_day_ago는 now보다 이전입니다.n";
} else {
print "one_day_ago는 now보다 이후입니다.n";
}
위 코드에서 `$duration`은 `$one_day_ago`와 `$now`의 차이를 나타내는 DateTime::Duration 객체입니다. `is_positive()` 메서드를 사용하여 `$duration`이 양수인지 음수인지 확인할 수 있습니다. 만약 양수라면 `$one_day_ago`은 `$now`보다 이전입니다.
또한, `$now`과 `$one_day_ago`의 타입이 같을 때 `$one_day_ago`의 날짜가 `$now`보다 이전인지 확인할 수 있는 방법은 다음과 같습니다.
#hostingforum.kr
perl
use DateTime;
my $now = DateTime->now();
my $one_day_ago = $now->subtract(days => 1);
if ($one_day_ago->is_previous($now)) {
print "one_day_ago는 now보다 이전입니다.n";
} else {
print "one_day_ago는 now보다 이후입니다.n";
}
위 코드에서 `is_previous()` 메서드를 사용하여 `$one_day_ago`이 `$now`보다 이전인지 확인할 수 있습니다. 만약 `$one_day_ago`이 `$now`보다 이전이라면 `is_previous()` 메서드는 true를 반환합니다.
2025-07-23 23:30