Skip to content

Commit

Permalink
feat(formula): date function single number
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Jan 30, 2024
1 parent b7970ab commit c533e6d
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';

import { FUNCTION_NAMES_DATE } from '../../function-names';
import { DateFunction } from '..';
import { NumberValueObject } from '../../../../engine/value-object/primitive-object';

describe('Test date function', () => {
const textFunction = new DateFunction(FUNCTION_NAMES_DATE.DATE);

describe('Date', () => {
it('Value is normal', () => {
const year = new NumberValueObject(2024);
const month = new NumberValueObject(1);
const day = new NumberValueObject(1);
const result = textFunction.calculate(year, month, day);
expect(result.getValue()).toBe(45292);
});
});
});
52 changes: 52 additions & 0 deletions packages/engine-formula/src/functions/date/date/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { BaseValueObject } from '../../..';
import { ErrorType, ErrorValueObject, NumberValueObject } from '../../..';
import { DEFFAULT_DATE_FORMAT, excelDateSerial } from '../../../basics/date';
import { BaseFunction } from '../../base-function';

export class DateFunction extends BaseFunction {
override calculate(year: BaseValueObject, month: BaseValueObject, day: BaseValueObject) {
if (year.isError()) {
return year;
}

if (month.isError()) {
return month;
}

if (day.isError()) {
return day;
}

// TODO@Dushusir: array
const yearValue = +year.getValue();
const monthValue = +month.getValue();
const dayValue = +day.getValue();

const date = new Date(yearValue, monthValue - 1, dayValue);

if (date.getMonth() !== monthValue - 1 || date.getDate() !== dayValue) {
return new ErrorValueObject(ErrorType.VALUE);
}

const currentSerial = excelDateSerial(date);
const valueObject = new NumberValueObject(currentSerial);
valueObject.setPattern(DEFFAULT_DATE_FORMAT);
return valueObject;
}
}
2 changes: 2 additions & 0 deletions packages/engine-formula/src/functions/date/function-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* limitations under the License.
*/

import { DateFunction } from './date';
import { FUNCTION_NAMES_DATE } from './function-names';
import { Today } from './today';

export const functionDate = [
[Today, FUNCTION_NAMES_DATE.TODAY],
[DateFunction, FUNCTION_NAMES_DATE.DATE],
];
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default {
},
],
functionParameter: {
number1: { name: 'number1', detail: 'first' },
number2: { name: 'number2', detail: 'second' },
year: { name: 'year', detail: 'The value of the year argument can include one to four digits. Excel interprets the year argument according to the date system your computer is using. By default, Univer uses the 1900 date system, which means the first date is January 1, 1900.' },
month: { name: 'month', detail: 'A positive or negative integer representing the month of the year from 1 to 12 (January to December).' },
day: { name: 'day', detail: 'A positive or negative integer representing the day of the month from 1 to 31.' },
},
},
DATEDIF: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default {
},
],
functionParameter: {
number1: { name: 'number1', detail: 'first' },
number2: { name: 'number2', detail: 'second' },
year: { name: '年', detail: 'year 引数の値 には 、1 ~ 4 桁の数字を指定できます。 Excel は、コンピューター が使用 している日付システムに応じて年の引数を解釈します。 既定では、Univer では 1900 年の日付システムが使用されます。つまり、最初の日付は 1900 年 1 月 1 日です。' },
month: { name: '月', detail: '1 ~ 12 (1 月から 12 月) の月を表す正または負の整数です。' },
day: { name: '日', detail: '1 ~ 31 の月の日を表す正または負の整数です。' },
},
},
DATEDIF: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

export default {
DATE: {
description: '返回特定日期的序列号',
description: '采用三个单独的值并将它们合并为一个日期。',
abstract: '返回特定日期的序列号',
links: [
{
Expand All @@ -25,8 +25,9 @@ export default {
},
],
functionParameter: {
number1: { name: 'number1', detail: 'first' },
number2: { name: 'number2', detail: 'second' },
year: { name: '年', detail: '可以包含 1 到 4 位数字。 Excel 根据计算机使用的日期系统解释 year 参数。 默认情况下,Univer 使用 1900 日期系统,这意味着第一个日期是 1900 年 1 月 1 日。' },
month: { name: '月', detail: '一个正整数或负整数,表示一年中从 1 月至 12 月(一月到十二月)的各个月。' },
day: { name: '日', detail: '一个正整数或负整数,表示一月中从 1 日到 31 日的各天。' },
},
},
DATEDIF: {
Expand Down
19 changes: 13 additions & 6 deletions packages/sheets-formula/src/services/function-list/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ export const FUNCTION_LIST_DATE: IFunctionInfo[] = [
abstract: 'formula.functionList.DATE.abstract',
functionParameter: [
{
name: 'formula.functionList.DATE.functionParameter.number1.name',
detail: 'formula.functionList.DATE.functionParameter.number1.detail',
example: 'A1:A20',
name: 'formula.functionList.DATE.functionParameter.year.name',
detail: 'formula.functionList.DATE.functionParameter.year.detail',
example: '2024',
require: 1,
repeat: 0,
},
{
name: 'formula.functionList.DATE.functionParameter.number2.name',
detail: 'formula.functionList.DATE.functionParameter.number2.detail',
example: 'A1:A20',
name: 'formula.functionList.DATE.functionParameter.month.name',
detail: 'formula.functionList.DATE.functionParameter.month.detail',
example: '1',
require: 1,
repeat: 0,
},
{
name: 'formula.functionList.DATE.functionParameter.day.name',
detail: 'formula.functionList.DATE.functionParameter.day.detail',
example: '1',
require: 1,
repeat: 0,
},
Expand Down

0 comments on commit c533e6d

Please sign in to comment.