Date and Time Subtraction

Thank you Ben for pointing out.
I multiply NTP returned by -1. (I do not know if this is right thing.)

Now is weird, latest NTP data is smaller.

NTP is unsigned,
therefore all bits are data.

PAC control is signed,
therefore treats the MSB as sign, 0 for positve and 1 for negative.

How do we tell PAC to ignore MSB?
Am I going in the right direction here?

I think its good now, I used BitClear to 63rd position:

// Get NTP Timestamp and store to [0]
nsChart = GetDateTime(nt_DateTime);
nsChart = DateTimeToNtpTimestamp(nt_DateTime, n64_temp);
//nt64_NtpReference[0] = n64_temp * -1;
nt64_NtpReference[0] = BitClear(n64_temp,63);

It is working now. :slight_smile:

Don’t do this. You don’t need to. You’ll end up with the opposite problem in about 17 years. Just take the difference of the two numbers, it will be fine.

How are you converting to minutes? The timestamp values are not in units of minutes, the most significant 32 bits are seconds, and the least significant bits are fractions of a second.

If you want to get the difference in minutes then divide the difference of the two timestamps by 2^32 * 60 - Calculate out this constant (literal) and put an i64 suffix on it, don’t use the Power function as it doesn’t support 64 bit integers.

Thank you Philip, I followed your computation.

So far it worked.

// Get NTP Timestamp and store to [0]
nsChart = GetDateTime(nt_DateTime);
nsChart = DateTimeToNtpTimestamp(nt_DateTime, n64_temp);
nt64_NtpReference[0] = BitClear(n64_temp,63);

DelaySec(310);

// Get NTP Timestamp and store to [1]
nsChart = GetDateTime(nt_DateTime);
nsChart = DateTimeToNtpTimestamp(nt_DateTime, n64_temp);
nt64_NtpReference[1] = BitClear(n64_temp,63);

// subtract NTP Timestamp
n64_DiffInMin = nt64_NtpReference[1] - nt64_NtpReference[0];

//convert to min
n64_DiffInMin = ((n64_DiffInMin * 144) / (Power(2,32) * 8640));

DelayMsec(10000);

image

Well, you sort of did.

Don’t clear bit 64 or multiply by -1, this will cause you grief in about 17 years.
I also said not to use Power with 64 bit numbers, but the documentation on this is wrong (@GrayChurch.Opto22) - in testing it seems to support returning a 64 bit integer, so that is okay. I would change it to a constant anyways and don’t forget the i64 suffix on any constants you are using with 64 bit numbers.

Slight modifications below. Should work until around 2104ish and could work beyond that if you care to test for it (overflow from 7FFFFFFFFFFFFFFF to 8000000000000000).

// Get NTP Timestamp and store to [0]
nsChart = GetDateTime(nt_DateTime);
nsChart = DateTimeToNtpTimestamp(nt_DateTime, n64_temp);
nt64_NtpReference[0] = n64_temp;

DelaySec(310);

// Get NTP Timestamp and store to [1]
nsChart = GetDateTime(nt_DateTime);
nsChart = DateTimeToNtpTimestamp(nt_DateTime, n64_temp);
nt64_NtpReference[1] = n64_temp;

// subtract NTP Timestamp
n64_DiffInMin = nt64_NtpReference[1] - nt64_NtpReference[0];

//convert to min
n64_DiffInMin = n64_DiffInMin / 257,698,037,760i64;

DelayMsec(10000);
1 Like

Thanks for the review, will modify and get back.