简体   繁体   English

如何跟踪随日期变化的数据?

[英]How can I keep track of data moving with dates?

I have a school app that keeps track of your schedule. 我有一个学校应用程序,可以跟踪您的日程安排。 The app has a multitude of options for inputting the schedule. 该应用程序具有输入时间表的众多选项。 If the user has a four day schedule, then they will have AD days. 如果用户有四天的时间表,那么他们将有AD天。 For my app. 对于我的应用程序。 I'd like to keep track of what the current day is (for instance, whether or not it is an A day vs. a C day). 我想跟踪当前的日期(例如,是否是A天还是C天)。 The user will be able to specify what day it is when they set up the app, but then the app should keep track of this change day to day, minus weekends. 用户可以在设置应用程序时指定当日,但是应用程序应每天(不包括周末)跟踪此更改。 I'm not sure what the best way to implement this is. 我不确定实现此目标的最佳方法是什么。

If the configuration allows completely arbitrary day assignments, you would only need a simple dictionary in the form [Date:String]. 如果配置允许完全任意的日分配,则只需要一个简单的字典,形式为[Date:String]。

If you want to let your users specify repeating patterns of day identifiers (letters), the configuration parameters and process will need to be more sophisticated and will depend on how far you're willing to go in processing exceptions (ie non school days). 如果要让用户指定重复的日期标识符(字母)模式,则配置参数和过程将需要更复杂,并且取决于您愿意处理异常情况的时间(即非上课日)。 Weekends can be automatic but holidays and scheduled "off days" would require that you define some kind of rule (hard coded or user configurable). 周末可以是自动的,但假期和计划的“休息日”将要求您定义某种规则(硬编码或用户可配置)。

In all cases, I would suggest creating a function that converts any date into its day identifier based on the configured parameters. 在所有情况下,我建议创建一个函数,该函数根据配置的参数将任何日期转换为日期标识符。 Depending on the complexity of the rules and the range of dates you are working with, it may be a good idea to dynamically build a global dictionary of date identifiers (eg [Date:String]) and only compute identifiers once per date. 根据规则的复杂性和您使用的日期范围,最好动态创建一个全局的日期标识符字典(例如[Date:String]),每个日期只计算一次标识符。

For example: 例如:

// your configuration parameters could be something like this:

let firstSchoolDay:Date     = // read from configuration
let lastSchoolDay           = // read from configuration
let dayIdentifiers:[String] = // read from configuration 
let skippedDates:Set<Date>  = // read from configuration 

// The global dictionary and function could work like this:

var dayIdentifiers:[Date:Sting] = [:] // global scope (or singleton)
func dayIdentifier(for date:Date) -> String
{
    if let dayID = dayIdentifiers[date]
    { return dayID  }

    let dayID:String = // compute your day ID once according to parameters.
                       // you could use an empty string as a convention for
                       // non school days

                       // a simple way to compute this is to start from
                       // the last computed date (or firstSchoolDay)
                       // and move forward using Calendar.enumerateDates
                       // applying the weekend and offdays rules
                       // and saving day identifiers as you
                       // move forward up to the requested date

    dayIdentifiers[date] = dayID
    return dayID         
}

I figured out how to keep track of the moving dates, while also not accounting for weekends and updating the value every time it is checked. 我想出了如何跟踪移动日期,同时也没有考虑周末和每次检查时都更新值。

func getCurrentDay() -> Int {

    let lastSetDay = //Get the last value of the set day
    let lastSetDayDate = //Get the last time the day was set
    var numberOfDays: Int! //Calculate number of recurring days, for instance an A, B, C, D day schedule would be 4

    var currentDay = lastSetDay
    var currentIteratedDate = lastSetDayDate

    while Calendar.current.isDate(currentIteratedDate, inSameDayAs: Date()) == false {
        currentIteratedDate = Calendar.current.date(byAdding: .day, value: 1, to: currentIteratedDate)!
        if !Calendar.current.isDateInWeekend(currentIteratedDate) {
            currentDay += 1
        }
        if currentDay > numberOfDays {
            currentDay = 1
        }
    }

    if Calendar.current.isDate(currentIteratedDate, inSameDayAs: Date()) == false {
        //Save new day
        //Save new date
    }

    return currentDay
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM