luke
luke

Reputation: 2773

Displaying text based on what day it is

I'm trying to display the time of something being open based on what day it is. Something like this:

Opening Hours 
**Monday: 8:00-17:00**
Tuesday: 8:00-17:00
Wednesday: 8:00-17:00
Thursday: 8:00-17:00
Friday: 8:00-17:00
Saturday: 8:00-13:00
Sunday: closed

Or simply display

Monday: 8:00-17:00

My assumption would be to use switch statements, but what would I need to do to find out what day it is?

Upvotes: 0

Views: 93

Answers (4)

Mundi
Mundi

Reputation: 80271

Rather than going with switch statements I would prefer a more generic solution. This is also a nice demonstration of leveraging tuples and type aliases for enhancing code expressiveness and readability.

typealias Time = (start: Int, end: Int)
// starting with Sunday
let openTimes: [Time] = [(0,0), (9,17), (9,17), (9,17), (9,17), (9,17), (9,12)]

let flags : NSCalendarUnit = [.Hour, .Weekday]

func isOpenAtTime(date: NSDate) -> Bool {
    let time = NSCalendar.currentCalendar().components(flags, fromDate: date)
    let openingHours = openTimes[time.weekday - 1]
    let hour = time.hour
    return hour >= openingHours.start && hour <= openingHours.end
}

You might want to handle a few edge cases as well, but you get the idea.
You could make this work with more granular time by using minutes instead of hours.

Upvotes: 0

dfrib
dfrib

Reputation: 73206

You can make use of NSCalendar to get the .Weekday unit as an integer (Sunday through Saturday as 1 ... 7 for the Gregorian calendar).

Given you know the day of the week represented as an Int, rather than using a switch statement, you could use a [Int: String] dictionary for the different opening hours.

let calendar = NSCalendar.currentCalendar()
let today = calendar.component(.Weekday, fromDate: NSDate())
    // Gregorian calendar: sunday = 0, monday = 1, ...

let openingHours: [Int: String] = [1: "Sunday: closed", 2: "Monday: 8:00-17:00", 3: "Tuesday: 8:00-17:00"] // ...

print("Opening hours:\n\(openingHours[today] ?? "")")  
/* Opening hours:
   Monday: 8:00-17:00 */

Another alternative is to create a computed property extension to NSDate() that returns the current weekday as a String

extension NSDate {
    var weekday : String {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "EEEE"
        return formatter.stringFromDate(self)
    }
}

This can be readily used with a [String: String] dictionary for holding the set of weekday : opening hours:

/* example usage */
let openingHours: [String: String] =
    ["Sunday": "closed",
     "Monday": "8:00-17:00",
     "Tuesday": "8:00-17:00"] // ...

let today = NSDate().weekday
print("Opening hours:\n\(today): \(openingHours[today] ?? "")")
/* Opening hours:
   Monday: 8:00-17:00 */

Upvotes: 1

brduca
brduca

Reputation: 3623

Another solution could be:

import Foundation

let today = NSDate()
let dateFormatter = NSDateFormatter()
let currentDay = NSCalendar.currentCalendar().component(.Weekday, fromDate:today);

dateFormatter.dateFormat = "EEEE"
let dayOfWeekString = dateFormatter.stringFromDate(today)

switch currentDay
{
case 2,3,4,5:
    print("\(dayOfWeekString): 8:00 - 17:00")

case 6:
    print("\(dayOfWeekString): 8:00 - 13:00")

default:
    print("\(dayOfWeekString): closed")
}

Upvotes: 1

sergio
sergio

Reputation: 69047

You can use component(_:fromDate:) to get the week day from the current date. That would look like:

let currentDay = NSCalendar.currentCalendar().component(.Weekday, fromDate:NSDate());

Based on the value you get for currentDay, you can provide the correct opening hours.

Upvotes: 1

Related Questions