Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate warnings when formatting naive-times with %Z or %z #211

Merged
merged 2 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

* Errors resulting from invalid dates or nonexistent/ambiguous times are now
a little nicer to read through the usage of an info bullet (#200).

* Formatting a naive-time with `%Z` or `%z` now warns that there were
format failures (#204).

* Fixed a Solaris ambiguous behavior issue from calling `pow(int, int)`.

Expand Down
21 changes: 20 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ warn_clock <- function(message, class = character()) {
# Thrown from C++
warn_clock_parse_failures <- function(n, first) {
if (n == 0) {
abort("Internal error: warning thrown with zero parse failures.")
abort("Internal error: warning thrown with zero failures.")
} else if (n == 1) {
message <- paste0(
"Failed to parse 1 string at location ", first, ". ",
Expand All @@ -224,6 +224,25 @@ warn_clock_parse_failures <- function(n, first) {
warn_clock(message, "clock_warning_parse_failures")
}

# Thrown from C++
warn_clock_format_failures <- function(n, first) {
if (n == 0) {
abort("Internal error: warning thrown with zero failures.")
} else if (n == 1) {
message <- paste0(
"Failed to format 1 string at location ", first, ". ",
"Returning `NA` at that location."
)
} else {
message <- paste0(
"Failed to format ", n, " strings, beginning at location ", first, ". ",
"Returning `NA` at the locations where there were format failures."
)
}

warn_clock(message, "clock_warning_format_failures")
}

# ------------------------------------------------------------------------------

max_collect <- function(max) {
Expand Down
73 changes: 73 additions & 0 deletions src/failure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef CLOCK_FAILURE_H
#define CLOCK_FAILURE_H

#include "clock.h"

// -----------------------------------------------------------------------------

namespace rclock {

class failures {
private:
r_ssize n_;
r_ssize first_;

public:
CONSTCD11 failures() NOEXCEPT;

void write(r_ssize i);
CONSTCD11 bool any_failures() const NOEXCEPT;

void warn_parse() const;
void warn_format() const;
};

CONSTCD11
inline
failures::failures() NOEXCEPT
: n_(0),
first_(0)
{}

inline
void
failures::write(r_ssize i) {
if (n_ == 0) {
first_ = i;
}
++n_;
}

CONSTCD11
inline
bool
failures::any_failures() const NOEXCEPT {
return n_ > 0;
}

inline
void
failures::warn_parse() const {
cpp11::writable::integers n(1);
cpp11::writable::integers first(1);
n[0] = (int) n_;
first[0] = (int) first_ + 1;
auto r_warn = cpp11::package("clock")["warn_clock_parse_failures"];
r_warn(n, first);
}

inline
void
failures::warn_format() const {
cpp11::writable::integers n(1);
cpp11::writable::integers first(1);
n[0] = (int) n_;
first[0] = (int) first_ + 1;
auto r_warn = cpp11::package("clock")["warn_clock_format_failures"];
r_warn(n, first);
}

} // namespace rclock

// -----------------------------------------------------------------------------
#endif // CLOCK_FAILURE_H
15 changes: 15 additions & 0 deletions src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "check.h"
#include "zone.h"
#include "locale.h"
#include "failure.h"
#include <sstream>
#include <locale>

Expand Down Expand Up @@ -1059,6 +1060,8 @@ cpp11::writable::strings format_time_point_impl(const ClockDuration& cd,
std::string decimal_mark_string = decimal_mark[0];
const char* decimal_mark_char = decimal_mark_string.c_str();

rclock::failures fail{};

for (r_ssize i = 0; i < size; ++i) {
if (cd.is_na(i)) {
SET_STRING_ELT(out, i, r_chr_na);
Expand All @@ -1082,6 +1085,7 @@ cpp11::writable::strings format_time_point_impl(const ClockDuration& cd,
);

if (stream.fail()) {
fail.write(i);
SET_STRING_ELT(out, i, r_chr_na);
continue;
}
Expand All @@ -1090,6 +1094,10 @@ cpp11::writable::strings format_time_point_impl(const ClockDuration& cd,
SET_STRING_ELT(out, i, Rf_mkCharLenCE(str.c_str(), str.size(), CE_UTF8));
}

if (fail.any_failures()) {
fail.warn_format();
}

return out;
}

Expand Down Expand Up @@ -1192,6 +1200,8 @@ cpp11::writable::strings format_zoned_time_impl(const ClockDuration& cd,
const std::string decimal_mark_string = decimal_mark[0];
const char* decimal_mark_char = decimal_mark_string.c_str();

rclock::failures fail{};

for (r_ssize i = 0; i < size; ++i) {
if (cd.is_na(i)) {
SET_STRING_ELT(out, i, r_chr_na);
Expand Down Expand Up @@ -1227,6 +1237,7 @@ cpp11::writable::strings format_zoned_time_impl(const ClockDuration& cd,
);

if (stream.fail()) {
fail.write(i);
SET_STRING_ELT(out, i, r_chr_na);
continue;
}
Expand All @@ -1235,6 +1246,10 @@ cpp11::writable::strings format_zoned_time_impl(const ClockDuration& cd,
SET_STRING_ELT(out, i, Rf_mkCharLenCE(str.c_str(), str.size(), CE_UTF8));
}

if (fail.any_failures()) {
fail.warn_format();
}

return out;
}

Expand Down
37 changes: 19 additions & 18 deletions src/gregorian-year-month-day.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "enums.h"
#include "get.h"
#include "parse.h"
#include "failure.h"
#include "locale.h"
#include "rcrd.h"

Expand Down Expand Up @@ -829,7 +830,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
Calendar& out) {
using Duration = typename Calendar::duration;
const r_ssize size = fmts.size();
Expand Down Expand Up @@ -863,7 +864,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand All @@ -877,7 +878,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
rclock::gregorian::y& out) {
const r_ssize size = fmts.size();

Expand All @@ -904,7 +905,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand All @@ -918,7 +919,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
rclock::gregorian::ym& out) {
const r_ssize size = fmts.size();

Expand All @@ -945,7 +946,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand All @@ -959,7 +960,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
rclock::gregorian::ymd& out) {
const r_ssize size = fmts.size();

Expand All @@ -986,7 +987,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand All @@ -1000,7 +1001,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
rclock::gregorian::ymdh& out) {
const r_ssize size = fmts.size();

Expand Down Expand Up @@ -1030,7 +1031,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand All @@ -1044,7 +1045,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
rclock::gregorian::ymdhm& out) {
const r_ssize size = fmts.size();

Expand Down Expand Up @@ -1075,7 +1076,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand All @@ -1089,7 +1090,7 @@ year_month_day_from_stream(std::istringstream& stream,
const std::pair<const std::string*, const std::string*>& ampm_names_pair,
const char& decimal_mark,
const r_ssize& i,
rclock::parse_failures& failures,
rclock::failures& fail,
rclock::gregorian::ymdhms& out) {
const r_ssize size = fmts.size();

Expand Down Expand Up @@ -1121,7 +1122,7 @@ year_month_day_from_stream(std::istringstream& stream,
}
}

failures.write(i);
fail.write(i);
out.assign_na(i);
}

Expand Down Expand Up @@ -1169,7 +1170,7 @@ year_month_day_parse_impl(const cpp11::strings& x,
ampm_names
);

rclock::parse_failures failures{};
rclock::failures fail{};

std::istringstream stream;

Expand All @@ -1195,15 +1196,15 @@ year_month_day_parse_impl(const cpp11::strings& x,
ampm_names_pair,
dmark,
i,
failures,
fail,
out
);
}

vmaxset(vmax);

if (failures.any_failures()) {
failures.warn();
if (fail.any_failures()) {
fail.warn_parse();
}

return out.to_list();
Expand Down
Loading