void getCurrentDate(DateTime *date) { //Retrieve current time time_t time = getCurrentUnixTime(); //Convert Unix timestamp to date convertUnixTimeToDate(time, date); }
error_t sntpClientTest(void) { error_t error; time_t unixTime; IpAddr ipAddr; NtpTimestamp timestamp; DateTime date; //Debug message TRACE_INFO("\r\n\r\nResolving server name...\r\n"); //Resolve SNTP server name error = getHostByName(NULL, "0.fr.pool.ntp.org", &ipAddr, 0); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to resolve server name!\r\n"); //Exit immediately return error; } //Debug message TRACE_INFO("Requesting time from SNTP server %s\r\n", ipAddrToString(&ipAddr, NULL)); //Retrieve current time from NTP server using SNTP protocol error = sntpClientGetTimestamp(NULL, &ipAddr, ×tamp); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to retrieve NTP timestamp!\r\n"); } else { //Unix time starts on January 1st, 1970 unixTime = timestamp.seconds - 2208988800; //Convert Unix timestamp to date convertUnixTimeToDate(unixTime, &date); //Debug message TRACE_INFO("Current date/time: %s\r\n", formatDate(&date, NULL)); //Move cursor lcdSetCursor(8, 0); //Refresh LCD display printf("%04u/%02u/%02u %02u:%02u:%02u", date.year, date.month, date.day, date.hours, date.minutes, date.seconds); } //Return status code return error; }
void RTC_CalendarConfig(time_t unixtime) { DateTime date; RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; convertUnixTimeToDate(unixtime, &date); /*##-1- Configure the Date #################################################*/ /* Set Date: Tuesday February 18th 2014 */ sdatestructure.Year = (uint8_t)(date.year - 2000); sdatestructure.Month = date.month; sdatestructure.Date = date.day; sdatestructure.WeekDay = date.dayOfWeek; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Time #################################################*/ /* Set Time: 02:00:00 */ stimestructure.Hours = date.hours; stimestructure.Minutes = date.minutes; stimestructure.Seconds = date.seconds; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&RtcHandle, &stimestructure, FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Writes a data in a RTC Backup data Register0 #######################*/ HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0x32F2); }
error_t httpServerCgiCallback(HttpConnection *connection, const char_t *param) { static uint_t pageCounter = 0; uint_t length; time_t unixTime; DateTime date; //Underlying network interface NetInterface *interface = connection->socket->interface; //Check parameter name if(!strcasecmp(param, "PAGE_COUNTER")) { pageCounter++; sprintf(connection->buffer, "%u time%s", pageCounter, (pageCounter >= 2) ? "s" : ""); } else if(!strcasecmp(param, "BOARD_NAME")) { strcpy(connection->buffer, interface->hostname); } else if(!strcasecmp(param, "SYSTEM_TIME")) { memset(&connection->buffer, 0x00, HTTP_SERVER_BUFFER_SIZE ); // unixTime = RTC_GetCounter(); unixTime = 0; //Convert Unix timestamp to date convertUnixTimeToDate(unixTime, &date); formatDate(&date, connection->buffer); } else if(!strcasecmp(param, "MAC_ADDR")) { macAddrToString(&interface->macAddr, connection->buffer); } else if(!strcasecmp(param, "IPV4_ADDR")) { ipv4AddrToString(interface->ipv4Config.addr, connection->buffer); } else if(!strcasecmp(param, "SUBNET_MASK")) { ipv4AddrToString(interface->ipv4Config.subnetMask, connection->buffer); } else if(!strcasecmp(param, "DEFAULT_GATEWAY")) { ipv4AddrToString(interface->ipv4Config.defaultGateway, connection->buffer); } else if(!strcasecmp(param, "IPV4_PRIMARY_DNS")) { ipv4AddrToString(interface->ipv4Config.dnsServer[0], connection->buffer); } else if(!strcasecmp(param, "IPV4_SECONDARY_DNS")) { ipv4AddrToString(interface->ipv4Config.dnsServer[1], connection->buffer); } #if (IPV6_SUPPORT == ENABLED) else if(!strcasecmp(param, "LINK_LOCAL_ADDR")) { ipv6AddrToString(&interface->ipv6Config.linkLocalAddr, connection->buffer); } else if(!strcasecmp(param, "GLOBAL_ADDR")) { ipv6AddrToString(&interface->ipv6Config.globalAddr, connection->buffer); } else if(!strcasecmp(param, "IPV6_PREFIX")) { ipv6AddrToString(&interface->ipv6Config.prefix, connection->buffer); length = strlen(connection->buffer); sprintf(connection->buffer + length, "/%u", interface->ipv6Config.prefixLength); } else if(!strcasecmp(param, "ROUTER")) { ipv6AddrToString(&interface->ipv6Config.router, connection->buffer); } else if(!strcasecmp(param, "IPV6_PRIMARY_DNS")) { ipv6AddrToString(&interface->ipv6Config.dnsServer[0], connection->buffer); } else if(!strcasecmp(param, "IPV6_SECONDARY_DNS")) { ipv6AddrToString(&interface->ipv6Config.dnsServer[1], connection->buffer); } #endif else { return ERROR_INVALID_TAG; } //Get the length of the resulting string length = strlen(connection->buffer); //Send the contents of the specified environment variable return httpWriteStream(connection, connection->buffer, length); }