time_print: new library, function time_print()

Implements error checking for the gmtime_r() call, which was missing
in two code locations.
This commit is contained in:
Max Kellermann 2012-08-08 09:15:34 +02:00
parent 63a2ac21e1
commit 21792386d8
6 changed files with 88 additions and 57 deletions

View File

@ -186,6 +186,7 @@ mpd_headers = \
src/replay_gain_info.h \
src/replay_gain_ape.h \
src/sig_handlers.h \
src/time_print.c src/time_print.h \
src/song.h \
src/song_print.h \
src/song_save.h \

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);

47
src/time_print.c Normal file
View File

@ -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 <glib.h>
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);
}

33
src/time_print.h Normal file
View File

@ -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 <time.h>
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