项目作者: kawoou

项目描述 :
Swifty Date & Time API inspired from Java 8 DateTime API.
高级语言: Swift
项目地址: git://github.com/kawoou/AnyDate.git
创建时间: 2017-07-16T22:26:55Z
项目社区:https://github.com/kawoou/AnyDate

开源协议:MIT License

下载


AnyDate

Awesome
Swift
Carthage compatible
Version
License
CI Status
Codecov
Platform
Jazzy

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.
  1. /// Before
  2. let now1 = Date()
  3. var calendar = Calendar.current
  4. calendar.timeZone = TimeZone(identifier: "UTC")!
  5. let day = calendar.components(.day, from: now1)
  6. /// After
  7. let now2 = ZonedDateTime(Clock.utc)
  8. 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.)
  1. /// Before
  2. let timeZone1 = TimeZone(identifier: "GMT+0900")!
  3. let timeZone2 = TimeZone(identifier: "America/Argentina/Buenos_Aires")!
  4. /// After
  5. let clock1 = Clock(offsetHour: 9)
  6. let clock2 = Clock(identifier: .americaArgentinaBuenosAires)
  • Null-Safe types. NO MORE NEEDLESS GUARD & OPTIONAL AND INDENT INCRESEMENT :-D
  1. /// Before
  2. var dateComponents = DateComponents()
  3. dateComponents.year = 2000
  4. dateComponents.month = 11
  5. dateComponents.day = 30
  6. dateComponents.hour = 11
  7. dateComponents.minute = 51
  8. dateComponents.second = 18
  9. dateComponents.nanosecond = 1573
  10. guard let date = Calendar.current.date(from: dateComponents) else {
  11. assertionFailure("Failed to create!")
  12. return
  13. }
  14. /// After
  15. let date = LocalDateTime(
  16. year: 2000,
  17. month: 11,
  18. day: 30,
  19. hour: 11,
  20. minute: 51,
  21. second: 18,
  22. nanoOfSecond: 1573
  23. )
  • Operators supported. Easy to compare dates, datetimes, times.
  1. let min = ZonedDateTime.min
  2. let max = ZonedDateTime.max
  3. let oldDate = ZonedDateTime(year: 1627, month: 2, day: 10, hour: 14, minute: 2, second: 18, nanoOfSecond: 1573, clock: .UTC)
  4. let newDate = ZonedDateTime(year: 1627, month: 2, day: 10, hour: 14, minute: 2, second: 18, nanoOfSecond: 1574, clock: .UTC)
  5. let equalDate = ZonedDateTime(year: 1627, month: 2, day: 10, hour: 14, minute: 2, second: 18, nanoOfSecond: 1573, clock: .UTC)
  6. let isLessThan = min < oldDate
  7. let isGreaterThan = max > newDate
  8. let isLessThanOrEqual = oldDate <= equalDate
  9. let isGreaterThanOrEqual = oldDate >= equalDate
  10. let isEqual = oldDate == equalDate
  11. let isLessThan = oldDate < newDate
  • Easy to manipulate. You can use our overridden operators to create / add / sub dates and times.
  1. /// 1000-01-07T11:51:18.000001573
  2. let date = LocalDateTime(
  3. year: 1000,
  4. month: 1,
  5. day: 7,
  6. hour: 11,
  7. minute: 51,
  8. second: 18,
  9. nanoOfSecond: 1573
  10. )
  11. print(date)
  12. /// Period(year: 1, month: 1, day: 9, hour: 2, minute: 3, second: 4, nano: 152)
  13. let period = 1.year + 1.month + 1.week + 2.day + 2.hour + 3.minute + 4.second + 152.nanosecond
  14. /// 1001-03-16T13:54:22.000001725
  15. let newDate = date + period
  16. print(newDate)

Installation

CocoaPods:

  1. pod 'AnyDate', '~> 1.2.0'

Carthage:

  1. github "kawoou/AnyDate" ~> 1.2.0

Swift Package Manager:

  1. import PackageDescription
  2. let package = Package(
  3. name: "MyAwesomeApp",
  4. dependencies: [
  5. .Package(url: "https://github.com/kawoou/AnyDate", majorVersion: 1),
  6. ]
  7. )

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.

Changelog

  • 1.0.0 - 2017/08/13
    • First release AnyDate!
  • 1.0.1 - 2017/08/16
    • Increase test codes.
    • Implement CustomPlaygroundQuickLookable, CustomReflectable protocols.
  • 1.0.2 - 2017/08/26
    • Increase test codes.
    • Support Codable protocol in swift 4.
    • Add operators on Instant.
  • 1.0.3 - 2017/09/03
    • Hotfix, Comparable bugs.
  • 1.0.4 - 2017/10/07
    • Fix Calendar(identifier: .iso8601) crash on swift SR-3828.
  • 1.0.5 - 2018/04/11
    • Support for Swift 4.1 and Xcode 9.3.
  • 1.0.6 - 2018/05/20
    • Support Hashable.
  • 1.1.0 - 2018/10/18
    • Support for Swift 4.2.
  • 1.2.0 - 2019/04/04
    • Support for Swift 5.0.

Author

Special Thanks

  • Naming by 아메바

License

AnyDate is under MIT license. See the LICENSE file for more info.