• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @file Describe the file
3 * Copyright (c) 2023 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * 根据公历的年月日,获取儒略历对应的儒略日
19 * (通用算法)
20 *
21 * @param year 年份
22 * @param month 月份
23 * @param monthDay 该月日期
24 */
25export function getJulianDay(year: number, month: number, monthDay: number): number {
26  let num = 0;
27  if (month <= 2) {
28    month += 12;
29    year -= 1;
30  }
31  if (year * 372 + month * 31 + monthDay >= 588829) {
32    num = Math.floor(year / 100);
33    num = 2 - num + Math.floor(year / 400);
34  }
35  const day: number = monthDay + 0.5000115740;
36  const date = Math.floor(365.25 * (year + 4716) + 0.01) + Math.floor(30.60001 * (month + 1)) + day + num - 1524.5;
37  return Math.floor(date);
38}
39
40export function getJulianDayByMills(date: Date, timeMills: number) {
41  if (date === null || date === undefined) {
42    date = new Date();
43  }
44  date.setTime(timeMills);
45  return getJulianDay(date.getFullYear(), date.getMonth() + 1, date.getDate());
46}