RealDateTimes GetClosestDate() { DateTime currDate; RealDateTimes closestDate = null; DateTime todayis = DateTime.Now; currDate = new DateTime(2016, 1, 1, todayis.Hour, todayis.Minute, 0); //Puts(string.Format("Current time: {0}:{1}", todayis.Hour, todayis.Minute)); var first = RealDateTimesDic.ElementAt(0); long min = Math.Abs((long)(currDate - first.Value.realdtime).TotalSeconds); long diff = 0; closestDate = first.Value; foreach (var data in RealDateTimesDic) { DateTime date = data.Value.realdtime; diff = Math.Abs((long)(currDate - date).TotalSeconds); //Puts(string.Format("{0}:{1} - diff {2}", date.Hour, date.Minute, diff)); if (diff < min) { min = diff; closestDate = data.Value; } } //Puts("Chosen: {0}:{1} with diff {2}", closestDate.realdtime.Hour, closestDate.realdtime.Minute, diff); return(closestDate); }
void setThisTime(RealDateTimes rdt) { int needc = ((10000 / 60) * rdt.virtualminutes); float stime = float.Parse(string.Format("{0}.{1}", rdt.virtualhour, needc)); EnvironmentControlCenter.Singleton.SetTime(stime); }
void Init() { // realtime.json time CheckCfg <Dictionary <string, Dictionary <string, int> > >("RealTime: Time table", ref TimeTable); CheckCfg <float>("RealTime: Sync time in seconds", ref synceveryxsec); CheckCfg <string>("RealTime: ChatTag", ref ChatTag); SaveConfig(); // let's add everything to dictionary if (TimeTable.Any()) { foreach (var WhatTime in TimeTable) { if (RealDateTimesDic.ContainsKey(WhatTime.Key)) { Puts(string.Format("{0} is repeatable, skipping, make sure to fix your config")); continue; } else { // It's better in this way string name = WhatTime.Key.ToString(); Dictionary <string, int> info = WhatTime.Value; // Let's do some checks, because some people are ... if (info["RealHour"] > 23 || info["RealHour"] < 0) { Puts(string.Format("{0} - RealHour is out of bounds ({1}) it must be between 0-23", name, info["RealHour"])); continue; } else if (info["RealMinute"] > 59 || info["RealMinute"] < 0) { Puts(string.Format("{0} - RealMinute is out of bounds ({1}) it must be between 0-59", name, info["RealMinute"])); continue; } else if (info["VirtualHour"] > 23 || info["VirtualHour"] < 0) { Puts(string.Format("{0} - VirtualHour is out of bounds ({1}) it must be between 0-23", name, info["VirtualHour"])); continue; } else if (info["VirtualMinute"] > 59 || info["VirtualMinute"] < 0) { Puts(string.Format("{0} - VirtualHour is out of bounds ({1}) it must be between 0-59", name, info["VirtualMinute"])); continue; } // Finally let's make some progress RealDateTimes new_rdt = new RealDateTimes(info["RealHour"], info["RealMinute"], info["VirtualHour"], info["VirtualMinute"]); RealDateTimesDic.Add(name, new_rdt); } } synctimer = timer.Repeat(synceveryxsec, 0, () => { RealDateTimes rdt = GetClosestDate(); //Puts(string.Format("{0}:{1}", rdt.virtualhour, rdt.virtualminutes)); if (rdt != null) { setThisTime(rdt); } else { Puts("Failure, date could not be set for some weird reason?>"); } }); } else { Puts("Fix your config, it doesn't contain a TimeTable :)"); } }