Qt5使用NTP服务器时间的嵌入式设备的夏令时

时间:2014-10-29 11:43:52

标签: android qt timezone dst

我试图在我正在处理的应用中考虑夏令时,但它无法正常工作。该应用程序本身在基于ARM的主板上运行,没有系统电池以保持时间,所以我正在从NTP服务器读取时间。

现在在Qt5中使用此设备在设备上运行的测试应用程序:

#include <QCoreApplication>
#include <QTimeZone>

#include <QDebug>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory.h>

time_t getNTPTime()
{
    char host[] = "192.168.1.16";

    qDebug() << "NTP request from host" << host;

    int     socket_descriptor = socket( AF_INET, SOCK_DGRAM, 0 );
    if ( socket_descriptor < 0 ) return 0;

    struct sockaddr_in client_addr;
    memset( &client_addr, 0, sizeof(client_addr) );
    client_addr.sin_family = AF_INET;
    client_addr.sin_addr.s_addr = htonl( INADDR_ANY );
    client_addr.sin_port = htons( 0 );

    if ( bind( socket_descriptor, (struct sockaddr*)&client_addr, sizeof(client_addr)) < 0 )
        return 0;

    struct sockaddr_in server_addr;
    memset( &server_addr, 0, sizeof(server_addr) );
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr( host );
    server_addr.sin_port = htons( 123 );

    char buf[1024];
    memset( buf, 0, 1024 );

    char request[48];
    memset( request, 0, 48 );
    request[0] = 0x1b;

    if ( sendto( socket_descriptor, request, 48, 0, (struct sockaddr*)&server_addr, sizeof(server_addr) ) < 0 )
        return 0;

    struct sockaddr_in recv_addr;
    socklen_t recv_addr_len = sizeof( recv_addr );
    int rcvd = recvfrom( socket_descriptor, buf, 1024, 0, (struct sockaddr*)&recv_addr,&recv_addr_len );

    qDebug() << rcvd << "bytes received from NTP server <-- NTP REQUEST";

    unsigned long ntpTime = ( buf[40] << 24 ) + ( buf[41] << 16 ) +
                            ( buf[42] << 8  ) +   buf[43];

    ntpTime -= 2208988800UL;    // take off 70 years

    qDebug() << "ntpTime =" << ntpTime;

    return ntpTime;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    time_t ntptime = getNTPTime();
    QDateTime time;

    qDebug() << "System time zone" << QTimeZone::availableTimeZoneIds();

    QTimeZone qZone( "UTC+00:00" );
    time.setTimeSpec( Qt::TimeZone );
    time.setTimeZone( qZone );
    time.setTime_t( ntptime );

    //time.addSecs( 3600 );

    qDebug() << time.toString();

    return a.exec();
}

在调试输出中给出了一个可用于arm设备的时区列表:

D/Qt      ( 4701): ..\TimeZoneTest\main.cpp:15 (time_t getNTPTime()): NTP request from host 192.168.1.16
D/Qt      ( 4701): ..\TimeZoneTest\main.cpp:49 (time_t getNTPTime()): 48 bytes received from NTP server <-- NTP REQUEST
D/Qt      ( 4701): ..\TimeZoneTest\main.cpp:56 (time_t getNTPTime()): ntpTime = 1414581684
D/Qt      ( 4701): ..\TimeZoneTest\main.cpp:68 (int main(int, char**)): System time zone ("UTC", "UTC+00:00", "UTC+01:00", "UTC+02:00", "UTC+03:00", "UTC+03:30", "UTC+04:00", "UTC+04:30", "UTC+05:00", "UTC+05:30", "UTC+05:45", "UTC+06:00", "UTC+06:30", "UTC+07:00", "UTC+08:00", "UTC+09:00", "UTC+09:30", "UTC+10:00", "UTC+11:00", "UTC+12:00", "UTC+13:00", "UTC+14:00", "UTC-00:00", "UTC-01:00", "UTC-02:00", "UTC-03:00", "UTC-03:30", "UTC-04:00", "UTC-04:30", "UTC-05:00", "UTC-06:00", "UTC-07:00", "UTC-08:00", "UTC-09:00", "UTC-10:00", "UTC-11:00", "UTC-12:00", "UTC-13:00", "UTC-14:00")
D/Qt      ( 4701): ..\TimeZoneTest\main.cpp:77 (int main(int, char**)): "Wed Oct 29 11:21:24 2014 GMT"

所以目前我仍然坚持如何实施夏令时。

我在本周日结束后得出结论,我正在处理的主要应用程序没有对时间进行任何更改。与上述输出一样,GMT设备没有可用的时区设置。目前我可能需要检查应用是否在DST日期范围内并手动调整。

有人知道如何在Qt5中为嵌入式设备使用夏令时吗?或者这是否意味着手动解决它?

0 个答案:

没有答案