-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.swift
executable file
·117 lines (93 loc) · 3.77 KB
/
Time.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Time.swift
// UniversityRating
//
// Created by Izzy on 06/01/2017.
// Copyright © 2017 Izzy. All rights reserved.
//
import UIKit
struct Time {
// No stored properrties needed
//MARK: Time Manipulation Methods
/**
A Method that returnns the time for the start of the current day, Monday Midnight for example if it was monday
- returns: Date() obj
*/
func startOfToday() -> Date {
let date = Date()
let cal = Calendar(identifier: .gregorian)
let midnightDate = cal.startOfDay(for: date)
// print("\(midnightDate)")
return midnightDate
}
/**
Calculates the number of days between two given dates, this method is primarily used to check
that the number of days given for feedback exceeds a certain amount in order to facilate the reset mechanic
for the feedback every Monday.
- returns: Int (number of days)
**/
func numberOfDaysPassedBetween(_ startDate: Date, AndEndDate endDate: Date) -> Int {
let calendar = Calendar.current
let components = calendar.dateComponents([Calendar.Component.day], from: startDate, to: endDate)
print(components.day!)
return components.day!
}
/**
Loops through the list of available timestamps from the university model and converts the String equivilant of
the date into a Date obj in order to be able use it for calculations for the number of days passed since feedback method.
If no timestamps have been set then the current date will be returned
- returns: Date() obj, converted from a String representation of the universities module timestamp
*/
func lastSyncedTime() -> Date {
let currentDate = Date()
for index in UniversityModel.sharedIntstance.universityArray {
let university = index as! University
if !university.timestamp.isEmpty {
return convertStringtoDate(timestamp: university.timestamp)
}
}
return currentDate //default
}
/**
Converts any String timestamp that is set to the default format found in Date() and returns a Date() obj
- returns: Date() obj
*/
func convertStringtoDate(timestamp: String) -> Date{
let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ssZZZZ"
if let timestampDate = dateFormatter.date(from:timestamp) {
return timestampDate
}
return currentDate
}
//MARK: Reset Methods
/**
Method that returns the day of the week as an optional Int, sunday = 1 ... satruday = 7
- returns: Optional Int
*/
func getDayOfWeek()->Int? {
let weekday = Calendar.current.component(.weekday, from: Date())
let day = Day(rawValue: weekday)!
switch (day) {
case .monday where numberOfDaysPassedBetween(lastSyncedTime(), AndEndDate: startOfToday()) > 0:
print("Monday Found/ Feedback given must be greater then one day")
// let hasFeedbackBeenRecieved = UserDefaults.standard.bool(forKey: "feedbackSubmitted")
// print("FeedbackSubmitted: \(hasFeedbackBeenRecieved)")
// if hasFeedbackBeenRecieved {
// print("In here!")
UniversityModel.sharedIntstance.reset()
// }
default:
// print("Not a Weekday we care about: No Reset necessary")
break
}
return weekday
}
/**
Enum for the dates of the week
*/
enum Day: Int {
case sunday = 1, monday, tuesday, wednesday, thrusday, friday, saturday
}
}