git.iko.soy/dashared/hookah-tau
on develop help sign in
68 lines 2.5 kB history raw
1//
2// HFunc.swift
3// Hookah Tau
4//
5// Created by cstore on 02/12/2019.
6// Copyright © 2019 Daria Rednikina. All rights reserved.
7//
9import Foundation
12class HFunc {
14 private init() {}
16 static let main = HFunc()
18 func exportClientsToCSV(_ arr: [Client]) -> String {
19 return arr.reduce(Client.csvHeading) { return funcToMapClientsIntoString($0, $1) }
20 }
22 /// Filtering function to get every periods which intersects with given date
23 /// - Parameter periods: all periods of bookings (for a particular table)
24 /// - Parameter today: date
25 func filterPeriodsInDate(_ periods: [ReservationPeriod], _ today: Date) -> [ReservationPeriod] {
26 return periods.filter(filterPeriod(today))
27 }
30 /// Filtering func to get every intersecting period for the table. Useful for 2nd booning screen
31 /// - Parameter periods: Periods for the table
32 /// - Parameter start: for client 12:00, for admin t - 24h
33 /// - Parameter end: for client 3:00, for admin t + 24h
34 func filterPeriodsStartEndDates(_ periods: [ReservationPeriod], _ start: Date, _ end: Date) -> [ReservationPeriod] {
35 return periods.filter(filterInInterval(start, end))
36 }
38 private func filterPeriod(_ today: Date) -> ((ReservationPeriod) -> Bool) {
40 func datePredicate(_ start: Date, _ end: Date, _ current: Date) -> Bool {
41 var calendar = Calendar.current
42 calendar.timeZone = TimeZone(identifier: "GMT")!
44 let result = calendar.isDate(start, equalTo: current, toGranularity: .day)
45 || calendar.isDate(end, equalTo: current, toGranularity: .day)
47 return result
48 }
50 return { (period: ReservationPeriod) in datePredicate(period.startTime, period.endTime, today) }
51 }
53 private func filterInInterval(_ start: Date, _ end: Date) -> ((ReservationPeriod) -> Bool) {
55 let dateInterval = DateInterval(start: start, end: end)
56 func intervalChecking(_ sp: Date, _ ep: Date) -> Bool {
57 let innerInterval = DateInterval(start: sp, end: ep)
59 return dateInterval.intersects(innerInterval)
60 }
62 return { (period: ReservationPeriod) in intervalChecking(period.startTime, period.endTime) }
63 }
65 private func funcToMapClientsIntoString(_ acc: String, _ el: Client) -> String {
66 return acc + el.toString + "\n"
67 }