Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result of interval.getTotalMonths #33

Open
hackwaly opened this issue Jun 25, 2022 · 0 comments
Open

Wrong result of interval.getTotalMonths #33

hackwaly opened this issue Jun 25, 2022 · 0 comments

Comments

@hackwaly
Copy link

hackwaly commented Jun 25, 2022

Consider interval of 2021-06-26, 2022-06-25. interval.getTotalMonths returns -1 instead of 11.

Here's my code fix it outside of datetime package.

@:access(datetime)
class IntervalFix {
	public static function getTotalMonthsFix(interval:DateTimeIntervalCore) {
		final begin = interval.begin;
		final end = interval.end;
		var months = (end.getYear() - begin.getYear()) * 12 + (end.getMonth() - begin.getMonth());
		var d1 = begin.getDay();
		var d2 = end.getDay();
		if (d2 < d1) {
			months --;
		} else if (d1 == d2) {
			var h1 = begin.getHour();
			var h2 = end.getHour();
			if (h2 < h1) {
				months --;
			} else if (h2 == h1) {
				var m1 = begin.getMinute();
				var m2 = end.getMinute();
				if (m2 < m1) {
					months --;
				} else if (m2 == m1 && end.getSecond() < begin.getSecond()) {
					months --;
				}
			}
		}
		return months;
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant