-1

This is the follow up of a previous question of mine.

In a nutshell, I am trying to follow this tutorial step-by-step: https://jtauber.github.io/mars-clock/ to get to Coordinated Mars Time, but I got stuck right before the end. My code works fine up until the end (some values are more accurate than in the tutorial because I went back to the source from NASA: https://www.giss.nasa.gov/tools/mars24/help/algorithm.html ):

double millis = ( [[NSDate date] timeIntervalSince1970] * 1000 );
NSLog(@"millis: %f", millis);

double JDUT = ( 2440587.5 + (millis / 86400000) );
NSLog(@"JDUT: %f", JDUT);

double JDTT = ( JDUT + (37 +32.184) / 86400);
NSLog(@"JDTT: %f", JDTT);

double J2000Epoch = ( JDTT - 2451545.0 );
NSLog(@"J2000Epoch: %f", J2000Epoch);

double MSD = ( (( J2000Epoch - 4.5 ) / 1.0274912517) + 44796.0 - 0.0009626 );
NSLog(@"MSD: %f", MSD);

The only step remaining is actually calculating Coordinated Mars Time, using this equation:

MTC = mod24 { 24 h × MSD } 

The problem is that I have no idea how. I tried to use modf( (double), (double *) ) but no idea how it actually works. I tried it the way below, but it gave me an incorrect answer (obviously as I have really no idea what I am doing). :(

double MSD24 = (24 * MSD);
double MCT = modf(24, &MSD24);
NSLog(@"MCT: %f", MCT); // Result: 0.000000

Any help would be much appreciated. Thank you very much!

p.s.: Notice that I use Objective-C; I do not understand swift unfortunately! :(

Gergely Kovacs
  • 1,045
  • 2
  • 10
  • 28
  • I would break this question up into more specific parts. This talks about mod operators: https://stackoverflow.com/questions/10351293/mod-operator-in-ios And this talks about date formatters: https://stackoverflow.com/questions/34765304/date-formatter-for-a-certain-format-of-date Once you get those two working, I think the remaining question is a math one. The link you provided has all of the equations laid out. Never thought of MTC until now, either. Also didn't know a Mars day is called a sol, thought it was just called a Mars day. – trndjc Dec 23 '19 at 19:43
  • Sorry but your question is rather diffuse. You seem to be asking two different things. Could you pick just one problem and make very clear what it is? – matt Dec 23 '19 at 20:19
  • I have modified the question so it only asks one. – Gergely Kovacs Dec 23 '19 at 21:59
  • 1
    Stylistic note: I would avoid using `CGFloat` outside of graphics-related code. Use plain old `float` or `double` instead. – Gereon Dec 23 '19 at 22:44
  • Hi, I think I have used those at the beginning, but they gave me widely inaccurate results. CGFloat was the only one that worked. But I will try again with simple floats; maybe I just made a typo or something. – Gergely Kovacs Dec 24 '19 at 07:17
  • Double works, but float does not. Float gives incorrect results. – Gergely Kovacs Dec 24 '19 at 08:59

1 Answers1

1

Carrying on from the code you gave, I tried:

CGFloat MTC = fmod(24 * MSD, 24);

and got

// 19.798515

which was right according to the web page you cited at the moment I tried it.

The sort of thing his page actually shows, e.g. "19:49:38" or whatever (at the time I tried it), is merely a string representation of that number, treating it as a number of hours and just dividing it up into minutes and seconds in the usual way. Which, I suppose, brings us to the second part of your question, i.e. how to convert a number of hours into an hours-minutes-seconds representation? But that is a simple matter, dealt with many times here. See NSNumber of seconds to Hours, minutes, seconds for example.

So, carrying on once again, I tried this:

CGFloat secs = MTC*3600;
NSDate* d = [NSDate dateWithTimeIntervalSince1970:secs];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = @"HH:mm:ss";
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSString* result = [df stringFromDate:d];
NSLog(@"%@", result); // 20:10:20

...which is exactly the same as his web page was showing at that moment.

And here's a Swift version for those who would like to know what the "mean time" is on Mars right now:

let millis = Date().timeIntervalSince1970 * 1000
let JDUT = 2440587.5 + (millis / 86400000)
let JDTT = JDUT + (37 + 32.184) / 86400
let J2000Epoch = ( JDTT - 2451545 )
let MSD = (( J2000Epoch - 4.5 ) / 1.0274912517) + 44796.0 - 0.0009626
let MTC = (24 * MSD).truncatingRemainder(dividingBy: 24)
let d = Date(timeIntervalSince1970: MTC*3600)
let df = DateFormatter()
df.dateFormat = "HH:mm:ss"
df.timeZone = TimeZone(abbreviation: "GMT")!
df.string(from:d)
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I don't quite see what the downvote is for. Sure, I wouldn't use CGFloat either, but the goal seems to be to get the same result as https://jtauber.github.io/mars-clock/ at the instant the code runs, and that is exactly what we do get. – matt Dec 23 '19 at 23:52
  • Thank you Matt! I will try this as soon as possible and report back! – Gergely Kovacs Dec 24 '19 at 07:18
  • This is the answer; it works perfectly. Thank you very much! – Gergely Kovacs Dec 24 '19 at 09:02