Skip to content

Instantly share code, notes, and snippets.

@gypark
Created February 3, 2015 03:15
Show Gist options
  • Save gypark/d0c9f5e2b7d908513105 to your computer and use it in GitHub Desktop.
Save gypark/d0c9f5e2b7d908513105 to your computer and use it in GitHub Desktop.
DateTime math
!/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 );
}
@gypark
Copy link
Author

gypark commented Feb 3, 2015

1 / 1 =>
 add_month(wrap)     : 2015-02-01T00:00:00
 add_month(limit)    : 2015-02-01T00:00:00
 add_month(preserve) : 2015-02-01T00:00:00
 add 1 month first : 2015-02-10T00:00:00
 set_day first     : 2015-02-10T00:00:00
1 / 27 =>
 add_month(wrap)     : 2015-02-27T00:00:00
 add_month(limit)    : 2015-02-27T00:00:00
 add_month(preserve) : 2015-02-27T00:00:00
 add 1 month first : 2015-02-10T00:00:00
 set_day first     : 2015-02-10T00:00:00
1 / 28 =>
 add_month(wrap)     : 2015-02-28T00:00:00
 add_month(limit)    : 2015-02-28T00:00:00
 add_month(preserve) : 2015-02-28T00:00:00
 add 1 month first : 2015-02-10T00:00:00
 set_day first     : 2015-02-10T00:00:00
1 / 29 =>
 add_month(wrap)     : 2015-03-01T00:00:00
 add_month(limit)    : 2015-02-28T00:00:00
 add_month(preserve) : 2015-02-28T00:00:00
 add 1 month first : 2015-03-10T00:00:00
 set_day first     : 2015-02-10T00:00:00
1 / 30 =>
 add_month(wrap)     : 2015-03-02T00:00:00
 add_month(limit)    : 2015-02-28T00:00:00
 add_month(preserve) : 2015-02-28T00:00:00
 add 1 month first : 2015-03-10T00:00:00
 set_day first     : 2015-02-10T00:00:00
1 / 31 =>
 add_month(wrap)     : 2015-03-03T00:00:00
 add_month(limit)    : 2015-02-28T00:00:00
 add_month(preserve) : 2015-02-28T00:00:00
 add 1 month first : 2015-03-10T00:00:00
 set_day first     : 2015-02-10T00:00:00
2 / 1 =>
 add_month(wrap)     : 2015-03-01T00:00:00
 add_month(limit)    : 2015-03-01T00:00:00
 add_month(preserve) : 2015-03-01T00:00:00
 add 1 month first : 2015-03-10T00:00:00
 set_day first     : 2015-03-10T00:00:00
2 / 27 =>
 add_month(wrap)     : 2015-03-27T00:00:00
 add_month(limit)    : 2015-03-27T00:00:00
 add_month(preserve) : 2015-03-27T00:00:00
 add 1 month first : 2015-03-10T00:00:00
 set_day first     : 2015-03-10T00:00:00
2 / 28 =>
 add_month(wrap)     : 2015-03-28T00:00:00
 add_month(limit)    : 2015-03-28T00:00:00
 add_month(preserve) : 2015-03-31T00:00:00
 add 1 month first : 2015-03-10T00:00:00
 set_day first     : 2015-03-10T00:00:00
3 / 1 =>
 add_month(wrap)     : 2015-04-01T00:00:00
 add_month(limit)    : 2015-04-01T00:00:00
 add_month(preserve) : 2015-04-01T00:00:00
 add 1 month first : 2015-04-10T00:00:00
 set_day first     : 2015-04-10T00:00:00
3 / 30 =>
 add_month(wrap)     : 2015-04-30T00:00:00
 add_month(limit)    : 2015-04-30T00:00:00
 add_month(preserve) : 2015-04-30T00:00:00
 add 1 month first : 2015-04-10T00:00:00
 set_day first     : 2015-04-10T00:00:00
3 / 31 =>
 add_month(wrap)     : 2015-05-01T00:00:00
 add_month(limit)    : 2015-04-30T00:00:00
 add_month(preserve) : 2015-04-30T00:00:00
 add 1 month first : 2015-05-10T00:00:00
 set_day first     : 2015-04-10T00:00:00
4 / 30 =>
 add_month(wrap)     : 2015-05-30T00:00:00
 add_month(limit)    : 2015-05-30T00:00:00
 add_month(preserve) : 2015-05-31T00:00:00
 add 1 month first : 2015-05-10T00:00:00
 set_day first     : 2015-05-10T00:00:00
5 / 31 =>
 add_month(wrap)     : 2015-07-01T00:00:00
 add_month(limit)    : 2015-06-30T00:00:00
 add_month(preserve) : 2015-06-30T00:00:00
 add 1 month first : 2015-07-10T00:00:00
 set_day first     : 2015-06-10T00:00:00
6 / 30 =>
 add_month(wrap)     : 2015-07-30T00:00:00
 add_month(limit)    : 2015-07-30T00:00:00
 add_month(preserve) : 2015-07-31T00:00:00
 add 1 month first : 2015-07-10T00:00:00
 set_day first     : 2015-07-10T00:00:00
7 / 31 =>
 add_month(wrap)     : 2015-08-31T00:00:00
 add_month(limit)    : 2015-08-31T00:00:00
 add_month(preserve) : 2015-08-31T00:00:00
 add 1 month first : 2015-08-10T00:00:00
 set_day first     : 2015-08-10T00:00:00
8 / 31 =>
 add_month(wrap)     : 2015-10-01T00:00:00
 add_month(limit)    : 2015-09-30T00:00:00
 add_month(preserve) : 2015-09-30T00:00:00
 add 1 month first : 2015-10-10T00:00:00
 set_day first     : 2015-09-10T00:00:00
9 / 30 =>
 add_month(wrap)     : 2015-10-30T00:00:00
 add_month(limit)    : 2015-10-30T00:00:00
 add_month(preserve) : 2015-10-31T00:00:00
 add 1 month first : 2015-10-10T00:00:00
 set_day first     : 2015-10-10T00:00:00
10 / 31 =>
 add_month(wrap)     : 2015-12-01T00:00:00
 add_month(limit)    : 2015-11-30T00:00:00
 add_month(preserve) : 2015-11-30T00:00:00
 add 1 month first : 2015-12-10T00:00:00
 set_day first     : 2015-11-10T00:00:00
11 / 30 =>
 add_month(wrap)     : 2015-12-30T00:00:00
 add_month(limit)    : 2015-12-30T00:00:00
 add_month(preserve) : 2015-12-31T00:00:00
 add 1 month first : 2015-12-10T00:00:00
 set_day first     : 2015-12-10T00:00:00
12 / 31 =>
 add_month(wrap)     : 2016-01-31T00:00:00
 add_month(limit)    : 2016-01-31T00:00:00
 add_month(preserve) : 2016-01-31T00:00:00
 add 1 month first : 2016-01-10T00:00:00
 set_day first     : 2016-01-10T00:00:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment