Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从一组报警中获取第一个报警时间_C#_Datetime_Time_Uwp_Data Management - Fatal编程技术网

C# 从一组报警中获取第一个报警时间

C# 从一组报警中获取第一个报警时间,c#,datetime,time,uwp,data-management,C#,Datetime,Time,Uwp,Data Management,我目前正在制作一个带有多个(重复出现)闹钟的闹钟 我使用安装了Microsoft IoT的raspberry pi和UWP(C#)作为布局和底层算法 我遇到的问题是检索下一个报警时间 伪代码: Select nextAlarm() For all alarms a if (((a.time >= now.time AND a.repeatDay == now.DayOfWeek) OR a.repeatDay > now.DayOfWeek)

我目前正在制作一个带有多个(重复出现)闹钟的闹钟

我使用安装了Microsoft IoT的raspberry pi和UWP(C#)作为布局和底层算法

我遇到的问题是检索下一个报警时间

伪代码:

Select nextAlarm()
   For all alarms a
       if (((a.time >= now.time AND a.repeatDay == now.DayOfWeek) 
          OR a.repeatDay > now.DayOfWeek) AND a.dateTime < currentAlarm.dateTime)
              currentAlarm = a;
选择nextAlarm()
对于所有报警a
if(((a.time>=now.time和a.repeatDay==now.DayOfWeek)
或者a.repeatDay>now.DayOfWeek)和a.dateTime
但是,每个警报都需要O(n)时间,函数a.repeatDay>now.DayOfWeek不是一个简单的函数(如果当前日期是星期三,下一个警报是星期一,则该函数不起作用)

我想问的是,我如何存储警报,以使上述功能正常工作(最好比O(n)快),或者如何存储所述问题解决后的重复天数

当前正在使用SQLite.net-pcl包

报警和重复日等级:

public class Alarm
{
    [PrimaryKey, AutoIncrement]
    public long Id { get; set; }

    [NotNull]
    public string Name { get; set; }

    [NotNull]
    public DateTime Time { get; set; }

    [NotNull]
    public int Repeat { get; set; }

    public Alarm(string name, DateTime time, RepeatWeek repeat)
    {
        this.Name = name;
        this.Time = time;
        this.Repeat = repeat;
    }

}

public class RepeatWeek
{
    int repeat = 0;
    public static implicit operator int(RepeatWeek w)
    {
        return w.repeat;
    }

    public void setDay(DayOfWeek w)
    {
        repeat |= 1 << (int)w;
    }

    public void removeDay(DayOfWeek w)
    {
        repeat &= ~(1 << (int)w);
    }

    public DayOfWeek getNext(DayOfWeek d, bool inclToday = false)
    {
        throw new NotImplementedException();
        return DayOfWeek.Monday; //Needs work
    }
}    
公共类报警
{
[主密钥,自动增量]
公共长Id{get;set;}
[非空]
公共字符串名称{get;set;}
[非空]
公共日期时间{get;set;}
[非空]
公共整数重复{get;set;}
公共警报(字符串名称、日期时间、RepeatWeek repeat)
{
this.Name=Name;
这个。时间=时间;
这个。重复=重复;
}
}
公共课周
{
int repeat=0;
公共静态隐式运算符int(RepeatWeek w)
{
返回w.重复;
}
公共无效设定日(星期五西)
{

重复|=1我已经试过手动实现GetNextDay。然后实现Alarm.GetNext就变得很简单了,一个更简单的LINQ查询可以满足您的需求。我将其中一些留给您实现,这样您就可以说您做到了

public class Alarm
{
    public long Id { get; set; }

    public string Name { get; set; }

    public DateTime Time { get; set; }

    public int Repeat { get; set; }

    public Alarm(string name, DateTime time, RepeatWeek repeat)
    {
        this.Name = name;
        this.Time = time;
        this.Repeat = repeat;
    }

    public DateTime GetNext()
    {
        var includeToday = true;
        if (DateTime.Now.TimeOfDay > Time.TimeOfDay)
        {
            includeToday = false;
        }

        var repeat = new RepeatWeek(Repeat);
        var nextDayOfWeek = repeat.GetNextDay(includeToday);
        return MergeDayOfWeekAndTime(nextDayOfWeek, Time);
    }

    private DateTime MergeDayOfWeekAndTime(DayOfWeek? nextDayOfWeek, DateTime Time)
    {
        //Left as exercise to the reader.
        throw new NotImplementedException();
    }
}

public class RepeatWeek
{
    int Repeat;

    public RepeatWeek(int repeat = 0)
    {
        Repeat = repeat;
    }

    public static implicit operator int(RepeatWeek w)
    {
        return w.Repeat;
    }

    public void setDay(DayOfWeek w)
    {
        Repeat |= 1 << (int)w;
    }

    public void removeDay(DayOfWeek w)
    {
        Repeat &= ~(1 << (int)w);
    }

    public static DayOfWeek FollowingDayOfWeek(DayOfWeek day)
    {
        if (day == DayOfWeek.Saturday)
        {
            return DayOfWeek.Sunday;
        }
        else
        {
            return day + 1;
        }
    }

    public DayOfWeek? GetNextDay(bool inclToday = false)
    {
        var inspect = DateTime.Now.DayOfWeek;
        if (!inclToday)
        {
            inspect = FollowingDayOfWeek(inspect);
        }

        for (int i = 0; i < 7; i++)
        {
            if ((Repeat & (1 << (int)inspect)) > 0) return inspect;
            inspect = FollowingDayOfWeek(inspect);
        }
        return null;
    }
}

[TestClass]
public class MyTestClass
{
    [TestMethod]
    public void GetNextDayOfWeek()
    {
        var repeat = new RepeatWeek();
        repeat.setDay(DayOfWeek.Monday);
        repeat.setDay(DayOfWeek.Tuesday);
        var expected = DayOfWeek.Monday;
        if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
        {
            expected = DayOfWeek.Tuesday;
        }

        var actual = repeat.GetNextDay();
        Assert.AreEqual(expected, actual);
    }

    [TestMethod]
    public void GetNextAlarm()
    {
        //Populate this yourself.
        var alarms = new List<Alarm>();
        var nextAlarm = alarms.Select(a => a.GetNext()).OrderBy(a => a.Ticks).FirstOrDefault();
    }
}
公共类报警
{
公共长Id{get;set;}
公共字符串名称{get;set;}
公共日期时间{get;set;}
公共整数重复{get;set;}
公共警报(字符串名称、日期时间、RepeatWeek repeat)
{
this.Name=Name;
这个。时间=时间;
这个。重复=重复;
}
公共日期时间GetNext()
{
var includeToday=true;
if(DateTime.Now.TimeOfDay>Time.TimeOfDay)
{
includeToday=假;
}
var repeat=新的RepeatWeek(repeat);
var nextDayOfWeek=repeat.GetNextDay(包括今天);
返回MergeDayOfWeek和Time(下一个星期五,时间);
}
私有日期时间合并DayOfWeeKandTime(DayOfWeek?nextDayOfWeek,日期时间)
{
//留给读者作为练习。
抛出新的NotImplementedException();
}
}
公共课周
{
int重复;
公共重复周(int repeat=0)
{
重复=重复;
}
公共静态隐式运算符int(RepeatWeek w)
{
返回w.重复;
}
公共无效设定日(星期五西)
{
重复|=1 a.Ticks).FirstOrDefault();
}
}

你只想要下一个计划的报警?是的,我现在被困在重复的日子里为什么不只是检查刻度并抓取一个最小值的报警?我知道这种方法适用于单次发生的报警,但是它如何处理重复报警如果你发布了报警类/对象的样子。我可以给你更多的建议手势。接受,它显示了一周中几天的迭代。