ntpTest.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "ntpclient.h"
  4. int main(int argc, char* argv[]) {
  5. if (argc != 2) {
  6. fprintf(stderr, "Usage: %s <NTP server address>\n", argv[0]);
  7. return 1;
  8. }
  9. const char* ntp_server = argv[1];
  10. NTPClient* client = createNTPClient(ntp_server, NTP_PORT);
  11. if (!client) {
  12. fprintf(stderr, "Failed to create NTP client.\n");
  13. return 1;
  14. }
  15. time_t current_time;
  16. if (getCurrentTimeFromNTP(client, &current_time) < 0) {
  17. fprintf(stderr, "Failed to get time from NTP server.\n");
  18. destroyNTPClient(client);
  19. return 1;
  20. }
  21. // 打印获取到的时间
  22. char time_string[26];
  23. strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", localtime(&current_time));
  24. printf("Current time from NTP server: %s\n", time_string);
  25. char weekday[20];
  26. getCurrentWeekday(current_time,weekday);
  27. // 打印获取到的星期
  28. printf("The day of the week is: %s\n", weekday);
  29. // 销毁NTP客户端实例
  30. destroyNTPClient(client);
  31. return 0;
  32. }