SongFilter: use std::chrono::system_clock::time_point instead of time_t

This commit is contained in:
Max Kellermann 2017-01-18 13:03:05 +01:00
parent b886dfae4d
commit b2b079a26b
2 changed files with 15 additions and 14 deletions

View File

@ -22,6 +22,7 @@
#include "db/LightSong.hxx" #include "db/LightSong.hxx"
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "tag/ParseName.hxx" #include "tag/ParseName.hxx"
#include "util/ChronoUtil.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/StringAPI.hxx" #include "util/StringAPI.hxx"
#include "util/ASCII.hxx" #include "util/ASCII.hxx"
@ -71,7 +72,8 @@ SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
{ {
} }
SongFilter::Item::Item(unsigned _tag, time_t _time) SongFilter::Item::Item(unsigned _tag,
std::chrono::system_clock::time_point _time)
:tag(_tag), value(nullptr), time(_time) :tag(_tag), value(nullptr), time(_time)
{ {
} }
@ -145,7 +147,7 @@ SongFilter::Item::Match(const DetachedSong &song) const noexcept
return uri_is_child_or_same(value.c_str(), song.GetURI()); return uri_is_child_or_same(value.c_str(), song.GetURI());
if (tag == LOCATE_TAG_MODIFIED_SINCE) if (tag == LOCATE_TAG_MODIFIED_SINCE)
return song.GetLastModified() >= std::chrono::system_clock::from_time_t(time); return song.GetLastModified() >= time;
if (tag == LOCATE_TAG_FILE_TYPE) if (tag == LOCATE_TAG_FILE_TYPE)
return StringMatch(song.GetURI()); return StringMatch(song.GetURI());
@ -162,7 +164,7 @@ SongFilter::Item::Match(const LightSong &song) const noexcept
} }
if (tag == LOCATE_TAG_MODIFIED_SINCE) if (tag == LOCATE_TAG_MODIFIED_SINCE)
return song.mtime >= std::chrono::system_clock::from_time_t(time); return song.mtime >= time;
if (tag == LOCATE_TAG_FILE_TYPE) { if (tag == LOCATE_TAG_FILE_TYPE) {
const auto uri = song.GetURI(); const auto uri = song.GetURI();
@ -183,8 +185,8 @@ SongFilter::~SongFilter()
} }
gcc_pure gcc_pure
static time_t static std::chrono::system_clock::time_point
ParseTimeStamp(const char *s) noexcept ParseTimeStamp(const char *s)
{ {
assert(s != nullptr); assert(s != nullptr);
@ -192,14 +194,13 @@ ParseTimeStamp(const char *s) noexcept
unsigned long long value = strtoull(s, &endptr, 10); unsigned long long value = strtoull(s, &endptr, 10);
if (*endptr == 0 && endptr > s) if (*endptr == 0 && endptr > s)
/* it's an integral UNIX time stamp */ /* it's an integral UNIX time stamp */
return (time_t)value; return std::chrono::system_clock::from_time_t((time_t)value);
try { try {
/* try ISO 8601 */ /* try ISO 8601 */
const auto t = ParseTimePoint(s, "%FT%TZ"); return ParseTimePoint(s, "%FT%TZ");
return std::chrono::system_clock::to_time_t(t);
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {
return 0; return std::chrono::system_clock::time_point::min();
} }
} }
@ -219,8 +220,8 @@ SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
} }
if (tag == LOCATE_TAG_MODIFIED_SINCE) { if (tag == LOCATE_TAG_MODIFIED_SINCE) {
time_t t = ParseTimeStamp(value); const auto t = ParseTimeStamp(value);
if (t == 0) if (IsNegative(t))
return false; return false;
items.push_back(Item(tag, t)); items.push_back(Item(tag, t));

View File

@ -24,9 +24,9 @@
#include "Compiler.h" #include "Compiler.h"
#include <list> #include <list>
#include <chrono>
#include <stdint.h> #include <stdint.h>
#include <time.h>
/** /**
* Limit the search to files within the given directory. * Limit the search to files within the given directory.
@ -55,12 +55,12 @@ public:
/** /**
* For #LOCATE_TAG_MODIFIED_SINCE * For #LOCATE_TAG_MODIFIED_SINCE
*/ */
time_t time; std::chrono::system_clock::time_point time;
public: public:
gcc_nonnull(3) gcc_nonnull(3)
Item(unsigned tag, const char *value, bool fold_case=false); Item(unsigned tag, const char *value, bool fold_case=false);
Item(unsigned tag, time_t time); Item(unsigned tag, std::chrono::system_clock::time_point time);
Item(const Item &other) = delete; Item(const Item &other) = delete;
Item(Item &&) = default; Item(Item &&) = default;