/*
 * time.cc
 *
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 *
 * This file is part of propeller-clock (hereafter referred to as "this
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 * information.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "time.h"
#include <DS1307.h>
#include <Wire.h>


#define YEAR_MAX 2030
#define YEAR_MIN 2010


// year
static int _year;

// month
static int _month;

// day
static int _day;

// hours
static int _hours;

// minutes
static int _minutes;

// seconds
static int _seconds;

// milliseconds at last update
static unsigned long _last_millis = millis();

// milliseconds carries over from last update
static unsigned long _carry = 0;



int Time::get_year()
{
	return _year;
}


int Time::get_month()
{
	return _month;
}


const char *Time::get_month_name()
{
	static const char *months[] = {
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"November",
		"December",
	};

	return months[ _month - 1 ];
}


int Time::get_day()
{
	return _day;
}


const char *Time::get_day_suffix()
{
	switch( _day )
	{
	case 1:
	case 21:
	case 31:
		return "st";
	case 2:
	case 22:
		return "nd";
	case 3:
	case 23:
		return "rd";
	default:
		return "th";
	}
}


int Time::get_hours()
{
	return _hours;
}


int Time::get_minutes()
{
	return _minutes;
}


int Time::get_seconds()
{
	return _seconds;
}


static int days_in_month( int month, int year )
{
	if( month == 2 )
		return !( year % 4 ) && ( ( year % 100 ) || !( year % 400 ) )? 29 : 28;
	else if( month == 9 || month == 6 || month == 4 || month == 11 )
		return 30;
	else
		return 31;
}


static void load_time()
{
	// get the time from the real-time clock
	int rtc_data[ 7 ];
	RTC.get( rtc_data, true );
	_year = rtc_data[ DS1307_YR ];
	_month = rtc_data[ DS1307_MTH ];
	_day = rtc_data[ DS1307_DAY ];
	_hours = rtc_data[ DS1307_HR ];
	_minutes = rtc_data[ DS1307_MIN ];
	_seconds = rtc_data[ DS1307_SEC ];

	// make sure some numbers are in range
	if( _year < YEAR_MIN || _year > YEAR_MAX ) _year = 2010;
	if( _month < 1 || _month > 12 ) _month = 1;
	if( _day < 1 || _day > days_in_month( _year, _month ) ) _day = 1;
}


void Time::update()
{
	// how many milliseconds have elapsed since we last checked?
	unsigned long millis = ::millis();
	unsigned long delta = millis - _last_millis + _carry;

	// update the previous time and carried-over milliseconds
	_last_millis = millis;
	_carry = delta % 1000;

	// add the seconds that have passed to the time
	_seconds += delta / 1000;
	while( _seconds >= 60 ) {
		_seconds -= 60;
		_minutes++;
		if( _minutes >= 60 ) {
			_minutes -= 60;
			_hours++;
			if( _hours >= 24 ) {
				_hours -= 24;

				// We *could* manually work out the day here... but we might as
				// well ask the RTC hardware. We can't do this *all* the time
				// though, because it isn't fast enough and will actually cause
				// a minor display glitch.
				load_time();
			}
		}
	}
}


void Time::init()
{
	load_time();
}


static void save_time()
{
	// set the time on the real-time clock
	RTC.stop();
	RTC.set( DS1307_YR, _year );
	RTC.set( DS1307_MTH, _month );
	RTC.set( DS1307_DAY, _day );
	RTC.set( DS1307_HR, _hours );
	RTC.set( DS1307_MIN, _minutes );
	RTC.set( DS1307_SEC, _seconds );
	RTC.start();
}


void Time::inc_hours()
{
	if( ++_hours >= 24 )
		_hours = 0;
	save_time();
}


void Time::inc_minutes()
{
	if( ++_minutes >= 60 )
		_minutes = 0;
	save_time();
}


void Time::reset_seconds()
{
	_seconds = 0;
	save_time();
}


void Time::inc_year()
{
	if( ++_year >= YEAR_MAX )
		_year = YEAR_MIN;
	save_time();
}


void Time::inc_month()
{
	if( ++_month > 12 )
		_month = 1;
}


void Time::inc_day()
{
	if( ++_day > days_in_month( _month, _year ) )
		_day = 1;
	save_time();
}


