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

3509 not utc for dates #3698

Merged
merged 2 commits into from
May 31, 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
29 changes: 19 additions & 10 deletions app/api/csv/specs/csvExporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isArray } from 'util';
import templates from 'api/templates';
import translations from 'api/i18n/translations';
import * as translate from 'shared/translate';
import moment from 'moment-timezone';
import CSVExporter, {
getTypes,
getTemplatesModels,
Expand Down Expand Up @@ -222,17 +223,25 @@ describe('csvExporter', () => {
expect(formatted).toBe(testTemplates['58ad7d240d44252fee4e61fd'].name);
});

it('should return the creation date', () => {
spyOn(formatters, 'formatCreationDate').and.returnValue('creationDateValue');
const options = {};
const formatted = processCommonField(
'creationDate',
searchResults.rows[0],
testTemplates['58ad7d240d44252fee4e61fd'],
options
describe('creationDate', () => {
it.each([
{ timezone: 'Europe/Madrid', timestamp: 0, expectedDate: '1970-01-01' },
{ timezone: 'Pacific/Honolulu', timestamp: 0, expectedDate: '1969-12-31' },
])(
'should be formated using local time %p',
async ({ timezone, timestamp, expectedDate }) => {
const options = {};
moment.tz.setDefault(timezone);
const creationDate = processCommonField(
'creationDate',
{ ...searchResults.rows[0], creationDate: timestamp },
testTemplates['58ad7d240d44252fee4e61fd'],
options
);
expect(creationDate).toBe(expectedDate);
moment.tz.setDefault();
}
);
expect(formatted).toBe('creationDateValue');
expect(formatters.formatCreationDate).toHaveBeenCalledWith(searchResults.rows[0], options);
});

it('should return the geolocation field processed', () => {
Expand Down
2 changes: 1 addition & 1 deletion app/api/csv/specs/typeFormatters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import moment from 'moment';
import moment from 'moment-timezone';
import {
formatters as typeFormatters,
formatFile,
Expand Down
4 changes: 2 additions & 2 deletions app/api/csv/typeFormatters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment';
import moment from 'moment-timezone';
import { MetadataObjectSchema } from 'shared/types/commonTypes';

const defaultDateFormat = 'YYYY-MM-DD';
Expand Down Expand Up @@ -60,4 +60,4 @@ export const formatDocuments = (row: any) =>
export const formatAttachments = (row: any) =>
(row.attachments || []).map((item: any) => formatAttachment(item.filename, row._id)).join('|');
export const formatCreationDate = (row: any, options: FormatterOptions) =>
moment.utc(row.creationDate).format(mapFormatToMoment(options.dateFormat || defaultDateFormat));
moment(row.creationDate).format(mapFormatToMoment(options.dateFormat || defaultDateFormat));
13 changes: 9 additions & 4 deletions app/react/Forms/components/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import 'react-datepicker/dist/react-datepicker.css';
import DatePickerComponent from 'react-datepicker';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import moment from 'moment';
import moment from 'moment-timezone';

const removeOffset = (useTimezone, value) => {
let datePickerValue = null;
const miliseconds = value * 1000;
if (value) {
const newValue = moment.utc(value * 1000);
const newValue = moment.utc(miliseconds);

if (!useTimezone) {
newValue.subtract(moment().utcOffset(), 'minute');
// in order to get the system offset for the specific date we
// need to create a new not UTC moment object with the original timestamp
newValue.subtract(moment(moment(miliseconds)).utcOffset(), 'minutes');
}

datePickerValue = parseInt(newValue.locale('en').format('x'), 10);
Expand All @@ -25,7 +28,9 @@ const addOffset = (useTimezone, endOfDay, value) => {
const newValue = moment.utc(value);

if (!useTimezone) {
newValue.add(moment().utcOffset(), 'minute');
// in order to get the proper offset moment has to be initialized with the actual date
// without this you always get the "now" moment offset
newValue.add(moment(value).utcOffset(), 'minutes');
}

if (endOfDay) {
Expand Down
43 changes: 35 additions & 8 deletions app/react/Forms/components/specs/DatePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ describe('DatePicker', () => {
input = component.find(DatePickerComponent);
};

const expectCorrectSelectedValue = () => {
it('should render a DatePickerComponent with the correct date transformed to local value', () => {
render();
const expectedSelectedValue = date.clone().subtract(moment().utcOffset(), 'minute');
expect(input.props().selected).toBe(parseInt(expectedSelectedValue.format('x'), 10));
};

it('should render a DatePickerComponent with the correct transformed to local value', () => {
expectCorrectSelectedValue();
expect(input.props().selected).toBe(parseInt(moment('2016-07-28').format('x'), 10));
});

describe('when useTimezone is true', () => {
Expand All @@ -62,6 +57,37 @@ describe('DatePicker', () => {
expectCorrectOnChange();
});

afterEach(() => {
moment.tz.setDefault();
});

describe('when date is in a diferent timezone than today', () => {
it.each([
{ timezone: 'Japan', dateToTest: '1950-08-05' },
{ timezone: 'Europe/Madrid', dateToTest: '1973-08-18' },
])('should use the timestamp offsetting to UTC %s', ({ timezone, dateToTest }) => {
moment.tz.setDefault(timezone);
const newDate = moment.utc(dateToTest);
props.value = Number(newDate.format('X'));

render();
expect(input.props().selected).toBe(parseInt(moment(dateToTest).format('x'), 10));
});

it.each([
{ timezone: 'Japan', dateToTest: '1950-08-05' },
{ timezone: 'Europe/Madrid', dateToTest: '1973-08-18' },
])('should set the value to timestamp offsetting to UTC %s', ({ timezone, dateToTest }) => {
moment.tz.setDefault(timezone);
const newDate = moment(dateToTest).toDate();
render();
input.simulate('change', newDate);
expect(props.onChange).toHaveBeenCalledWith(
parseInt(moment.utc(dateToTest).format('X'), 10)
);
});
});

describe('When locale is a non-latin locale', () => {
let originalLocale;

Expand All @@ -75,7 +101,8 @@ describe('DatePicker', () => {
});

it('should render a latin-based value (until correct locales are implemented)', () => {
expectCorrectSelectedValue();
render();
expect(input.props().selected).toBe(parseInt(moment('2016-07-28').format('x'), 10));
});

it('should not fail on change', () => {
Expand Down
6 changes: 3 additions & 3 deletions app/react/Metadata/helpers/formater.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import moment from 'moment';
import moment from 'moment-timezone';
import Immutable from 'immutable';
import { advancedSort } from 'app/utils/advancedSort';
import { store } from 'app/store';
Expand Down Expand Up @@ -38,7 +38,7 @@ const formatMetadataSortedProperty = (metadata, sortedProperty) =>

const addCreationDate = (result, doc) =>
result.push({
value: moment.utc(doc.creationDate).format('ll'),
value: moment(doc.creationDate).format('ll'),
label: 'Date added',
name: 'creationDate',
translateContext: 'System',
Expand All @@ -47,7 +47,7 @@ const addCreationDate = (result, doc) =>

const addModificationDate = (result, doc) =>
result.push({
value: moment.utc(doc.editDate).format('ll'),
value: moment(doc.editDate).format('ll'),
label: 'Date modified',
name: 'editDate',
translateContext: 'System',
Expand Down
38 changes: 31 additions & 7 deletions app/react/Metadata/helpers/specs/formater.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-statements */

import Immutable from 'immutable';
import moment from 'moment-timezone';

import { metadataSelectors } from '../../selectors';

Expand Down Expand Up @@ -368,6 +369,36 @@ describe('metadata formater', () => {
});
});

describe('creationDate', () => {
it('should be formated using local time', async () => {
moment.tz.setDefault('Europe/Madrid');
let formated = prepareMetadata('creationDate');
let formatedCreationDate = formated[formated.length - 1];
expect(formatedCreationDate.value).toBe('Jan 1, 1970');

moment.tz.setDefault('Pacific/Honolulu');
formated = prepareMetadata('creationDate');
formatedCreationDate = formated[formated.length - 1];
expect(formatedCreationDate.value).toBe('Dec 31, 1969');
moment.tz.setDefault();
});
});

describe('editDate', () => {
it('should be formated using local time', async () => {
moment.tz.setDefault('Europe/Madrid');
let formated = prepareMetadata('editDate');
let formatedEditDate = formated[formated.length - 1];
expect(formatedEditDate.value).toBe('Jan 1, 1970');

moment.tz.setDefault('Pacific/Honolulu');
formated = prepareMetadata('editDate');
formatedEditDate = formated[formated.length - 1];
expect(formatedEditDate.value).toBe('Dec 31, 1969');
moment.tz.setDefault();
});
});

describe('when sort property is creationDate', () => {
it('should add it as a value to show', () => {
[
Expand All @@ -382,12 +413,6 @@ describe('metadata formater', () => {
] = prepareMetadata('creationDate');
expect(text.sortedBy).toBe(false);
expect(markdown.sortedBy).toBe(false);
assessBasicProperties(creationDate, [
'Date added',
'creationDate',
'System',
'Jan 1, 1970',
]);
expect(creationDate.sortedBy).toBe(true);
});
});
Expand All @@ -399,7 +424,6 @@ describe('metadata formater', () => {
);
expect(text.sortedBy).toBe(false);
expect(markdown.sortedBy).toBe(false);
assessBasicProperties(editDate, ['Date modified', 'editDate', 'System', 'Jan 1, 1970']);
expect(editDate.sortedBy).toBe(true);
});
});
Expand Down