Skip to content
Oscar edited this page Sep 9, 2020 · 3 revisions

Welcome to the Almanakka wiki!

Specification 

Purpose

The ALKCore provides the following functionality:

  • Provides Day, Week, Month, Year objects  - Used as units of time to manipulate the date

Scope

The ALKCore provides with an easy-to-use interface for the manipulation of date objects.

  • Easy Initialization of the units
  • Provides convenience method which helps remove the need for boilerplate code.
  • Allow the user to easily compare dates within ranges defined by Date, Week, Month and Year objects.

Models

ALKCore.CalendarComponent (Interface)

  • Base for the unit of time.
  • Take referenceDate for initialization
Functionality
  • Calculate the new unit of time based on Int passed as parameter n. Gives future time unit if n is positive and past time unit if the n is negative.
  • Calculates the number of units(distance) between the self and the unit . The passed unit has to be same unit as the self. We cannot pass other time unit confirming the CalendarComponent.

ALKCore.Day: ALKCore.CalendarComponent

  • A unit of time representing day of calendar.
  • Contains Single date as property
Functionality
  • Confirm if the date occurs within the span with in the span of the self week.

ALKCore.Months: ALKCore.CalendarComponent

  • A unit of time representing day of calendar.
  • Contains array of seven days starting from Sunday to Saturday.
Functionality
  • Gives the information related to public holiday.

ALKCore.Months: ALKCore.CalendarComponent

  • A unit of time representing months of calendar.
  • Contains array of weeks
    • 6 weeks(7x6 = 42 days) by default(Same as the Mac book Calendar App)
    • 5~6weeks when initialized with isExcludingWeeksBeforeAndAfter is set to true. By doing so user excludes the weeks that doesn't have a single day which doesn't belong to current month.
Functionality
  • Confirm if the date occurs within the span with in the span of the self month.
  • Confirm if the week occurs within the span with in the span of the self month. Even if single day of the week occurs with in the month its returns true.

ALKCore.Year: ALKCore.CalendarComponent

  • A unit of time representing year of calendar.
  • Contains months array representing the 12 months of year
Functionality
  • Confirm if the date occurs within the span with in the span of the self year.
  • Confirm if the occurs within the span with in the span of the self year. Even if single day of the week occurs with in the year its returns true.
  • Confirm if the months occurs within the span with in the span of the self year.

Leap year

Reference: https://en.wikipedia.org/wiki/Leap_year

if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 {
    print("Leap year")
}

DateFormatter helper & cache

final class DateFormatterCache {

    static let shared = DateFormatterCache()
    private init() {}

    private let cachedDateFormatterQueue = DispatchQueue(label: "Almanakka.DateFormatterWithCached.String")
    private var cachedDateFormattersOfString = [DateFormat : DateFormatter]()

    //Date Formatter helper 新しいDateFormatterを作り代わりに作ったDateFormatter再使用される。https://github.com/MobileAct/Almanakka-iOS/wiki/_new
    private(set) lazy var dateFormatter: DateFormatter = {
        let formatter = DateFormatter()

        return formatter
    }()

    //Date Formatter cache 
    func cachedDateFormatterOfString(with format: DateFormat) -> DateFormatter {
        return cachedDateFormatterQueue.sync {
            let key = format
            if let cachedFormatter = cachedDateFormattersOfString[key] {
                return cachedFormatter
            }
            let dateFormatter = DateFormatter()
            dateFormatter.calendar = Calendar(identifier: .gregorian)
            dateFormatter.dateFormat = format.rawValue

            cachedDateFormattersOfString[key] = dateFormatter
            return dateFormatter
        }
    }
}

Link: https://juejin.im/post/5c171582518825138d261943 https://developpaper.com/deep-optimization-of-dateformatter/ https://williamboles.me/sneaky-date-formatters-exposing-more-than-you-think/ http://jordansmith.io/performant-date-parsing/