-
-
Save 582033/75c37b3db78dad1291bcb14a700cf009 to your computer and use it in GitHub Desktop.
计算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
time1 := 1626205761000 // 毫秒时间1 | |
time2 := 1626205771000 // 毫秒时间2 | |
interval := (time2 - time1 + 999) / 1000 // 计算时间间隔,不足秒的当做秒 | |
fmt.Println(interval) // 输出时间间隔 | |
} | |
type TemperatureData struct { | |
temperature int | |
timestamp int | |
} | |
func main() { | |
// 假设这是连续上报的温度数据 | |
oldData := []TemperatureData{ | |
{temperature: 20, timestamp: 100}, | |
{temperature: 21, timestamp: 400}, | |
{temperature: 22, timestamp: 700}, | |
{temperature: 23, timestamp: 1100}, | |
{temperature: 24, timestamp: 1300}, | |
{temperature: 25, timestamp: 1600}, | |
{temperature: 26, timestamp: 2000}, | |
} | |
// 生成新的温度数据 | |
newData := make([]TemperatureData, 0) | |
lastTemperature := 0 | |
for i := 0; i < 10; i++ { | |
currentTimestamp := i * 1000 | |
for j := 0; j < len(oldData); j++ { | |
if oldData[j].timestamp > currentTimestamp { | |
if j > 0 { | |
lastTemperature = oldData[j-1].temperature | |
} | |
break | |
} else if j == len(oldData)-1 { | |
lastTemperature = oldData[j].temperature | |
} | |
} | |
newData = append(newData, TemperatureData{temperature: lastTemperature, timestamp: currentTimestamp}) | |
} | |
// 输出新的温度数据 | |
for _, data := range newData { | |
fmt.Printf("Temperature: %d, Timestamp: %d\n", data.temperature, data.timestamp) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
有一组连续上报的温度数据,时间间隔几百毫秒至几千毫秒上报一次;
现在需要根据这个上报的温度数据组装一组新的温度数据,长度10,时间间隔是1秒;
根据就数据生成新数据的规则如下:
如果旧数据的时间刚好是1000毫秒,则用旧的温度数据; 如果旧数据的间隔小于1000毫秒,则覆盖上一秒的数据,如果大于1000毫秒则计入下一秒;如果1秒区间内没有数据则取相邻的两个温度的平均值
使用golang实现,温度类型int,时间类型int