bash - 如何获得(当前)儒略日数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43317428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:00:31  来源:igfitidea点击:

bash - how to get (current) Julian day number?

bashjulian-date

提问by Buffalo

How to get today's Julian day number (JDN)equivalent? or any date's, for that matter?

如何获得今天的儒略日数 (JDN)等价物?或任何日期,就此而言?

I've looked and looked, but only found some functions that produced "year-dayOfYear", not something like: 2457854.

我看了又看,但只发现了一些产生“year-dayOfYear”的函数,而不是像:2457854这样的函数。

回答by Buffalo

I ended up porting one myself. Here is a SSCCE:

我最终自己移植了一个。这是一个SSCCE

#!/bin/bash

function GetJulianDay # year, month, day
{
  year=
  month=
  day=

  jd=$((day - 32075 + 1461 * (year + 4800 - (14 - month) / 12) / 4 + 367 * (month - 2 + ((14 - month) / 12) * 12) / 12 - 3 * ((year + 4900 - (14 - month) / 12) / 100) / 4))

  echo $jd
}

function GetTodayJulianDay
{
  year=`date +"%Y"`
  month=`date +"%m"`
  day=`date +"%d"`

  todayJd=$(GetJulianDay $year $month $day)

  echo $todayJd
}

# samples

todayJd=$(GetTodayJulianDay)
echo "got today jd: $todayJd"

yesterdayJd=$(GetJulianDay 2017 4 9)
echo "got yesterday jd: $yesterdayJd"

Test it: http://aa.usno.navy.mil/data/docs/JulianDate.php

测试一下:http: //aa.usno.navy.mil/data/docs/JulianDate.php

回答by DeanGuilberry

in bash date +%jreturns julian date.

在 bash 中date +%j返回朱利安日期。

[root@TX-Serv-1 ~]# date +%j
108

Also Julian dates in the future are pretty easy too.

未来的朱利安约会也很容易。

[root@es-host01 ~]# date --date='10 days' +%j
119
[root@es-host01 ~]# date --date='2 weeks' +%j
123

回答by user11034172

OLD_JULIAN_VAR=$(date -u -d 1840-12-31 +%s)

TODAY_DATE=`date --date="$odate" +"%Y-%m-%d"`
TODAY_DATE_VAR=`date -u -d "$TODAY_DATE" +"%s"`
export JULIAN_DATE=$((((TODAY_DATE_VAR - OLD_JULIAN_VAR))/86400))
echo $JULIAN_DATE

the mathematically

数学上的

[(date in sec)-(1840-12-31 in sec)]/86400 

回答by vdavid

You could get the Unix date with date +%s --date=YYYYMMDDand use it to compute the Julian Day from Unix date

您可以获取 Unix 日期date +%s --date=YYYYMMDD并使用它从 Unix 日期计算儒略日

It would end up something like:

它最终会是这样的:

echo $(( $(date +%s --date=YYYYMMDD) / 86400 + 2440587 ))

Edit:I don't know how important this is, but some people like to add 0.5 to the conversion. Basically this means that if your current date/time is after noon, the conversion would put the Julian date 1 day later.

编辑:我不知道这有多重要,但有些人喜欢将 0.5 添加到转换中。基本上这意味着如果您当前的日期/时间在中午之后,则转换会将 Julian 日期推迟 1 天。