Borrar filtros
Borrar filtros

I have error when i convert modified julian data to datetime

12 visualizaciones (últimos 30 días)
yunji song
yunji song el 20 de Oct. de 2023
Editada: Stephen23 el 20 de Oct. de 2023
I want to convert MJD to calender date in exact same date
So i test this code
%%%%%%%%%%%%%%%%%%
time0=datetime(2023,3,28,7,0,0);
time_mjd=mjuliandate(time0);
time1=datetime(time_mjd,'ConvertFrom','MJD');
time0==time1
ans = logical
0
%%%%%%%%%%%%%%%%%%%%%%%%%%%
but time0 and time1 is not exact same date
time0 is 2023/3/28 07:00:00
time1 is 2023/2/28 06:59:59
how can i convert time_mjd to exact same date with time0?

Respuestas (2)

Walter Roberson
Walter Roberson el 20 de Oct. de 2023
MJD is a double, and so has resolution on the order of 6e-7 seconds.
Which happens to not be good enough to convert back to exactly the desired time to within the nanosecond. The re-converted value is about 24 nanoseconds before the target time.
format long g
time0=datetime(2023,3,28,7,0,0);
time_mjd = mjuliandate(time0)
time_mjd =
60031.2916666667
time1=datetime(time_mjd,'ConvertFrom','MJD');
next_time_mjd = time_mjd * (1+eps)
next_time_mjd =
60031.2916666667
time2 = datetime(next_time_mjd, 'ConvertFrom', 'MJD');
time0==time1
ans = logical
0
time0 == time2
ans = logical
0
seconds(time0 - time1)
ans =
2.44140625e-07
seconds(time0 - time2)
ans =
-9.765625e-07
eps(time_mjd) * 24 * 60 * 60
ans =
6.28642737865448e-07
What can you do? Not much... not without assumptions. For example,
time1
time1 = datetime
28-Mar-2023 06:59:59
time1.Second = round(time1.Second,6)
time1 = datetime
28-Mar-2023 07:00:00
but that assumes that you are okay with your modified julian date only being considered accurate to the nearest microsecond.

Stephen23
Stephen23 el 20 de Oct. de 2023
Editada: Stephen23 el 20 de Oct. de 2023
"how can i convert time_mjd to exact same date with time0?"
The short answer is that you can't, simply because the limited precison of one DOUBLE class number is insufficient to represent that date down to nanoseconds. In contrast, the DATETIME object does not use one single floating point value to store its data, and so it has a higher precision (the documentation claims "nanosecond precision").
For your example date, the precision of the Julian date is:
time0 = datetime(2023,3,28,7,0,0);
time_mjd = mjuliandate(time0)
time_mjd = 6.0031e+04
class(time_mjd)
ans = 'double'
small = eps(time_mjd)
small = 7.2760e-12
seconds(days(small))
ans = 6.2864e-07
around one micro-second or so. Once you have made the conversion from DATETIME (with nanosecond precision) to DOUBLE (with microsecond precision) then you have irretrievably lost information.
So your question boils down to "if I convert from a higher-precision data type to a low-precision data type and then back to a high-precision data type, can I retain all of the high-precision information?" It should be clear to all readers that this is computationally and mathematically impossible. If you want to store julian dates (i.e. whole and fractional days) as a binary floating point number then you need to accept all of the behaviors of binary floating point numbers, including finite limited precision etc.
Note that this is all predicated on the use of one DOUBLE type value to store the Julian date. Of course there is nothing stopping you from writing your own class that uses high-precision numerics to store the Julian date, and that supports all of the required mathematical operations, etc. Writing, testing, etc. would not be a trivial task.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by