-
Notifications
You must be signed in to change notification settings - Fork 13
Datetime
In Swift, dates are handled in Date class. But It was not simple enough to use for us. So LeagueAPI uses a new type that wrappes Date type and provide a more user-friendly interface. The wrapped Data variable is still accessible as read-only in Datetime instances.
The new Datetime type exposes computed properties:
- year: Int
- month: Int
- day: Int
- hour: Int
- minute: Int
- second: Int
- monthName: String
- weekday: String
- weekdayIndex: Int
Datetime contains multiple init methods to create instances:
- Datetime()
- Datetime(date: Date)
- Datetime(timestamp: Long)
- Datetime?(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0, timeZone: TimeZone? = nil)
- Datetime?(dateTime: Datetime, timeZone: TimeZone)
- Datetime?(string: String, format: String)
Initialize a new Datetime object with current time.
Initialize a new Datetime object with date time
Initialize a new Datetime object with a number of seconds milliseconds since 01/01/1970.
Datetime?(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0, timeZone: TimeZone? = nil)
Initialize a new Datetime object, specifying date components. Only year, month and day are required.
Initialize a new Datetime object from another datetime, only changing parameter's timezone.
Initialize a new Datetime object from a String corresponding to format format.
format is a String composed of DateToken.Tokens
and respecting valid date rule:
- One Year4Digits ("yyyy")
- One MonthDigits/MonthShort/MonthLong ("MM"/"MS"/"ML")
- One DayDigits ("dd")
- At most one "HourDigits" ("hh")
- At most one "MinuteDigits" ("mm")
- At most one "SecondDigits" ("ss")
Here is a usage example:
Datetime(string: "2018/08/23T00:19:00 (Custom Format)", format: "yyyy/MM/ddThh:mm:ss (Custom Format)")
With dates, a frequent usage is to print it on console. Datetime provide a toString(format: String) method for this. Default format is "yyyy/MM/dd hh:mm:ss", which will be for instance: 2018/08/24 01:05:00. Just like Datetime?(string: String, format: String), you can build a String containing DateToken.Tokens
, with no limitation for occurence number. "yyyyyyyy" would print "20182018" for example. DateToken.Tokens
are String so you can build the String with enum's rawValue but here's the list:
- Year4Digits = "yyyy", year on 4 digits. Example: 2018
- Year2Digits = "yy", year on 2 digits (last digits of Year4Digits). Example: 18
- MonthDigits = "MM", month on 2 digits. Example: 08
- DayDigits = "dd", day on 2 digits. Example: 24
- HourDigits = "hh", hours on 2 digits: Example: 01
- MinuteDigits = "mm", minutes on 2 digits: Example: 05
- SecondDigits = "ss", seconds on 2 digits: Example: 00
- MonthShort = "MS", first three characters of english month's name. Example: Aug
- MonthLong = "ML", english month's name. Example: August
- DayShort = "dS", first three characters of english day of the week name. Example: Fri
- DayLong = "dL", english day of the week name. Example: Friday Any other characters will be in the resulting String.