AmendHub

Download:

vkoskiv

/

MacNTP

/

amendments

/

9

Actually update system clock :^)

Surprisingly easy, OSUtils provides a simple SetDateTime() that
takes seconds.
Still need to figure out how to configure UTC offset. Will likely
have to be a cdev panel to configure manually, I doubt people worried
about timezone switching in 1991 much.

vkoskiv made amendment 9 about 1 year ago
--- main.c Tue Aug 22 20:55:58 2023 +++ main.c Tue Aug 22 23:26:24 2023 @@ -1,5 +1,6 @@ #include <stdio.h> #include <string.h> +#include <OSUtils.h> #include "util.h" #include "tcp.h" #include "dnr.h" @@ -9,6 +10,7 @@ void dump_ntp_packet(struct ntp_packet *packet); u_int8_t leap_information(struct ntp_packet pkt); u_int8_t version_number(struct ntp_packet pkt); u_int8_t mode(struct ntp_packet pkt); +void set_system_time(struct ntp_ts ts); static global_received = 0; static global_err = 0; @@ -49,6 +51,32 @@ u_int8_t mode(struct ntp_packet pkt) { return pkt.li_vn_mode & 7; } +#define offset +void set_system_time(struct ntp_ts ts) { + /* + The NTP protocol expresses time as seconds from 01-01-1900. + The Macintosh expresses them as seconds from 01-01-1904. + This first step converts the two. + Seconds between 01-01-1900 and 01-01-1904 (none were leap years): + 4 * 365 * 24 * 60 * 60 = 126144000 + */ + //FIXME: Find out how timezone offsets are done on Sys6, if at all. + OSErr err; + long mac_seconds; + short utc_offset = 3; + long ntp_seconds = ts.upper; + ntp_seconds += utc_offset * 60 * 60; + mac_seconds = ntp_seconds - 126144000; + err = SetDateTime(mac_seconds); + if (err) { + if (err == -86) { // clkWrErr + printf("clkWrErr encountered\n"); + } else if (err == -85) { // clkRdErr + printf("clkRdErr encountered\n"); + } + } +} + struct ntp_request { ip_addr host_ip; char *url; @@ -207,6 +235,7 @@ int main(int argc, char **argv) { } dump_ntp_packet(&req.payload); + set_system_time(req.payload.reference_timestamp); error: _UDPRelease(&req.udp_iopb, req.udp_stream, nil, nil, false);