From 21792386d835903d2f9e2d04a1860d60bb8488e2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 8 Aug 2012 09:15:34 +0200 Subject: time_print: new library, function time_print() Implements error checking for the gmtime_r() call, which was missing in two code locations. --- src/DatabasePrint.cxx | 17 ++--------------- src/command.c | 18 ++---------------- src/song_print.c | 29 +++-------------------------- src/time_print.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/time_print.h | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 src/time_print.c create mode 100644 src/time_print.h (limited to 'src') diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx index 174ce1b70..843d218cb 100644 --- a/src/DatabasePrint.cxx +++ b/src/DatabasePrint.cxx @@ -27,6 +27,7 @@ extern "C" { #include "client.h" #include "song.h" #include "song_print.h" +#include "time_print.h" #include "playlist_vector.h" #include "tag.h" } @@ -105,21 +106,7 @@ PrintPlaylistFull(struct client *client, { print_playlist_in_directory(client, directory, playlist.name); -#ifndef G_OS_WIN32 - struct tm tm; -#endif - char timestamp[32]; - time_t t = playlist.mtime; - strftime(timestamp, sizeof(timestamp), -#ifdef G_OS_WIN32 - "%Y-%m-%dT%H:%M:%SZ", - gmtime(&t) -#else - "%FT%TZ", - gmtime_r(&t, &tm) -#endif - ); - client_printf(client, "Last-Modified: %s\n", timestamp); + time_print(client, "Last-Modified", playlist.mtime); return true; } diff --git a/src/command.c b/src/command.c index adda734c9..0856589b0 100644 --- a/src/command.c +++ b/src/command.c @@ -30,6 +30,7 @@ #include "playlist_queue.h" #include "playlist_error.h" #include "queue_print.h" +#include "time_print.h" #include "ls.h" #include "uri.h" #include "decoder_print.h" @@ -115,25 +116,10 @@ print_spl_list(struct client *client, GPtrArray *list) for (unsigned i = 0; i < list->len; ++i) { struct stored_playlist_info *playlist = g_ptr_array_index(list, i); - time_t t; -#ifndef WIN32 - struct tm tm; -#endif - char timestamp[32]; client_printf(client, "playlist: %s\n", playlist->name); - t = playlist->mtime; - strftime(timestamp, sizeof(timestamp), -#ifdef G_OS_WIN32 - "%Y-%m-%dT%H:%M:%SZ", - gmtime(&t) -#else - "%FT%TZ", - gmtime_r(&t, &tm) -#endif - ); - client_printf(client, "Last-Modified: %s\n", timestamp); + time_print(client, "Last-Modified", playlist->mtime); } } diff --git a/src/song_print.c b/src/song_print.c index fb608a8b2..d876b85a8 100644 --- a/src/song_print.c +++ b/src/song_print.c @@ -19,6 +19,7 @@ #include "config.h" #include "song_print.h" +#include "time_print.h" #include "song.h" #include "directory.h" #include "tag_print.h" @@ -63,32 +64,8 @@ song_print_info(struct client *client, struct song *song) song->start_ms / 1000, song->start_ms % 1000); - if (song->mtime > 0) { -#ifndef G_OS_WIN32 - struct tm tm; -#endif - const struct tm *tm2; - -#ifdef G_OS_WIN32 - tm2 = gmtime(&song->mtime); -#else - tm2 = gmtime_r(&song->mtime, &tm); -#endif - - if (tm2 != NULL) { - char timestamp[32]; - - strftime(timestamp, sizeof(timestamp), -#ifdef G_OS_WIN32 - "%Y-%m-%dT%H:%M:%SZ", -#else - "%FT%TZ", -#endif - tm2); - client_printf(client, "Last-Modified: %s\n", - timestamp); - } - } + if (song->mtime > 0) + time_print(client, "Last-Modified", song->mtime); if (song->tag) tag_print(client, song->tag); diff --git a/src/time_print.c b/src/time_print.c new file mode 100644 index 000000000..6fb569d96 --- /dev/null +++ b/src/time_print.c @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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 2 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "time_print.h" +#include "client.h" + +#include + +void +time_print(struct client *client, const char *name, time_t t) +{ +#ifdef G_OS_WIN32 + const struct tm *tm2 = gmtime(&t); +#else + struct tm tm; + const struct tm *tm2 = gmtime_r(&t, &tm); +#endif + if (tm2 == NULL) + return; + + char buffer[32]; + strftime(buffer, sizeof(buffer), +#ifdef G_OS_WIN32 + "%Y-%m-%dT%H:%M:%SZ", +#else + "%FT%TZ", +#endif + tm2); + client_printf(client, "%s: %s\n", name, buffer); +} diff --git a/src/time_print.h b/src/time_print.h new file mode 100644 index 000000000..7eff446b2 --- /dev/null +++ b/src/time_print.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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 2 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_TIME_PRINT_H +#define MPD_TIME_PRINT_H + +#include + +struct client; + +/** + * Write a line with a time stamp to the client. + */ +void +time_print(struct client *client, const char *name, time_t t); + +#endif -- cgit v1.2.3