# [ncl-talk] Date calculation of hours since "0001-01-01 00:00"

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Mon Apr 16 13:59:27 MDT 2018

```Jerry et al,

I also checked this using only NCL and 32-bit integer math.  Please see the
attached NCL script, and check my math.  The result agrees with cd_calendar.

Please notice that there are two different calendar systems involved.
Years 0001 to 1581 are in the Julian calendar, and 1583 to 2018 are in
the Gregorian calendar.  This script breaks up the full interval into
several segments, and computes the exact number of hours in each segment,
using the correct set of calendar rules for each segment.  This is the
result:

(0) nhour_julian   = 13859040
(0) nhour_1582     = 8520
(0) nhour_greg     = 3813144
(0) nhour_2018     = 1824
(0) nhour_march_18 = 14
(0)
(0) Total hours    = 17682542
(0) Target         = 17682542 hours since 0001-01-01 00:00

However, the Julian calendar is not a proper standard, and there are
historical variations.  See the Wikipedia article.  This may have led to
varying interpretations in different software packages, and there are also
known bugs in some calendar software.  My script uses the Udunits
interpretation, which I think is the same as cd_calendar.

Best practice is to completely avoid the Julian calendar, and use only
modern Gregorian origin dates (1583 or later) with modern data, such as:

"days since 1900-01-01"
"hours since 2000-01-01"

To avoid future problems, I recommend that you ask your data source to
change their files to use only the Gregorian calendar and modern origin
dates.

--Dave

On Mon, Apr 16, 2018 at 8:22 AM, Beáta Szabó-Takács <szabo.b at czechglobe.cz>
wrote:

> Dear Jerry,
>
>
> I checked your date conversion in Matlab as well by the following way:
>
> I divided 17682542 hours by 24 to get days (736772.6 days). I calculated
> the serial date number of 0001.01.01 00:00:00 by datenum function which is
> 372 days. I added up these days (736772.6 + 372) and used this value in
> datevec function which resulted 2018.03.20 14:00. In my view, the NCL
> cd_calendar function is correct because in Gregorian calendar there are no
> double leap years (372 days).
>
> Furthermore, I checked the date -d "17682542 hours 0001-01-01 00:00"
> +"%Y-%m-%d %H:%M" function on different linux system and I got different
> date values:
>
> Debian (GNU/Lin 8): 2018-03-20 13:43
>
> Ubuntu 14.04: 2018-03-20 14:02
>
> Linux 3.10.0: 2018-03-20 14:00.
>
>
>
> Best regards,
>
> Beata
> ------------------------------
> *From:* ncl-talk <ncl-talk-bounces at ucar.edu> on behalf of Jianyu Liu <
> jerry_leo at msn.com>
> *Sent:* Monday, April 9, 2018 10:46:18 AM
> *To:* ncl-talk at ucar.edu
> *Subject:* [ncl-talk] Date calculation of hours since "0001-01-01 00:00"
>
>
> Hi,
>
> Just noticed the inconsistent of date calculation of hours since
> "0001-01-01 00:00",   there are 2 days difference between NCL and other
> script/command, such as date, php.
>
> Saying "17682542 hours since 0001-01-01 00:00",  NCL gave 2018-03-18 14:00,
> Linux date command and php script gave 2018-03-20 14:00.   Wondering who
> is correct.
>
> Here are the output of NCL
>
> ncl 1> print (f->time)
>
> Variable: time (file variable)
> Type: double
> Total Size: 8 bytes
>             1 values
> Number of Dimensions: 1
> Dimensions and sizes:   [time | 1]
> Coordinates:
>             time: [17682542..17682542]
> Number Of Attributes: 2
>   units :       hours since 0001-01-01 00:00:00.0
>   calendar :    standard
> (0)     17682542
>
> ncl 2> utc_date=cd_calendar(f->time,0)
> ncl 3> print(utc_date)
> Variable: utc_date
> Type: float
> Total Size: 24 bytes
>             6 values
> Number of Dimensions: 2
> Dimensions and sizes:   [1] x [6]
> Coordinates:
> Number Of Attributes: 1
>   calendar :    standard
> (0,0)   2018
> (0,1)    3
> (0,2)   18
> (0,3)   14
> (0,4)    0
> (0,5)   1.341105e-05
>
> Here are the output of date command
>
>     date -d "17682542 hours 0001-01-01 00:00" +"%Y-%m-%d %H:%M"
>
> 2018-03-20 14:00
>
> Here are the output of PHP
>
>     ini_set('date.timezone', 'UTC');
>     \$t=strtotime('0001-01-01  +17682542 hours) ;
>     echo date('Y-m-d H:i', \$t);
>
> 2018-03-20 14:00
>
>
> Regards
>
> Jerry
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180416/dc9c08f7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: construct.date.0413.ncl
Type: application/octet-stream
Size: 2368 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180416/dc9c08f7/attachment.obj>
```