This repository has been archived on 2024-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
hedera/gvn/gvn-time.c

98 lines
2.7 KiB
C
Raw Normal View History

2013-10-11 23:07:35 +00:00
/*
* Copyright (C) 2012 - Juan Ferrer Toribio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gvn-time.h"
/**
* SECTION: gvn-time
* @Short_description: a boxed type to represent time with optional time-zone
* @Title: GvnTime
*
* #GvnTime is a boxed type to manage time of a day without specifying the date,
* just hours, minutes, seconds and the time-zone.
**/
G_DEFINE_BOXED_TYPE (GvnTime, gvn_time, gvn_time_copy, gvn_time_free);
/**
* gvn_time_copy:
* @obj: a #GvnTime
*
* Copies @obj to a new #GvnTime.
*
* Return value: a new #GvnTime
**/
GvnTime * gvn_time_copy (GvnTime * obj)
{
g_return_val_if_fail (obj, NULL);
return gvn_time_new (obj->hour, obj->min, obj->sec, obj->tz);
}
/**
* gvn_time_free:
* @obj: a #GvnTime
*
* Releases the memory used by @obj.
**/
void gvn_time_free (GvnTime * obj)
{
g_return_if_fail (obj);
g_free (obj);
}
/**
* gvn_time_new:
* @h: the hour, from 0 to 23
* @m: the minutes, from 0 to 59
* @s: the seconds, from 0 to 59
* @tz: the time-zone, from -12 to 12
*
* Creates a new #GvnTime with the given parameters, if any parameter is out of
* range, that parameter will be put to 0 instead.
**/
GvnTime * gvn_time_new (guint h, guint m, guint s, gint tz)
{
GvnTime * time = g_new (GvnTime, 1);
time->hour = h > 24 ? 0 : h;
time->min = m > 59 ? 0 : m;
time->sec = s > 59 ? 0 : s;
time->tz = tz < -12 || tz > 12 ? 0 : tz;
return time;
}
/**
* gvn_time_to_string:
* @obj: a #GvnTime
* @tz: whether or not the time-zone will be used
*
* Returns a newly allocated string containing the time set by @obj. By passing
* %FALSE in @tz, the time-zone is omited from the string. The format is
* hh:mm:ss±tz if @tz is %TRUE and hh:mm:ss otherwise. The string must be freed.
*
* Return value: a string containing the time
**/
gchar * gvn_time_to_string (GvnTime * obj, gboolean tz)
{
g_return_val_if_fail (obj, NULL);
if (tz)
return g_strdup_printf ("%d:%d:%d%s%d", obj->hour, obj->min, obj->sec,
obj->tz > 0 ? "+" : "-", obj->tz < 0 ? obj->tz * -1 : obj->tz);
else
return g_strdup_printf ("%d:%d:%d", obj->hour, obj->min, obj->sec);
}