1 /*
2  * Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "utils/datetime.h"
21 
22 #include <gtk/gtk.h>
23 
24 #include <time.h>
25 
26 /**
27  * Returns the current datetime as a string.
28  *
29  * Must be free()'d by caller.
30  */
31 char *
datetime_get_current_as_string()32 datetime_get_current_as_string ()
33 {
34 
35   time_t t = time (NULL);
36 #ifdef HAVE_LOCALTIME_R
37   struct tm tm;
38   struct tm * ret = localtime_r (&t, &tm);
39   g_return_val_if_fail (ret, NULL);
40 #else
41   struct tm tm = *localtime (&t);
42 #endif
43 
44   char * str =
45     g_strdup_printf (
46       "%d-%02d-%02d %02d:%02d:%02d",
47       tm.tm_year + 1900,
48       tm.tm_mon + 1,
49       tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
50 
51   return str;
52 }
53 
54 /**
55  * Get the current datetime to be used in filenames,
56  * eg, for the log file.
57  */
58 char *
datetime_get_for_filename(void)59 datetime_get_for_filename (void)
60 {
61   GDateTime * datetime =
62     g_date_time_new_now_local ();
63   char * str_datetime =
64     g_date_time_format (
65       datetime,
66       "%F_%H-%M-%S");
67   g_date_time_unref (datetime);
68 
69   return str_datetime;
70 }
71