DetachedSong, db/LightSong, db/simple/Song: use std::chrono::system_clock::time_point

This commit is contained in:
Max Kellermann 2017-01-18 13:19:13 +01:00
parent 902fbb3347
commit b886dfae4d
13 changed files with 51 additions and 30 deletions

View File

@ -25,11 +25,10 @@
#include "Chrono.hxx"
#include "Compiler.h"
#include <chrono>
#include <string>
#include <utility>
#include <time.h>
struct LightSong;
class Storage;
class Path;
@ -63,7 +62,12 @@ class DetachedSong {
Tag tag;
time_t mtime = 0;
/**
* The time stamp of the last file modification. A negative
* value means that this is unknown/unavailable.
*/
std::chrono::system_clock::time_point mtime =
std::chrono::system_clock::time_point::min();
/**
* Start of this sub-song within the file.
@ -201,11 +205,11 @@ public:
tag.MoveItemsFrom(std::move(other.tag));
}
time_t GetLastModified() const {
std::chrono::system_clock::time_point GetLastModified() const {
return mtime;
}
void SetLastModified(time_t _value) {
void SetLastModified(std::chrono::system_clock::time_point _value) {
mtime = _value;
}

View File

@ -145,7 +145,7 @@ SongFilter::Item::Match(const DetachedSong &song) const noexcept
return uri_is_child_or_same(value.c_str(), song.GetURI());
if (tag == LOCATE_TAG_MODIFIED_SINCE)
return song.GetLastModified() >= time;
return song.GetLastModified() >= std::chrono::system_clock::from_time_t(time);
if (tag == LOCATE_TAG_FILE_TYPE)
return StringMatch(song.GetURI());
@ -162,7 +162,7 @@ SongFilter::Item::Match(const LightSong &song) const noexcept
}
if (tag == LOCATE_TAG_MODIFIED_SINCE)
return song.mtime >= time;
return song.mtime >= std::chrono::system_clock::from_time_t(time);
if (tag == LOCATE_TAG_FILE_TYPE) {
const auto uri = song.GetURI();

View File

@ -28,6 +28,7 @@
#include "TagPrint.hxx"
#include "client/Response.hxx"
#include "fs/Traits.hxx"
#include "util/ChronoUtil.hxx"
#include "util/UriUtil.hxx"
#define SONG_FILE "file: "
@ -88,7 +89,7 @@ song_print_info(Response &r, const LightSong &song, bool base)
PrintRange(r, song.start_time, song.end_time);
if (song.mtime > 0)
if (!IsNegative(song.mtime))
time_print(r, "Last-Modified", song.mtime);
tag_print(r, *song.tag);
@ -101,7 +102,7 @@ song_print_info(Response &r, const DetachedSong &song, bool base)
PrintRange(r, song.GetStartTime(), song.GetEndTime());
if (song.GetLastModified() > 0)
if (!IsNegative(song.GetLastModified()))
time_print(r, "Last-Modified", song.GetLastModified());
tag_print_values(r, song.GetTag());

View File

@ -27,6 +27,7 @@
#include "tag/ParseName.hxx"
#include "tag/Tag.hxx"
#include "tag/Builder.hxx"
#include "util/ChronoUtil.hxx"
#include "util/StringStrip.hxx"
#include "util/RuntimeError.hxx"
@ -54,7 +55,9 @@ song_save(BufferedOutputStream &os, const Song &song)
tag_save(os, song.tag);
os.Format(SONG_MTIME ": %li\n", (long)song.mtime);
if (!IsNegative(song.mtime))
os.Format(SONG_MTIME ": %li\n",
(long)std::chrono::system_clock::to_time_t(song.mtime));
os.Format(SONG_END "\n");
}
@ -67,7 +70,9 @@ song_save(BufferedOutputStream &os, const DetachedSong &song)
tag_save(os, song.GetTag());
os.Format(SONG_MTIME ": %li\n", (long)song.GetLastModified());
if (!IsNegative(song.GetLastModified()))
os.Format(SONG_MTIME ": %li\n",
(long)std::chrono::system_clock::to_time_t(song.GetLastModified()));
os.Format(SONG_END "\n");
}
@ -99,7 +104,7 @@ song_load(TextFile &file, const char *uri)
} else if (strcmp(line, "Playlist") == 0) {
tag.SetHasPlaylist(strcmp(value, "yes") == 0);
} else if (strcmp(line, SONG_MTIME) == 0) {
song->SetLastModified(atoi(value));
song->SetLastModified(std::chrono::system_clock::from_time_t(atoi(value)));
} else if (strcmp(line, "Range") == 0) {
char *endptr;

View File

@ -88,7 +88,7 @@ Song::UpdateFile(Storage &storage)
return false;
}
mtime = std::chrono::system_clock::to_time_t(info.mtime);
mtime = info.mtime;
tag_builder.Commit(tag);
return true;
}
@ -151,7 +151,7 @@ DetachedSong::LoadFile(Path path)
if (!tag_file_scan(path, tag_builder))
return false;
mtime = std::chrono::system_clock::to_time_t(fi.GetModificationTime());
mtime = fi.GetModificationTime();
tag_builder.Commit(tag);
return true;
}
@ -171,7 +171,7 @@ DetachedSong::Update()
if (!tag_stream_scan(uri.c_str(), tag_builder))
return false;
mtime = 0;
mtime = std::chrono::system_clock::time_point::min();
tag_builder.Commit(tag);
return true;
} else

View File

@ -24,8 +24,7 @@
#include "Compiler.h"
#include <string>
#include <time.h>
#include <chrono>
struct Tag;
@ -62,7 +61,11 @@ struct LightSong {
*/
const Tag *tag;
time_t mtime;
/**
* The time stamp of the last file modification. A negative
* value means that this is unknown/unavailable.
*/
std::chrono::system_clock::time_point mtime;
/**
* Start of this sub-song within the file.

View File

@ -197,7 +197,11 @@ ProxySong::ProxySong(const mpd_song *song)
uri = mpd_song_get_uri(song);
real_uri = nullptr;
tag = &tag2;
mtime = mpd_song_get_last_modified(song);
const auto _mtime = mpd_song_get_last_modified(song);
mtime = _mtime > 0
? std::chrono::system_clock::from_time_t(_mtime)
: std::chrono::system_clock::time_point::min();
#if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
start_time = SongTime::FromS(mpd_song_get_start(song));

View File

@ -29,7 +29,7 @@
#include <string.h>
inline Song::Song(const char *_uri, size_t uri_length, Directory &_parent)
:parent(&_parent), mtime(0),
:parent(&_parent), mtime(std::chrono::system_clock::time_point::min()),
start_time(SongTime::zero()), end_time(SongTime::zero())
{
memcpy(uri, _uri, uri_length + 1);

View File

@ -70,7 +70,11 @@ struct Song {
*/
Directory *const parent;
time_t mtime;
/**
* The time stamp of the last file modification. A negative
* value means that this is unknown/unavailable.
*/
std::chrono::system_clock::time_point mtime;
/**
* Start of this sub-song within the file.

View File

@ -63,7 +63,7 @@ public:
uri = uri2.c_str();
real_uri = real_uri2.c_str();
tag = &tag2;
mtime = 0;
mtime = std::chrono::system_clock::time_point::min();
start_time = end_time = SongTime::zero();
}
};
@ -314,7 +314,7 @@ visitSong(const UPnPDirObject &meta, const char *path,
song.uri = path;
song.real_uri = meta.url.c_str();
song.tag = &meta.tag;
song.mtime = 0;
song.mtime = std::chrono::system_clock::time_point::min();
song.start_time = song.end_time = SongTime::zero();
if (selection.Match(song))

View File

@ -107,7 +107,7 @@ UpdateWalk::UpdateContainerFile(Directory &directory,
*contdir);
// shouldn't be necessary but it's there..
song->mtime = std::chrono::system_clock::to_time_t(info.mtime);
song->mtime = info.mtime;
FormatDefault(update_domain, "added %s/%s",
contdir->GetPath(), song->uri);

View File

@ -51,9 +51,7 @@ UpdateWalk::UpdateSongFile2(Directory &directory,
return;
}
if (!(song != nullptr &&
std::chrono::system_clock::to_time_t(info.mtime) == song->mtime &&
!walk_discard) &&
if (!(song != nullptr && info.mtime == song->mtime && !walk_discard) &&
UpdateContainerFile(directory, name, suffix, info)) {
if (song != nullptr)
editor.LockDeleteSong(directory, song);
@ -80,7 +78,7 @@ UpdateWalk::UpdateSongFile2(Directory &directory,
modified = true;
FormatDefault(update_domain, "added %s/%s",
directory.GetPath(), name);
} else if (std::chrono::system_clock::to_time_t(info.mtime) != song->mtime || walk_discard) {
} else if (info.mtime != song->mtime || walk_discard) {
FormatDefault(update_domain, "updating %s/%s",
directory.GetPath(), name);
if (!song->UpdateFile(storage)) {

View File

@ -16,6 +16,7 @@
#include "db/DatabaseSong.hxx"
#include "storage/plugins/LocalStorage.hxx"
#include "Mapper.hxx"
#include "util/ChronoUtil.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
@ -179,8 +180,9 @@ ToString(const DetachedSong &song)
char buffer[64];
if (song.GetLastModified() > 0) {
sprintf(buffer, "%lu", (unsigned long)song.GetLastModified());
if (!IsNegative(song.GetLastModified())) {
sprintf(buffer, "%lu",
(unsigned long)std::chrono::system_clock::to_time_t(song.GetLastModified()));
result.append(buffer);
}