Swifty Date & Time API inspired from Java 8 DateTime API.

Background


I think that date & time API should be easy and accurate.

Previous dates, times, timezones API of Swift are inconvenience to use. (It’s very complex to create, manipulate or everything else.)

But there’s quite nice pioneer, The Brand New Time API of Java8.

Java 8 introduced whole new API for handle date & time more efficiently and easy a.k.a LocalDateTime, ZonedDateTime(JSR-310). The main idea that is:

  • Immutable-value classes
  • Domain-driven design
  • Separation of chronologies

Those ideas can be easily ported to another languages, like .Net’s Rx ports of another languages.

So, here’s the AnyDate, whole new Swift date & time API that has coherence between Java 8.

Features


  • Convinience typed year, month, day, time.
  •  Pre-defined Timezone Identifiers.
  •  Null-Safe types.
  •  Separation of chronologies.
  •  Coherence with Java.
  •  Operators supported.
  •  Easy to manipulate.

FAQ


  • Looks like SwiftDate?
    • SwiftDate is a support library for Date, AnyDate can completely replace Date.

Usage


  • Convinience typed year, month, day, time.

/// Before
let now1 = Date()
var calendar = Calendar.current
calendar.timeZone = TimeZone(identifier: “UTC”)!

let day = calendar.components(.day, from: now1)

/// After
let now2 = ZonedDateTime(Clock.utc)
let day = now2.day

  • Pre-defined Timezone Identifiers. String typed timezones are not safe from your TYPING ERROR. (Plz, do not belive your finger. They can always betray you.)

/// Before
let timeZone1 = TimeZone(identifier: “GMT+0900”)!
let timeZone2 = TimeZone(identifier: “America/Argentina/Buenos_Aires”)!

/// After
let clock1 = Clock(offsetHour: 9)
let clock2 = Clock(identifier: .americaArgentinaBuenosAires)

  • Null-Safe types. NO MORE NEEDLESS GUARD & OPTIONAL AND INDENT INCRESEMENT 😀

/// Before
var dateComponents = DateComponents()
dateComponents.year = 2000
dateComponents.month = 11
dateComponents.day = 30
dateComponents.hour = 11
dateComponents.minute = 51
dateComponents.second = 18
dateComponents.nanosecond = 1573

guard let date = Calendar.current.date(from: dateComponents) else {
assertionFailure(“Failed to create!”)
return
}

/// After
let date = LocalDateTime(
year: 2000,
month: 11,
day: 30,
hour: 11,
minute: 51,
second: 18,
nanoOfSecond: 1573
)

  • Operators supported. Easy to compare dates, datetimes, times.

let min = ZonedDateTime.min
let max = ZonedDateTime.max

let oldDate = ZonedDateTime(year: 1627, month: 2, day: 10, hour: 14, minute: 2, second: 18, nanoOfSecond: 1573, clock: .UTC)
let newDate = ZonedDateTime(year: 1627, month: 2, day: 10, hour: 14, minute: 2, second: 18, nanoOfSecond: 1574, clock: .UTC)
let equalDate = ZonedDateTime(year: 1627, month: 2, day: 10, hour: 14, minute: 2, second: 18, nanoOfSecond: 1573, clock: .UTC)

let isLessThan = min < oldDate
let isGreaterThan = max > newDate
let isLessThanOrEqual = oldDate <= equalDate
let isGreaterThanOrEqual = oldDate >= equalDate
let isEqual = oldDate == equalDate
let isLessThan = oldDate < newDate

  • Easy to manipulate. You can use our overridden operators to create / add / sub dates and times.

/// 1000-01-07T11:51:18.000001573
let date = LocalDateTime(
year: 1000,
month: 1,
day: 7,
hour: 11,
minute: 51,
second: 18,
nanoOfSecond: 1573
)
print(date)

/// Period(year: 1, month: 1, day: 9, hour: 2, minute: 3, second: 4, nano: 152)
let period = 1.year + 1.month + 1.week + 2.day + 2.hour + 3.minute + 4.second + 152.nanosecond

/// 1001-03-16T13:54:22.000001725
let newDate = date + period
print(newDate)

Installation


CocoaPods:

pod ‘AnyDate’, ‘~> 1.2.0’

Carthage:

github “kawoou/AnyDate” ~> 1.2.0

Swift Package Manager:

import PackageDescription

let package = Package(
name: “MyAwesomeApp”,
dependencies: [
.Package(url: “https://github.com/kawoou/AnyDate”, majorVersion: 1),
]
)

Manually

You can either simply drag and drop the Sources folder into your existing project.

Requirements


  • Swift 3.1
  • iOS 8.0+
  • tvOS 9.0+
  • macOS 10.10+
  • watchOS 2.0+
  • Virtually any platform which is compatible with Swift 3 and implements the Swift Foundation Library.

GitHub


View Github

#datetime #ios #java #linux #macos #swift #timezone #tvos #watchos
YOU MIGHT ALSO LIKE...
exyte

     

camerakit-ios

CameraKit helps you add reliable camera to your app quickly. Our open source camera platform provides consistent capture results, service ...

HybridCamera

[video width="192" height="416" mp4="https://swiftgit.com/wp-content/uploads/2024/12/68747470733a2f2f7261776769742e636f6d2f7374796c656b69742f696d672f6d61737465722f7669645f6564697465645f325f326d622e676966.mp4"][/video]

TakeASelfie

An iOS framework that uses the front camera, detects your face and takes a selfie. This api opens the front ...

iOS-Depth-Sampler

Code examples of Depth APIs in iOS