RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime) { struct timeval tv; RTTimeSpecGetTimeval(pTime, &tv); set_real_time_clock(tv.tv_sec); return VINF_SUCCESS; }
/*! Read the CMOS clock and update the system time accordingly. */ static void rtc_hw_to_system(void) { uint32 current_time; current_time = arch_rtc_get_hw_time(); set_real_time_clock(current_time + (sIsGMT ? 0 : sTimezoneOffset)); }
status_t _user_set_real_time_clock(uint32 time) { if (geteuid() != 0) return B_NOT_ALLOWED; set_real_time_clock(time); return B_OK; }
static int rtc_debug(int argc, char **argv) { if (argc < 2) { // If no arguments were given, output all useful data. uint32 currentTime; bigtime_t systemTimeOffset = arch_rtc_get_system_time_offset(sRealTimeData); currentTime = (systemTimeOffset + system_time()) / 1000000; dprintf("system_time: %Ld\n", system_time()); dprintf("system_time_offset: %Ld\n", systemTimeOffset); dprintf("current_time: %lu\n", currentTime); } else { // If there was an argument, reset the system and hw time. set_real_time_clock(strtoul(argv[1], NULL, 10)); } return 0; }
status_t ntp_update_time(const char* hostname, const char** errorString, int32* errorCode) { hostent *server = gethostbyname(hostname); if (server == NULL) { *errorString = B_TRANSLATE("Could not contact server"); return B_ENTRY_NOT_FOUND; } ntp_data message; memset(&message, 0, sizeof(ntp_data)); message.leap_indicator = LEAP_CLOCK_NOT_IN_SYNC; message.version = NTP_VERSION_3; message.mode = MODE_CLIENT; message.stratum = 1; // primary reference message.precision = -5; // 2^-5 ~ 32-64 Hz precision message.root_delay.SetTo(1); // 1 sec message.root_dispersion.SetTo(1); message.transmit_timestamp.SetTo(seconds_since_1900()); int connection = socket(AF_INET, SOCK_DGRAM, 0); if (connection < 0) { *errorString = B_TRANSLATE("Could not create socket"); *errorCode = errno; return B_ERROR; } struct sockaddr_in address; address.sin_family = AF_INET; address.sin_port = htons(NTP_PORT); address.sin_addr.s_addr = *(uint32 *)server->h_addr_list[0]; if (sendto(connection, (char *)&message, sizeof(ntp_data), 0, (struct sockaddr *)&address, sizeof(address)) < 0) { *errorString = B_TRANSLATE("Sending request failed"); *errorCode = errno; return B_ERROR; } fd_set waitForReceived; FD_ZERO(&waitForReceived); FD_SET(connection, &waitForReceived); struct timeval timeout; timeout.tv_sec = 3; timeout.tv_usec = 0; // we'll wait 3 seconds for the answer if (select(connection + 1, &waitForReceived, NULL, NULL, &timeout) <= 0) { *errorString = B_TRANSLATE("Waiting for answer failed"); *errorCode = errno; return B_ERROR; } message.transmit_timestamp.SetTo(0); socklen_t addressSize = sizeof(address); if (recvfrom(connection, (char *)&message, sizeof(ntp_data), 0, (sockaddr *)&address, &addressSize) < (ssize_t)sizeof(ntp_data)) { *errorString = B_TRANSLATE("Message receiving failed"); *errorCode = errno; close(connection); return B_ERROR; } close(connection); if (message.transmit_timestamp.Integer() == 0) { *errorString = B_TRANSLATE("Received invalid time"); return B_BAD_VALUE; } time_t now = message.transmit_timestamp.Integer() - kSecondsBetween1900And1970; set_real_time_clock(now); return B_OK; }