diff --git a/CHANGELOG.md b/CHANGELOG.md index aab774f8f1..0f8dfe4220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed handling of arrays for ROWS/COLUMNS functions. (#677) - Fixed an issue with nested namedexpressions. (#679) - Fixed an issue with matrixDetection + number parsing. (#686) +- Fixed an issue with NOW and TODAY functions. (#709) - Fixed an issue with MIN/MAX function caches. (#711) ## [0.6.2] - 2021-05-26 diff --git a/src/interpreter/plugin/DateTimePlugin.ts b/src/interpreter/plugin/DateTimePlugin.ts index 5f55615fba..0137887eef 100644 --- a/src/interpreter/plugin/DateTimePlugin.ts +++ b/src/interpreter/plugin/DateTimePlugin.ts @@ -444,9 +444,9 @@ export class DateTimePlugin extends FunctionPlugin implements FunctionPluginType public now(ast: ProcedureAst, state: InterpreterState): InterpreterValue { return this.runFunction(ast.args, state, this.metadata('NOW'), () => { - const now = new Date() + const now = new Date(Date.now()) return timeToNumber({hours: now.getHours(), minutes: now.getMinutes(), seconds: now.getSeconds()})+ - this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDay()}) + this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDate()}) } ) } @@ -454,8 +454,8 @@ export class DateTimePlugin extends FunctionPlugin implements FunctionPluginType public today(ast: ProcedureAst, state: InterpreterState): InterpreterValue { return this.runFunction(ast.args, state, this.metadata('TODAY'), () => { - const now = new Date() - return this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDay()}) + const now = new Date(Date.now()) + return this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDate()}) } ) } diff --git a/test/interpreter/function-now.spec.ts b/test/interpreter/function-now.spec.ts index 2f58796579..de59e966c5 100644 --- a/test/interpreter/function-now.spec.ts +++ b/test/interpreter/function-now.spec.ts @@ -9,17 +9,62 @@ function sleepFor( sleepDuration: number ){ } describe('Interpreter - function NOW', () => { + let originalNow: () => number + + beforeEach(() => { + originalNow = Date.now + let cnt = 20 + Date.now = () => { + cnt += 1 + return Date.parse(`1985-08-16T03:45:${cnt}`) + } + }) + it('works', () => { const engine = HyperFormula.buildFromArray([ ['=NOW()'], ]) const t1 = engine.getCellValue(adr('A1')) as number + expect(t1).toBeCloseTo(31275.1565856481) expect(engine.getCellValueDetailedType(adr('A1'))).toBe(CellValueDetailedType.NUMBER_DATETIME) - sleepFor(1000) engine.setCellContents(adr('A2'), null) - const t2 = engine.getCellValue(adr('A1')) as number - const delta = (t2-t1)*(24*60*60) - expect(delta).toBeCloseTo(1) + const delta = engine.getCellValue(adr('A1')) as number - t1 + expect(delta*24*60*60).toBeGreaterThanOrEqual(1) //internals of the engine are also using Date.now(), so the value should be actually 4 or even more + }) + + it('works #2', () => { + const engine = HyperFormula.buildFromArray([ + ['=YEAR(NOW())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(1985) + }) + + it('works #3', () => { + const engine = HyperFormula.buildFromArray([ + ['=MONTH(NOW())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(8) + }) + + it('works #4', () => { + const engine = HyperFormula.buildFromArray([ + ['=DAY(NOW())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(16) + }) + + it('works #5', () => { + const engine = HyperFormula.buildFromArray([ + ['=HOUR(NOW())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(3) + }) + + it('works #6', () => { + const engine = HyperFormula.buildFromArray([ + ['=MINUTE(NOW())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(45) }) it('validates number of arguments', () => { @@ -29,4 +74,8 @@ describe('Interpreter - function NOW', () => { expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) }) + + afterEach(() => { + Date.now = originalNow + }) }) diff --git a/test/interpreter/function-today.spec.ts b/test/interpreter/function-today.spec.ts index 44df73105d..ae1c132394 100644 --- a/test/interpreter/function-today.spec.ts +++ b/test/interpreter/function-today.spec.ts @@ -4,14 +4,46 @@ import {ErrorMessage} from '../../src/error-message' import {adr, detailedError} from '../testUtils' describe('Interpreter - function TODAY', () => { + let originalNow: () => number + + beforeEach(() => { + originalNow = Date.now + let cnt = 20 + Date.now = () => { + cnt += 1 + return Date.parse(`1985-08-16T03:45:${cnt}`) + } + }) + it('works', () => { const engine = HyperFormula.buildFromArray([ ['=TODAY()'], ]) - expect(engine.getCellValue(adr('A1'))).not.toBeNull() + expect(engine.getCellValue(adr('A1'))).toEqual(31275) expect(engine.getCellValueDetailedType(adr('A1'))).toBe(CellValueDetailedType.NUMBER_DATE) }) + it('works #2', () => { + const engine = HyperFormula.buildFromArray([ + ['=YEAR(TODAY())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(1985) + }) + + it('works #3', () => { + const engine = HyperFormula.buildFromArray([ + ['=MONTH(TODAY())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(8) + }) + + it('works #4', () => { + const engine = HyperFormula.buildFromArray([ + ['=DAY(TODAY())'], + ]) + expect(engine.getCellValue(adr('A1'))).toEqual(16) + }) + it('validates number of arguments', () => { const engine = HyperFormula.buildFromArray([ ['=TODAY(42)'], @@ -19,4 +51,8 @@ describe('Interpreter - function TODAY', () => { expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) }) + + afterEach(() => { + Date.now = originalNow + }) })