utils: Add GDateTime utils
This will greatly help with the transition to GDateTime. In the future, we can propose these APIs to GLib itself.
This commit is contained in:
@@ -20,6 +20,7 @@ gcal_deps = [
|
||||
]
|
||||
|
||||
sources = files(
|
||||
'utils/gcal-date-time-utils.c',
|
||||
'utils/gcal-utils.c',
|
||||
'views/gcal-month-cell.c',
|
||||
'views/gcal-month-popover.c',
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
/* gcal-date-time-utils.c
|
||||
*
|
||||
* Copyright 2019 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "gcal-date-time-utils.h"
|
||||
#include "gcal-utils.h"
|
||||
|
||||
/**
|
||||
* gcal_set_datetime:
|
||||
* @dest: location to a #GDateTime pointer
|
||||
* @src: (nullable): a #GDateTime
|
||||
*
|
||||
* Performs the same of g_set_object(), but with #GDateTime
|
||||
* instances.
|
||||
*
|
||||
* Returns: %TRUE if the value of @dest changed, %FALSE otherwise
|
||||
*/
|
||||
gboolean
|
||||
gcal_set_date_time (GDateTime **dest,
|
||||
GDateTime *src)
|
||||
{
|
||||
gboolean changed = *dest != src;
|
||||
|
||||
gcal_clear_datetime (dest);
|
||||
|
||||
if (src)
|
||||
*dest = g_date_time_ref (src);
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* gcal_date_time_get_days_in_month:
|
||||
* @date: a #GDateTime
|
||||
*
|
||||
* Retrieves the number of days in the month and year
|
||||
* represented by @date.
|
||||
*
|
||||
* Returns: number of days in month during the year of @date
|
||||
*/
|
||||
guint8
|
||||
gcal_date_time_get_days_in_month (GDateTime *date)
|
||||
{
|
||||
return g_date_get_days_in_month (g_date_time_get_month (date),
|
||||
g_date_time_get_year (date));
|
||||
}
|
||||
|
||||
/**
|
||||
* gcal_date_time_get_start_of_week:
|
||||
* @date: a #GDateTime
|
||||
*
|
||||
* Retrieves the first day of the week @date is in, at 00:00
|
||||
* of the local timezone.
|
||||
*
|
||||
* This date is inclusive.
|
||||
*
|
||||
* Returns: (transfer full): a #GDateTime
|
||||
*/
|
||||
GDateTime*
|
||||
gcal_date_time_get_start_of_week (GDateTime *date)
|
||||
{
|
||||
g_autoptr (GDateTime) start_of_week = NULL;
|
||||
gint n_days_after_week_start;
|
||||
gint first_weekday;
|
||||
gint weekday;
|
||||
|
||||
g_assert (date != NULL);
|
||||
|
||||
first_weekday = get_first_weekday ();
|
||||
weekday = g_date_time_get_day_of_week (date) - 1;
|
||||
n_days_after_week_start = (weekday - first_weekday) % 7;
|
||||
|
||||
start_of_week = g_date_time_add_days (date, -n_days_after_week_start);
|
||||
|
||||
return g_date_time_new_local (g_date_time_get_year (start_of_week),
|
||||
g_date_time_get_month (start_of_week),
|
||||
g_date_time_get_day_of_month (start_of_week),
|
||||
0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* gcal_date_time_get_end_of_week:
|
||||
* @date: a #GDateTime
|
||||
*
|
||||
* Retrieves the last day of the week @date is in, at 23:59:59
|
||||
* of the local timezone.
|
||||
*
|
||||
* Because this date is exclusive, it actually is start of the
|
||||
* next week.
|
||||
*
|
||||
* Returns: (transfer full): a #GDateTime
|
||||
*/
|
||||
GDateTime*
|
||||
gcal_date_time_get_end_of_week (GDateTime *date)
|
||||
{
|
||||
g_autoptr (GDateTime) week_start = NULL;
|
||||
|
||||
week_start = gcal_date_time_get_start_of_week (date);
|
||||
return g_date_time_add_weeks (week_start, 1);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/* gcal-date-time-utils.h
|
||||
*
|
||||
* Copyright 2019 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
/**
|
||||
* gcal_clear_datetime:
|
||||
* @dt: location of a #GDateTime pointer
|
||||
*
|
||||
* Unreferences @dt if not %NULL, and set it to %NULL.
|
||||
*/
|
||||
#define gcal_clear_datetime(dt) g_clear_pointer (dt, g_date_time_unref)
|
||||
|
||||
gboolean gcal_set_date_time (GDateTime **dest,
|
||||
GDateTime *src);
|
||||
|
||||
guint8 gcal_date_time_get_days_in_month (GDateTime *date);
|
||||
|
||||
GDateTime* gcal_date_time_get_start_of_week (GDateTime *date);
|
||||
|
||||
GDateTime* gcal_date_time_get_end_of_week (GDateTime *date);
|
||||
|
||||
G_END_DECLS
|
||||
@@ -19,7 +19,9 @@
|
||||
#ifndef __GCAL_UTILS_H__
|
||||
#define __GCAL_UTILS_H__
|
||||
|
||||
#include "gcal-date-time-utils.h"
|
||||
#include "gcal-manager.h"
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <libecal/libecal.h>
|
||||
#include <libgweather/gweather.h>
|
||||
@@ -31,7 +33,6 @@
|
||||
#define MINUTES_PER_DAY 1440
|
||||
#define MAX_MINUTES (7 * MINUTES_PER_DAY)
|
||||
|
||||
#define gcal_clear_datetime(dt) g_clear_pointer (dt, g_date_time_unref)
|
||||
#define gcal_clear_timeout(pp) { if (pp && *pp) { g_source_remove (*pp); *pp = 0; } }
|
||||
|
||||
#if !EDS_CHECK_VERSION (3, 31, 90)
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "gcal-utils.h"
|
||||
#include "utils/gcal-utils.h"
|
||||
#include "gcal-event.h"
|
||||
|
||||
#define STUB_EVENT "BEGIN:VEVENT\n" \
|
||||
|
||||
Reference in New Issue
Block a user