Wish Time Library

Discussion in 'B4R Bugs & Wishlist' started by BaGRoS, Sep 5, 2017.

  1. BaGRoS

    BaGRoS Member Licensed User


    Time and time manipulation are very much needed in systems connected to the Internet. Every controller who has to turn something on or off at a specific time or time interval must be able to operate on time. We are a community from all over the world, so time zones also have a big influence.
    Therefore, I believe that the library of this type should be placed as a flagship in the B4R installation.
  2. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that this library supports time zones.
  3. BaGRoS

    BaGRoS Member Licensed User

    No, but support times, dates from second.

    const int timeZone = 1; // Central European Time
    const int timeZone = -5; // Eastern Standard Time (USA)
    const int timeZone = -4; // Eastern Daylight Time (USA)
    const int timeZone = -8; // Pacific Standard Time (USA)
    const int timeZone = -7; // Pacific Daylight Time (USA)


    time_t getNtpTime()
    IPAddress ntpServerIP; // NTP 
    server's ip address
    while (Udp.parsePacket() > 0) ; // discard any previously received packets
    Serial.println("Transmit NTP Request");
    // get a random 
    server from the pool
    WiFi.hostByName(ntpServerName, ntpServerIP);
    Serial.print(": ");
    uint32_t beginWait = millis();
    while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
    Serial.println("Receive NTP Response");
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
    unsigned long secsSince1900;
    // convert four bytes starting at 
    location 40 to a long integer
    secsSince1900 = (unsigned long)packetBuffer[
    40] << 24;
    secsSince1900 |= (unsigned long)packetBuffer[
    41] << 16;
    secsSince1900 |= (unsigned long)packetBuffer[
    42] << 8;
    secsSince1900 |= (unsigned long)packetBuffer[
    return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    Serial.println("No NTP Response :-(");
    return 0; // return 0 if unable to get the time
  4. Erel

    Erel Administrator Staff Member Licensed User

    If you ignore time zone differences, and especially DST effects, then all these calculations become quite simple.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice