xref: /openbsd-src/gnu/usr.bin/perl/cpan/Time-Piece/t/04mjd.t (revision b39c515898423c8d899e35282f4b395f7cad3298)
1*b39c5158Smillertuse Test;
2*b39c5158SmillertBEGIN { plan tests => 12 }
3*b39c5158Smillert# Test the calculation of (modified) Julian date
4*b39c5158Smillertuse Time::Piece;
5*b39c5158Smillert
6*b39c5158Smillert# First a lookup table of epoch and MJD
7*b39c5158Smillert# Use 3 sig fig in MJD (hence the use of strings)
8*b39c5158Smillert# This will not work on systems that use a different reference
9*b39c5158Smillert# epoch to unix time. To be more general we should use strptime
10*b39c5158Smillert# to parse the reference date.
11*b39c5158Smillertmy %mjd = (
12*b39c5158Smillert          951827696  => '51603.524', # 2000-02-29T12:34:56UT
13*b39c5158Smillert          1000011    => '40598.574', # 1970-01-12T13:46:51UT
14*b39c5158Smillert          1021605703 => '52411.140', # 2002-05-17T03:21:43UT
15*b39c5158Smillert          1121605703 => '53568.547', # 2005-07-17T13:08:23UT
16*b39c5158Smillert          1011590000 => '52295.218', # 2002-01-21T05:13:20UT
17*b39c5158Smillert          1011605703 => '52295.399', # 2002-01-21T09:35:03
18*b39c5158Smillert         );
19*b39c5158Smillert
20*b39c5158Smillert# Now loop over each MJD
21*b39c5158Smillertfor my $time (keys %mjd) {
22*b39c5158Smillert
23*b39c5158Smillert  # First check using GMT
24*b39c5158Smillert  my $tp = gmtime( $time );
25*b39c5158Smillert  ok(sprintf("%.3f",$tp->mjd),$mjd{$time});
26*b39c5158Smillert
27*b39c5158Smillert  # Now localtime should give the same answer for MJD
28*b39c5158Smillert  # since MJD is always referred to as UT
29*b39c5158Smillert  $tp = localtime( $time );
30*b39c5158Smillert  ok(sprintf("%.3f",$tp->mjd),$mjd{$time});
31*b39c5158Smillert
32*b39c5158Smillert}
33*b39c5158Smillert
34