// Copyright 2008 by Basil Vandegriend.  All rights reserved.

package com.basilv.resumecalculator;

import org.joda.time.Days;
import org.joda.time.Duration;

import com.basilv.core.Assert;


public class DateTimeUtils
{
	private static final double daysPerYear = 365.26;
	private static final double daysPerMonth = daysPerYear/12;
	
	/** 
	 * Calculate the number of months whole and partial (i.e. unrounded) for the supplied duration. 
	 * @param duration Cannot be null.
	 * @return the number of months.
	 */
	public static double convertDurationToMonths(Duration duration) {
		Assert.notNull("duration", duration);

		int days = convertDurationToDays(duration);
		double months = days / daysPerMonth;
		return months;
	}

	
	/** 
	 * Calculate the number of years whole and partial (i.e. unrounded) for the supplied duration. 
	 * @param duration Cannot be null.
	 * @return the number of years.
	 */
	public static double convertDurationToYears(Duration duration) {
		Assert.notNull("duration", duration);

		int days = convertDurationToDays(duration);
		double years = days / daysPerYear;
		return years;
	}
	
	private static int convertDurationToDays(Duration duration) {
		Days days = Days.daysIn(duration.toIntervalFrom(null));
		return days.getDays();
	}


}
