| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "ntpclient.h"
-
- int main(int argc, char* argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <NTP server address>\n", argv[0]);
- return 1;
- }
-
- const char* ntp_server = argv[1];
- NTPClient* client = createNTPClient(ntp_server, NTP_PORT);
- if (!client) {
- fprintf(stderr, "Failed to create NTP client.\n");
- return 1;
- }
-
- time_t current_time;
- if (getCurrentTimeFromNTP(client, ¤t_time) < 0) {
- fprintf(stderr, "Failed to get time from NTP server.\n");
- destroyNTPClient(client);
- return 1;
- }
-
- // 打印获取到的时间
- char time_string[26];
- strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", localtime(¤t_time));
- printf("Current time from NTP server: %s\n", time_string);
-
- char weekday[20];
- getCurrentWeekday(current_time,weekday);
- // 打印获取到的星期
- printf("The day of the week is: %s\n", weekday);
- // 销毁NTP客户端实例
- destroyNTPClient(client);
-
- return 0;
- }
|