| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | |
|---|
| 5 | use Test::More tests => 135; |
|---|
| 6 | |
|---|
| 7 | use DateTime::Lite qw(Arithmetic); |
|---|
| 8 | |
|---|
| 9 | my $d = DateTime::Lite->new( year => 2001, |
|---|
| 10 | month => 7, |
|---|
| 11 | day => 5, |
|---|
| 12 | hour => 2, |
|---|
| 13 | minute => 12, |
|---|
| 14 | second => 50, |
|---|
| 15 | time_zone => 'UTC', |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | is( $d->year, 2001, '->year' ); |
|---|
| 19 | is( $d->ce_year, 2001, '->ce_year' ); |
|---|
| 20 | is( $d->month, 7, '->month' ); |
|---|
| 21 | is( $d->quarter, 3, '->quarter'); |
|---|
| 22 | is( $d->month_0, 6, '->month_0' ); |
|---|
| 23 | is( $d->month_name, 'July', '->month_name' ); |
|---|
| 24 | is( $d->month_abbr, 'Jul', '->month_abbr' ); |
|---|
| 25 | is( $d->day_of_month, 5, '->day_of_month' ); |
|---|
| 26 | is( $d->day_of_month_0, 4, '->day_of_month_0' ); |
|---|
| 27 | is( $d->day, 5, '->day' ); |
|---|
| 28 | is( $d->day_0, 4, '->day_0' ); |
|---|
| 29 | is( $d->mday, 5, '->mday' ); |
|---|
| 30 | is( $d->mday_0, 4, '->mday_0' ); |
|---|
| 31 | is( $d->mday, 5, '->mday' ); |
|---|
| 32 | is( $d->mday_0, 4, '->mday_0' ); |
|---|
| 33 | is( $d->hour, 2, '->hour' ); |
|---|
| 34 | is( $d->hour_1, 2, '->hour_1' ); |
|---|
| 35 | is( $d->hour_12, 2, '->hour_12' ); |
|---|
| 36 | is( $d->hour_12_0, 2, '->hour_12_0' ); |
|---|
| 37 | is( $d->minute, 12, '->minute' ); |
|---|
| 38 | is( $d->min, 12, '->min' ); |
|---|
| 39 | is( $d->second, 50, '->second' ); |
|---|
| 40 | is( $d->sec, 50, '->sec' ); |
|---|
| 41 | |
|---|
| 42 | is( $d->day_of_year, 186, '->day_of_year' ); |
|---|
| 43 | is( $d->day_of_year_0, 185, '->day_of_year' ); |
|---|
| 44 | is( $d->day_of_quarter, 5, '->day_of_quarter' ); |
|---|
| 45 | is( $d->doq, 5, '->doq' ); |
|---|
| 46 | is( $d->day_of_quarter_0, 4, '->day_of_quarter_0' ); |
|---|
| 47 | is( $d->doq_0, 4, '->doq_0' ); |
|---|
| 48 | is( $d->day_of_week, 4, '->day_of_week' ); |
|---|
| 49 | is( $d->day_of_week_0, 3, '->day_of_week_0' ); |
|---|
| 50 | is( $d->week_of_month, 1, '->week_of_month' ); |
|---|
| 51 | is( $d->weekday_of_month, 1, '->weekday_of_month' ); |
|---|
| 52 | is( $d->wday, 4, '->wday' ); |
|---|
| 53 | is( $d->wday_0, 3, '->wday_0' ); |
|---|
| 54 | is( $d->dow, 4, '->dow' ); |
|---|
| 55 | is( $d->dow_0, 3, '->dow_0' ); |
|---|
| 56 | is( $d->day_name, 'Thursday', '->day_name' ); |
|---|
| 57 | is( $d->day_abbr, 'Thu', '->day_abrr' ); |
|---|
| 58 | |
|---|
| 59 | is( $d->ymd, '2001-07-05', '->ymd' ); |
|---|
| 60 | is( $d->ymd('!'), '2001!07!05', "->ymd('!')" ); |
|---|
| 61 | is( $d->date, '2001-07-05', '->ymd' ); |
|---|
| 62 | |
|---|
| 63 | is( $d->mdy, '07-05-2001', '->mdy' ); |
|---|
| 64 | is( $d->mdy('!'), '07!05!2001', "->mdy('!')" ); |
|---|
| 65 | |
|---|
| 66 | is( $d->dmy, '05-07-2001', '->dmy' ); |
|---|
| 67 | is( $d->dmy('!'), '05!07!2001', "->dmy('!')" ); |
|---|
| 68 | |
|---|
| 69 | is( $d->hms, '02:12:50', '->hms' ); |
|---|
| 70 | is( $d->hms('!'), '02!12!50', "->hms('!')" ); |
|---|
| 71 | is( $d->time, '02:12:50', '->hms' ); |
|---|
| 72 | |
|---|
| 73 | is( $d->datetime, '2001-07-05T02:12:50', '->datetime' ); |
|---|
| 74 | is( $d->iso8601, '2001-07-05T02:12:50', '->iso8601' ); |
|---|
| 75 | |
|---|
| 76 | is( $d->is_leap_year, 0, '->is_leap_year' ); |
|---|
| 77 | |
|---|
| 78 | is( $d->era_abbr, 'AD', '->era_abbr' ); |
|---|
| 79 | is( $d->era, $d->era_abbr, '->era (deprecated)' ); |
|---|
| 80 | is( $d->era_name, 'Anno Domini', '->era_abbr' ); |
|---|
| 81 | |
|---|
| 82 | is( $d->quarter_abbr, 'Q3', '->quarter_abbr' ); |
|---|
| 83 | is( $d->quarter_name, '3rd quarter', '->quarter_name' ); |
|---|
| 84 | |
|---|
| 85 | my $leap_d = DateTime::Lite->new( year => 2004, |
|---|
| 86 | month => 7, |
|---|
| 87 | day => 5, |
|---|
| 88 | hour => 2, |
|---|
| 89 | minute => 12, |
|---|
| 90 | second => 50, |
|---|
| 91 | time_zone => 'UTC', |
|---|
| 92 | ); |
|---|
| 93 | |
|---|
| 94 | is( $leap_d->is_leap_year, 1, '->is_leap_year' ); |
|---|
| 95 | |
|---|
| 96 | my $sunday = DateTime::Lite->new( year => 2003, |
|---|
| 97 | month => 1, |
|---|
| 98 | day => 26, |
|---|
| 99 | time_zone => 'UTC', |
|---|
| 100 | ); |
|---|
| 101 | |
|---|
| 102 | is( $sunday->day_of_week, 7, "Sunday is day 7" ); |
|---|
| 103 | |
|---|
| 104 | my $monday = DateTime::Lite->new( year => 2003, |
|---|
| 105 | month => 1, |
|---|
| 106 | day => 27, |
|---|
| 107 | time_zone => 'UTC', |
|---|
| 108 | ); |
|---|
| 109 | |
|---|
| 110 | is( $monday->day_of_week, 1, "Monday is day 1" ); |
|---|
| 111 | |
|---|
| 112 | { |
|---|
| 113 | # time zone offset should not affect the values returned |
|---|
| 114 | my $d = DateTime::Lite->new( year => 2001, |
|---|
| 115 | month => 7, |
|---|
| 116 | day => 5, |
|---|
| 117 | hour => 2, |
|---|
| 118 | minute => 12, |
|---|
| 119 | second => 50, |
|---|
| 120 | time_zone => '-0124', |
|---|
| 121 | ); |
|---|
| 122 | |
|---|
| 123 | is( $d->year, 2001, '->year' ); |
|---|
| 124 | is( $d->ce_year, 2001, '->ce_year' ); |
|---|
| 125 | is( $d->month, 7, '->month' ); |
|---|
| 126 | is( $d->day_of_month, 5, '->day_of_month' ); |
|---|
| 127 | is( $d->hour, 2, '->hour' ); |
|---|
| 128 | is( $d->hour_1, 2, '->hour_1' ); |
|---|
| 129 | is( $d->minute, 12, '->minute' ); |
|---|
| 130 | is( $d->second, 50, '->second' ); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | { |
|---|
| 134 | my $dt0 = DateTime::Lite->new( year => 1, time_zone => 'UTC' ); |
|---|
| 135 | |
|---|
| 136 | is( $dt0->year, 1, "year 1 is year 1" ); |
|---|
| 137 | is( $dt0->ce_year, 1, "ce_year 1 is year 1" ); |
|---|
| 138 | is( $dt0->era_abbr, 'AD', 'era is AD' ); |
|---|
| 139 | is( $dt0->year_with_era, '1AD', 'year_with_era is 1AD' ); |
|---|
| 140 | is( $dt0->christian_era, 'AD', 'christian_era is AD' ); |
|---|
| 141 | is( $dt0->year_with_christian_era, '1AD', 'year_with_christian_era is 1AD' ); |
|---|
| 142 | is( $dt0->secular_era, 'CE', 'secular_era is CE' ); |
|---|
| 143 | is( $dt0->year_with_secular_era, '1CE', 'year_with_secular_era is 1CE' ); |
|---|
| 144 | |
|---|
| 145 | $dt0->subtract( years => 1 ); |
|---|
| 146 | |
|---|
| 147 | is( $dt0->year, 0, "year 1 minus 1 is year 0" ); |
|---|
| 148 | is( $dt0->ce_year, -1, "ce_year 1 minus 1 is year -1" ); |
|---|
| 149 | is( $dt0->era_abbr, 'BC', 'era is BC' ); |
|---|
| 150 | is( $dt0->year_with_era, '1BC', 'year_with_era is 1BC' ); |
|---|
| 151 | is( $dt0->christian_era, 'BC', 'christian_era is BC' ); |
|---|
| 152 | is( $dt0->year_with_christian_era, '1BC', 'year_with_christian_era is 1BC' ); |
|---|
| 153 | is( $dt0->secular_era, 'BCE', 'secular_era is BCE' ); |
|---|
| 154 | is( $dt0->year_with_secular_era, '1BCE', 'year_with_secular_era is 1BCE' ); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | { |
|---|
| 158 | my $dt_neg = DateTime::Lite->new( year => -10, time_zone => 'UTC', ); |
|---|
| 159 | is( $dt_neg->year, -10, "Year -10 is -10" ); |
|---|
| 160 | is( $dt_neg->ce_year, -11, "year -10 is ce_year -11" ); |
|---|
| 161 | |
|---|
| 162 | SKIP: { |
|---|
| 163 | skip "duration not yet implemented", 2; |
|---|
| 164 | my $dt1 = $dt_neg + DateTime::Lite::Duration->new( years => 10 ); |
|---|
| 165 | is( $dt1->year, 0, "year is 0 after adding ten years to year -10" ); |
|---|
| 166 | is( $dt1->ce_year, -1, "ce_year is -1 after adding ten years to year -10" ); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | { |
|---|
| 171 | my $dt = DateTime::Lite->new( year => 50, month => 2, |
|---|
| 172 | hour => 3, minute => 20, second => 5, |
|---|
| 173 | time_zone => 'UTC', |
|---|
| 174 | ); |
|---|
| 175 | |
|---|
| 176 | is( $dt->ymd('%s'), '0050%s02%s01', 'use %s as separator in ymd' ); |
|---|
| 177 | is( $dt->mdy('%s'), '02%s01%s0050', 'use %s as separator in mdy' ); |
|---|
| 178 | is( $dt->dmy('%s'), '01%s02%s0050', 'use %s as separator in dmy' ); |
|---|
| 179 | |
|---|
| 180 | is( $dt->hms('%s'), '03%s20%s05', 'use %s as separator in hms' ); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | # test doy in leap year |
|---|
| 184 | { |
|---|
| 185 | my $dt = DateTime::Lite->new( year => 2000, month => 1, day => 5, |
|---|
| 186 | time_zone => 'UTC', |
|---|
| 187 | ); |
|---|
| 188 | |
|---|
| 189 | is( $dt->day_of_year, 5, 'doy for 2000-01-05 should be 5' ); |
|---|
| 190 | is( $dt->day_of_year_0, 4, 'doy_0 for 2000-01-05 should be 4' ); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | { |
|---|
| 194 | my $dt = DateTime::Lite->new( year => 2000, month => 2, day => 29, |
|---|
| 195 | time_zone => 'UTC', |
|---|
| 196 | ); |
|---|
| 197 | |
|---|
| 198 | is( $dt->day_of_year, 60, 'doy for 2000-02-29 should be 60' ); |
|---|
| 199 | is( $dt->day_of_year_0, 59, 'doy_0 for 2000-02-29 should be 59' ); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | { |
|---|
| 203 | my $dt = DateTime::Lite->new( year => -6, month => 2, day => 25, |
|---|
| 204 | time_zone => 'UTC', |
|---|
| 205 | ); |
|---|
| 206 | |
|---|
| 207 | is( $dt->ymd, '-0006-02-25', 'ymd is -0006-02-25' ); |
|---|
| 208 | is( $dt->iso8601, '-0006-02-25T00:00:00', 'iso8601 is -0005-02-25T00:00:00' ); |
|---|
| 209 | is( $dt->year, -6, 'year is -6' ); |
|---|
| 210 | is( $dt->ce_year, -7, 'ce_year is -7' ); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | { |
|---|
| 214 | my $dt = DateTime::Lite->new( year => 1996, month => 2, day => 1 ); |
|---|
| 215 | |
|---|
| 216 | is( $dt->quarter, 1, '->quarter is 1' ); |
|---|
| 217 | is( $dt->day_of_quarter, 32, '->day_of_quarter' ); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | { |
|---|
| 221 | my $dt = DateTime::Lite->new( year => 1996, month => 5, day => 1 ); |
|---|
| 222 | |
|---|
| 223 | is( $dt->quarter, 2, '->quarter is 2' ); |
|---|
| 224 | is( $dt->day_of_quarter, 31, '->day_of_quarter' ); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | { |
|---|
| 228 | my $dt = DateTime::Lite->new( year => 1996, month => 8, day => 1 ); |
|---|
| 229 | |
|---|
| 230 | is( $dt->quarter, 3, '->quarter is 3' ); |
|---|
| 231 | is( $dt->day_of_quarter, 32, '->day_of_quarter' ); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | { |
|---|
| 235 | my $dt = DateTime::Lite->new( year => 1996, month => 11, day => 1 ); |
|---|
| 236 | |
|---|
| 237 | is( $dt->quarter, 4, '->quarter is 4' ); |
|---|
| 238 | is( $dt->day_of_quarter, 32, '->day_of_quarter' ); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | # nano, micro, and milli seconds |
|---|
| 242 | { |
|---|
| 243 | my $dt = DateTime::Lite->new( year => 1996, nanosecond => 500_000_000 ); |
|---|
| 244 | |
|---|
| 245 | is( $dt->nanosecond, 500_000_000, 'nanosecond is 500,000,000' ); |
|---|
| 246 | is( $dt->microsecond, 500_000, 'microsecond is 500,000' ); |
|---|
| 247 | is( $dt->millisecond, 500, 'millisecond is 500' ); |
|---|
| 248 | |
|---|
| 249 | $dt->set( nanosecond => 500_000_500 ); |
|---|
| 250 | |
|---|
| 251 | is( $dt->nanosecond, 500_000_500, 'nanosecond is 500,000,500' ); |
|---|
| 252 | is( $dt->microsecond, 500_001, 'microsecond is 500,001' ); |
|---|
| 253 | is( $dt->millisecond, 500, 'millisecond is 500' ); |
|---|
| 254 | |
|---|
| 255 | $dt->set( nanosecond => 499_999_999 ); |
|---|
| 256 | |
|---|
| 257 | is( $dt->nanosecond, 499_999_999, 'nanosecond is 499,999,999' ); |
|---|
| 258 | is( $dt->microsecond, 500_000, 'microsecond is 500,000' ); |
|---|
| 259 | is( $dt->millisecond, 500, 'millisecond is 500' ); |
|---|
| 260 | |
|---|
| 261 | $dt->set( nanosecond => 450_000_001 ); |
|---|
| 262 | |
|---|
| 263 | is( $dt->nanosecond, 450_000_001, 'nanosecond is 450,000,001' ); |
|---|
| 264 | is( $dt->microsecond, 450_000, 'microsecond is 450,000' ); |
|---|
| 265 | is( $dt->millisecond, 450, 'millisecond is 450' ); |
|---|
| 266 | |
|---|
| 267 | $dt->set( nanosecond => 450_500_000 ); |
|---|
| 268 | |
|---|
| 269 | is( $dt->nanosecond, 450_500_000, 'nanosecond is 450,500,000' ); |
|---|
| 270 | is( $dt->microsecond, 450_500, 'microsecond is 450,500' ); |
|---|
| 271 | is( $dt->millisecond, 451, 'millisecond is 451' ); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | { |
|---|
| 275 | my $dt = DateTime::Lite->new( year => 2003, month => 5, day => 7 ); |
|---|
| 276 | is( $dt->weekday_of_month, 1, '->weekday_of_month' ); |
|---|
| 277 | is( $dt->week_of_month, 2, '->week_of_month' ); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | { |
|---|
| 281 | my $dt = DateTime::Lite->new( year => 2003, month => 5, day => 8 ); |
|---|
| 282 | is( $dt->weekday_of_month, 2, '->weekday_of_month' ); |
|---|
| 283 | is( $dt->week_of_month, 2, '->week_of_month' ); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | { |
|---|
| 287 | my $dt = DateTime::Lite->new( year => 1000, hour => 23 ); |
|---|
| 288 | is( $dt->hour, 23, '->hour' ); |
|---|
| 289 | is( $dt->hour_1, 23, '->hour_1' ); |
|---|
| 290 | is( $dt->hour_12, 11, '->hour_12' ); |
|---|
| 291 | is( $dt->hour_12_0, 11, '->hour_12_0' ); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | { |
|---|
| 295 | my $dt = DateTime::Lite->new( year => 1000, hour => 0 ); |
|---|
| 296 | is( $dt->hour, 0, '->hour' ); |
|---|
| 297 | is( $dt->hour_1, 24, '->hour_1' ); |
|---|
| 298 | is( $dt->hour_12, 12, '->hour_12' ); |
|---|
| 299 | is( $dt->hour_12_0, 0, '->hour_12_0' ); |
|---|
| 300 | } |
|---|