/*
 * Copyright (C) Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the AUTHORS file distributed with this source
 * distribution
 *
 * This library 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 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "birbdatetime.h"

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
birb_date_time_set(GDateTime **datetime_ptr, GDateTime *new_datetime) {
	GDateTime *old = NULL;

	g_return_val_if_fail(datetime_ptr != NULL, FALSE);

	old = *datetime_ptr;

	if(birb_date_time_equal(old, new_datetime)) {
		return FALSE;
	}

	if(old != NULL) {
		g_date_time_unref(old);
	}

	if(new_datetime != NULL) {
		*datetime_ptr = g_date_time_ref(new_datetime);
	} else {
		*datetime_ptr = NULL;
	}

	return TRUE;
}

void
birb_date_time_clear(GDateTime **datetime_ptr) {
	if(datetime_ptr == NULL || *datetime_ptr == NULL) {
		return;
	}

	g_date_time_unref(*datetime_ptr);
	*datetime_ptr = NULL;
}

int
birb_date_time_compare(GDateTime *dt1, GDateTime *dt2) {
	if(dt1 != NULL && dt2 != NULL) {
		return g_date_time_compare(dt1, dt2);
	}

	if(dt1 == dt2) {
		return 0;
	}

	if(dt1 == NULL) {
		return -1;
	} else {
		return 1;
	}
}

gboolean
birb_date_time_equal(GDateTime *dt1, GDateTime *dt2) {
	return birb_date_time_compare(dt1, dt2) == 0;
}
