Created
February 3, 2015 03:15
-
-
Save gypark/d0c9f5e2b7d908513105 to your computer and use it in GitHub Desktop.
DateTime math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use DateTime; | |
use 5.010; | |
foreach my $date ( | |
[ 1, 1 ], | |
[ 1, 27 ], | |
[ 1, 28 ], | |
[ 1, 29 ], | |
[ 1, 30 ], | |
[ 1, 31 ], | |
[ 2, 1 ], | |
[ 2, 27 ], | |
[ 2, 28 ], | |
[ 3, 1 ], | |
[ 3, 30 ], | |
[ 3, 31 ], | |
[ 4, 30 ], | |
[ 5, 31 ], | |
[ 6, 30 ], | |
[ 7, 31 ], | |
[ 8, 31 ], | |
[ 9, 30 ], | |
[10, 31 ], | |
[11, 30 ], | |
[12, 31 ] | |
) | |
{ | |
my ($month, $day) = @{$date}; | |
my $date = DateTime->new( | |
year => 2015, | |
month => $month, | |
day => $day, | |
time_zone => 'local', | |
)->set_time_zone('floating'); | |
say "$month / $day => "; | |
# 다음 달 | |
say " add_month(wrap) : ", $date->clone->add( months => 1, end_of_month => 'wrap' ); | |
say " add_month(limit) : ", $date->clone->add( months => 1, end_of_month => 'limit' ); | |
say " add_month(preserve) : ", $date->clone->add( months => 1, end_of_month => 'preserve' ); | |
# 다음 달 10일 | |
say " add 1 month first : ", $date->clone->add( months => 1 )->set_day(10); # 잘못!! | |
say " set_day first : ", $date->clone->set_day(10)->add( months => 1 ); | |
} |
Author
gypark
commented
Feb 3, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment