Gregorian to Julian DateTime calculation not returning the Time portion of julian day

japreja

Active member
I can get the appropriate Julian Day portion but I am having trouble with the hours, minutes, seconds portion of Julian day. No matter what I put for hours, minutes, seconds, I dont get that part of the calculation. What am I doing wrong?

Here is the code I have so far.

Code:
/**
  *
  *  Convert a date from Calandar Date (Gregorian) to Julian, and Julian to Gregorian
  *
  *  from https://aa.usno.navy.mil/faq/JD_formula 
  *
**/


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial){
    ; // wait for serial port to connect. Needed for native USB port only
  }


  delay(10);


  Serial.print("Converting 2023-12-24 at 10:01:01 UTC to Julian Day = ");
  Serial.println(String(g2j(2023, 12, 24, 10, 01, 01 )) + " \n");
}


double g2j(int year, int month, int day, int hh, int mm, int ss)
{
  double JD;
  double JDTime;
 
  JD = day-32075+1461*(year+4800+(month-14)/12)/4+367*(month-2-(month-14)/12*12)/12-3*((year+4900+(month-14)/12)/100)/4;
  JDTime = JD + (((hh-12)/24) + (mm/1440) + (ss/86400));
  return JDTime;
}


void loop() {
  // put your main code here, to run repeatedly:


}

Thank in advance for any help.
Jorge
 
The formula in the reference is based on integer arithmetic
I would try to replace "double JD" by "int32_t JD"
having the results declared as double (i.e. floating point) may inspire the compiler to estimate all in float
double JDTime seems OK
 
I toyed around with this quite a bit and realized that I should be using printf with %lf . Keeping JD as a double and changing the hh, mm, ss to double for the inputs, also wrapping the entire calculation in () gave me the expected results for the given date of 2023-12-24 at 10:01:01. By not using printf with %lf everything was getting truncated to 2 digits of precision. Here is the resulting code that is not fully tested with several dates but seems to be working with 6 digits of precission.

Code:
/**
  *
  *  Convert a date from Calandar Date (Gregorian) to Julian, and Julian to Gregorian
  *
  *  from https://aa.usno.navy.mil/faq/JD_formula 
  *
**/


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial){
    ; // wait for serial port to connect. Needed for native USB port only
  }


  delay(10);


  Serial.printf("Converting 2023-12-24 at 10:01:01 UTC to Julian Day = %lf \n", g2j(2023, 12, 24, 10, 1, 1 )); // expecting 2460302.9173727 
  //Serial.println(String(g2j(2023.0, 12.0, 24.0, 10.0, 1.0, 1.0 )) + " \n");
}


double g2j(int year, int month, int day, double hh, double mm, double ss)
{
  double JD;
  double JDTime;
 
  // from fortran example : where I=YEAR, J=MONTH, K=DAY
  //  JD= K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4
  // so
 // JD= day-32075+1461*(year+4800+(month-14)/12)/4+367*(month-2-(month-14)/12*12)/12-3*((year+4900+(month-14)/12)/100)/4
  JD = (day-32075+1461*(year+4800+(month-14)/12)/4+367*(month-2-(month-14)/12*12)/12-3*((year+4900+(month-14)/12)/100)/4); // expecting 2460302.5000000 for 2023-12-24 at UT Noon
  JDTime = ((hh-12)/24) + (mm/1440) + (ss/86400);


  return JD + JDTime;
}


void loop() {
  // put your main code here, to run repeatedly:


}

Forum posts that helped me realize this are the following:
https://forum.pjrc.com/threads/6565...for-teensy-4-1?p=264979&viewfull=1#post264979
and
https://ssd.jpl.nasa.gov/tools/jdc/#/cd

I am going to alter the code a bit more to accept string input from the serial terminal and/or use Teensy 4.x internal RTC to calculate the time and date in Julian :)

Thanks
 
Back
Top