Skip to content

Commit

Permalink
Fix time formatting on transactions (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickycodes authored Jul 9, 2021
1 parent 60380bb commit ef1a953
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 34 deletions.
22 changes: 10 additions & 12 deletions app/util/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ export function toLocaleDateTime(timestamp) {
}

export function toDateFormat(timestamp) {
const dateObj = new Date(timestamp);
const month = strings(`date.months.${dateObj.getMonth()}`);
const day = dateObj.getDate();
let meridiem = 'am';
let hour = dateObj.getHours();
if (hour > 12) {
meridiem = 'pm';
hour -= 12;
}
let min = dateObj.getMinutes();
if (`${min}`.length === 1) min = `0${min}`;
return `${month} ${day} ${strings('date.connector')} ${hour}:${min}${meridiem}`;
const date = new Date(timestamp);
const month = strings(`date.months.${date.getMonth()}`);
const day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
const ampm = hours >= 12 ? 'pm' : 'am';
hours %= 12;
hours = hours || 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
return `${month} ${day} ${strings('date.connector')} ${hours}:${minutes} ${ampm}`;
}

export function toLocaleDate(timestamp) {
Expand Down
31 changes: 31 additions & 0 deletions app/util/date.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { toDateFormat } from './date';

const TZ = 'America/Toronto';

describe('Date', () => {
describe('toDateFormat', () => {
it('should be America/Toronto timeZone', () => {
// we're explicitly setting TZ in `jest.config.js`
// this test is to verify that
expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(TZ);
});
it('should format date correctly', () => {
// if TZ is not 'America/Toronto' the following test cases will fail
expect(toDateFormat(1592877684000)).toBe('Jun 22 at 10:01 pm');
expect(toDateFormat(1592877340000)).toBe('Jun 22 at 9:55 pm');
expect(toDateFormat(1592850067000)).toBe('Jun 22 at 2:21 pm');
expect(toDateFormat(1615308615000)).toBe('Mar 9 at 11:50 am');
expect(toDateFormat(1615308108000)).toBe('Mar 9 at 11:41 am');
// this was previously rendering as 0:28 pm:
expect(toDateFormat(1615912139000)).toBe('Mar 16 at 12:28 pm');
expect(toDateFormat(1592883929000)).toBe('Jun 22 at 11:45 pm');
expect(toDateFormat(1592883518000)).toBe('Jun 22 at 11:38 pm');
expect(toDateFormat(1592882817000)).toBe('Jun 22 at 11:26 pm');
expect(toDateFormat(1592881746000)).toBe('Jun 22 at 11:09 pm');
expect(toDateFormat(1592879617000)).toBe('Jun 22 at 10:33 pm');
expect(toDateFormat(1592879267000)).toBe('Jun 22 at 10:27 pm');
expect(toDateFormat(1592879146000)).toBe('Jun 22 at 10:25 pm');
expect(toDateFormat(1592878450000)).toBe('Jun 22 at 10:14 pm');
});
});
});
23 changes: 23 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
process.env.TZ = 'America/Toronto';

const config = {
preset: 'react-native',
setupFiles: ['<rootDir>/app/util/testSetup.js'],
transform: {
'^.+\\.js$': '<rootDir>jest.preprocessor.js'
},
transformIgnorePatterns: [
'node_modules/(?!(react-native|rn-fetch|redux-persist-filesystem|@react-navigation|@react-native-community|react-navigation|react-navigation-redux-helpers|@sentry))'
],
snapshotSerializers: ['enzyme-to-json/serializer'],
collectCoverage: false,
collectCoverageFrom: [
'<rootDir>/app/**/*.{js,jsx}',
'!<rootDir>/node_modules/',
'!<rootDir>/app/util/*.{js,jsx}',
'!<rootDir>/app/entry*.js'
]
};

// eslint-disable-next-line import/no-commonjs
module.exports = config;
22 changes: 0 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,28 +293,6 @@
"runner-config": "e2e/config.json",
"test-runner": "jest"
},
"jest": {
"preset": "react-native",
"setupFiles": [
"<rootDir>/app/util/testSetup.js"
],
"transform": {
"^.+\\.js$": "<rootDir>jest.preprocessor.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(react-native|rn-fetch|redux-persist-filesystem|@react-navigation|@react-native-community|react-navigation|react-navigation-redux-helpers|@sentry))"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"collectCoverage": false,
"collectCoverageFrom": [
"<rootDir>/app/**/*.{js,jsx}",
"!<rootDir>/node_modules/",
"!<rootDir>/app/util/*.{js,jsx}",
"!<rootDir>/app/entry*.js"
]
},
"react-native": {
"crypto": "react-native-crypto",
"_stream_transform": "readable-stream/transform",
Expand Down

0 comments on commit ef1a953

Please sign in to comment.