69 #include <sys/types.h> 72 #define MBG_FIRST_COPYRIGHT_YEAR 2001 73 #define MBG_LAST_COPYRIGHT_YEAR 0 // use default 75 static const char *
pname =
"mbgsetsystime";
88 return snprintf_safe( s, max_len,
"%04i-%02i-%02i %02i:%02i:%02i.%09li UTC",
89 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
90 tm.tm_hour, tm.tm_min, tm.tm_sec,
91 (
long) p_ts->tv_nsec );
102 #if defined( MBG_TGT_WIN32 ) 103 #define clock_settime mbg_clock_settime //### TODO cleanup 106 int rc = clock_settime( CLOCK_REALTIME, p_ts );
114 fprintf( stderr,
"Failed to set system time to %s: %s\n", ws,
mbg_strerror( rc ) );
118 printf(
"Date/time set to %s\n", ws );
129 struct tm tm = { 0 };
136 if ( t == (time_t) -1 )
138 fprintf( stderr,
"Failed to convert %02u.%02u.%02u %02u:%02u:%02u.%02u (UTC%+02ih) to system time\n",
153 if ( ( tm.tm_year % 100 != p_t->
year ) ||
154 ( tm.tm_mon + 1 != p_t->
month ) ||
155 ( tm.tm_mday != p_t->
mday ) ||
156 ( tm.tm_hour != p_t->
hour ) ||
157 ( tm.tm_min != p_t->
min ) ||
158 ( tm.tm_sec != p_t->
sec ) )
160 fprintf( stderr,
"reversely computed date/time differs from original\n" );
164 #if defined( MBG_TGT_WIN32 ) 175 u.ull = FILETIME_1970 +
176 (ULONGLONG) t * 10 * 1000 * 1000 +
179 if ( !FileTimeToSystemTime( &u.ft, &st ) )
183 fprintf( stderr,
"Failed to convert FILETIME to system time: %s\n",
188 if ( !SetSystemTime( &st ) )
192 DWORD last_error = GetLastError();
194 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
195 FORMAT_MESSAGE_FROM_SYSTEM |
196 FORMAT_MESSAGE_IGNORE_INSERTS,
199 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
205 fprintf( stderr,
"Failed to set system time: %s (code 0x%08lX)\n",
206 lpMsgBuf, (
ulong) last_error );
209 LocalFree( lpMsgBuf );
213 fprintf( stderr,
"Failed to set system time: err %i\n", err );
220 #else // assuming POSIX 249 printf(
"Radio clock has no valid date/time.\n" );
265 struct timespec ts = { 0 };
276 printf(
"Radio clock has no valid date/time.\n" );
292 static int system_time_has_been_set;
296 if ( system_time_has_been_set )
307 system_time_has_been_set = 1;
324 "This program can be used to set the system time to the card's time.\n" 325 "This should be done only at boot time, before the NTP daemon is started.\n" 326 "Please *don't* run this program while ntpd is already active." 336 int main(
int argc,
char *argv[] )
344 while ( ( c = getopt( argc, argv,
"h?" ) ) != -1 )
static int set_system_time(const struct timespec *p_ts)
invalid time because battery had been disconnected, or absolute time can't be decoded safely ...
uint8_t year
year of the century, 0..99
uint8_t mday
day of month, 0..31
uint8_t sec100
hundredths of seconds, 0..99, 10 ms resolution
Requested action completed successfully.
static int do_set_system_time_from_pcps_hr_time(MBG_DEV_HANDLE dh)
bool mbg_cond_err_msg(int rc, const char *what)
Check if a value is an error code and print an associated error message.
PCPS_SECONDS sec
seconds since 1970, usually UTC scale
_MBG_API_ATTR int _MBG_API mbg_get_time(MBG_DEV_HANDLE dh, PCPS_TIME *p)
Read a PCPS_TIME structure returning the current date/time/status.
Unable to handle requested action, usage printed.
int sn_cpy_str_safe(char *dst, size_t max_len, const char *src)
A function to copy a string safely, returning the number of characters copied.
_MBG_API_ATTR int _MBG_API mbg_get_hr_time(MBG_DEV_HANDLE dh, PCPS_HR_TIME *p)
Read the card's current time with high resolution, including status.
#define mbg_rc_is_success(_rc)
#define MBG_ERR_RANGE
input parameter was out of range
MBG_CHK_SUPP_FNC mbg_chk_dev_has_hr_time
Check if a device supports the mbg_get_hr_time... functions.
#define MBG_FIRST_COPYRIGHT_YEAR
PCPS_TIME_STATUS_X status
status bits, see PCPS_TIME_STATUS_FLAGS
static int set_system_time_from_pcps_time(const PCPS_TIME *p_t)
static MBG_DEV_HANDLER_FNC do_mbgsetsystime
PCPS_FRAC_32 frac
binary fractions of second, see PCPS_FRAC_32
Device has no valid time to set the system time with.
PCPS_TIME_STAMP tstamp
High resolution time stamp (UTC)
int main(int argc, char *argv[])
int8_t offs_utc
[hours], 0 if not _pcps_has_utc_offs
#define MBG_ERR_INV_TIME
The device has no valid time.
#define MBG_SUCCESS
Error codes used with Meinberg devices and drivers.
static __mbg_inline int mbg_gmtime(struct tm *p_tm, const time_t *p_time)
Action failed for specified device.
int mbg_win32_sys_err_to_mbg(DWORD win32_sys_rc, const char *info)
Translate a Windows non-socket API return code to one of the MBG_RETURN_CODES.
static const char * pname
#define MBG_LAST_COPYRIGHT_YEAR
static __mbg_inline uint32_t bin_frac_32_to_dec_frac(uint32_t bin, uint32_t scale)
Convert a 32 bit binary fraction to a scaled decimal.
High resolution time including status and local time offset.
Local calendar date and time, plus sync status.
static int do_set_system_time_from_pcps_time(MBG_DEV_HANDLE dh)
int snprintf_safe(char *s, size_t max_len, const char *fmt,...)
A portable, safe implementation of snprintf()
#define MBG_ERR_OVERFLOW
range or buffer overflow
#define mbg_rc_is_error(_rc)
uint8_t min
minutes, 0..59
const char * mbg_strerror(int mbg_errno)
Return an error string associated with the MBG_ERROR_CODES.
static int sn_printf_timespec(char *s, size_t max_len, const struct timespec *p_ts)
int mbg_get_last_error(const char *info)
Get and translate last error after non-socket function call.
uint8_t month
month, 1..12
PCPS_TIME_STATUS status
status bits, see PCPS_TIME_STATUS_FLAGS_COMMON
time_t pcps_mktime(const PCPS_TIME *tp)
Compute a linear time_t value from PCPS_TIME.
uint8_t sec
seconds, 0..59, or 60 if leap second