Skip to content

Commit

Permalink
fix #898
Browse files Browse the repository at this point in the history
  • Loading branch information
sc1f committed Feb 3, 2020
1 parent b6fff8f commit acd0229
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
13 changes: 3 additions & 10 deletions cpp/perspective/src/cpp/computed_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,25 +554,18 @@ t_tscalar week_bucket<DTYPE_TIME>(t_tscalar x) {
// Create a copy of the timestamp with day precision
auto days = date::floor<date::days>(ts);

// Cast the `time_point` to contain year/month/day
auto ymd = date::year_month_day(days);
// Subtract Sunday from the ymd to get the beginning of the last day
date::year_month_day ymd = days - (date::weekday{days} - date::Monday);

// Get the day of month and day of the week
std::int32_t year = static_cast<std::int32_t>(ymd.year());

// date::month is [1-12], whereas `t_date.month()` is [0-11]
std::uint32_t month = static_cast<std::uint32_t>(ymd.month()) - 1;
std::uint32_t day = static_cast<std::uint32_t>(ymd.day());
auto weekday = date::year_month_weekday(days).weekday_indexed().weekday();

// Get the day of the week as an int from 0 to 6
auto day_of_week = (weekday - date::Sunday).count();

// Set the day to the beginning of the week
auto diff = day - day_of_week + (day_of_week == 0 ? - 6 : 1);

// Return the new `t_date`
t_date new_date = t_date(year, month, diff);
t_date new_date = t_date(year, month, day);
rval.set(new_date);
return rval;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ export const install = function(grid) {

const filters = config.filter.concat(row_filters).concat(column_filters);

// this only works for single row select (which is all we support right now)
// this only works for single row select (which is all we support
// right now)
if (this.grid.properties.rowSelection) {
const selected = this.grid.getSelectedRows()[0] === y;
this.grid.canvas.dispatchEvent(
Expand Down
33 changes: 33 additions & 0 deletions packages/perspective/test/js/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,39 @@ module.exports = perspective => {
table.delete();
});

it("Bucket (W), datetime shouldn't ever overflow at beginning of year", async function() {
const table = perspective.table({
a: "datetime"
});

const table2 = table.add_computed([
{
column: "bucket",
computed_function_name: "Bucket (W)",
inputs: ["a"]
}
]);

let view = table2.view();

const schema = await table2.schema();
expect(schema).toEqual({
a: "datetime",
bucket: "date"
});

table2.update({
a: [new Date(2015, 0, 3, 15), new Date(2015, 0, 4)]
});

let result = await view.to_columns();

expect(result.bucket.map(x => new Date(x))).toEqual(result.a.map(x => week_bucket(x)));
view.delete();
table2.delete();
table.delete();
});

it("Bucket (M), datetime", async function() {
const table = perspective.table({
a: "datetime"
Expand Down

0 comments on commit acd0229

Please sign in to comment.