Giter Site home page Giter Site logo

timepiece's Introduction

Timepiece CocoaPods Compatible Build Status

Intuitive date handling in Swift

Features

  • 💡 Intuitive: Timepiece provides a set of helpers to make date handling easier.
  • 🕘 Correct: Using Foundation API correctly, Timepiece helps to calculate dates correctly without deep understanding.
  • 📦 Small: Timepiece has only 4 file. You can read the inside of this easily.

Requirements

  • Swift 3.0+
  • iOS 8.0+ / macOS 10.9+ / tvOS 9.0+ / watchOS 2.0+

Usage

Initialization

Date(era: 235, year: 26, month: 8, day: 14, hour: 20, minute: 25, second: 43, nanosecond: 0, on: Calendar(identifier: .japanese))
Date(year: 2014, month: 8, day: 14, hour: 20, minute: 25, second: 43, nanosecond: 0)
Date(year: 2014, month: 8, day: 14, hour: 20, minute: 25, second: 43)
Date(year: 2014, month: 8, day: 14)

Date.today()
Date.yesterday()
Date.tomorrow()

Calculation

now + 1.year
now - 2.months
now + (3.weeks - 4.days + 5.hours)

1.year.later
1.year.ago

Change

now.changed(year: 2014)
now.changed(weekday: 1)
now.truncated([.minute, .second, .nanosecond])
now.truncated(from: .day)

Formating

now.stringIn(dateStyle: .long, timeStyle: .medium)
now.dateString(in: .medium)
now.timeString(in: .short)

3.days.string(in: .full)

Parsing

"2014/8/14".date(inFormat: "yyyy/MM/dd")
"2014-08-14T20:25:43+0900".dateInISO8601Format()

Installation

CocoaPods

# Podfile
pod "Timepiece"

Carthage

# Cartfile
github "naoty/Timepiece"

Contribution

  1. Fork
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

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

Author

naoty

timepiece's People

Contributors

basememara avatar basthomas avatar cbeloch avatar dani-mp avatar dcordero avatar irlabs avatar mattijsf avatar naoty avatar nickygerritsen avatar skofgar avatar woxtu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

timepiece's Issues

NSDate.beginningOfHour is not compatible with Daylight Saving Time.

Sample code is following:

//: Playground - noun: a place where people can play

import Foundation
import Timepiece

let timeZone = NSTimeZone(name: "Australia/Adelaide")!
timeZone.daylightSavingTime // true
NSTimeZone.setDefaultTimeZone(timeZone)

let calendar = NSCalendar.currentCalendar()

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

// This Year
let comp = calendar.components(.CalendarUnitEra | .CalendarUnitYear, fromDate: NSDate())
let startOfThisYear = calendar.dateFromComponents(comp)!

var prevDate = startOfThisYear
// transition
while let transition = timeZone.nextDaylightSavingTimeTransitionAfterDate(prevDate)
    where calendar.isDate(transition, equalToDate: startOfThisYear, toUnitGranularity: .CalendarUnitYear) {

        println("transition: \(formatter.stringFromDate(transition))(\(transition))")

        let twoHourBefore = transition - 2.hours
        let twoHourAfter = transition + 2.hours

        var time = twoHourBefore
        while time <= twoHourAfter {

            let beginningOfHour = time.beginningOfHour

            // check equality by hour
            if calendar.isDate(time, equalToDate: beginningOfHour, toUnitGranularity: .CalendarUnitHour) {
                println("\(formatter.stringFromDate(time))(\(time)) is in \(formatter.stringFromDate(beginningOfHour))(\(beginningOfHour))")
            } else {
                println("\(formatter.stringFromDate(time))(\(time)) is not in \(formatter.stringFromDate(beginningOfHour))(\(beginningOfHour))")
            }

            time = time + 5.minutes
        }

        prevDate = transition
}

result is following:

transition: 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:05:00 +1030(2015-04-04 14:35:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:10:00 +1030(2015-04-04 14:40:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:15:00 +1030(2015-04-04 14:45:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:20:00 +1030(2015-04-04 14:50:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:25:00 +1030(2015-04-04 14:55:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:30:00 +1030(2015-04-04 15:00:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:35:00 +1030(2015-04-04 15:05:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:40:00 +1030(2015-04-04 15:10:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:45:00 +1030(2015-04-04 15:15:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:50:00 +1030(2015-04-04 15:20:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 01:55:00 +1030(2015-04-04 15:25:00 +0000) is in 2015-04-05 01:00:00 +1030(2015-04-04 14:30:00 +0000)
2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:05:00 +1030(2015-04-04 15:35:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:10:00 +1030(2015-04-04 15:40:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:15:00 +1030(2015-04-04 15:45:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:20:00 +1030(2015-04-04 15:50:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:25:00 +1030(2015-04-04 15:55:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:30:00 +1030(2015-04-04 16:00:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:35:00 +1030(2015-04-04 16:05:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:40:00 +1030(2015-04-04 16:10:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:45:00 +1030(2015-04-04 16:15:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:50:00 +1030(2015-04-04 16:20:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:55:00 +1030(2015-04-04 16:25:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:05:00 +0930(2015-04-04 16:35:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:10:00 +0930(2015-04-04 16:40:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:15:00 +0930(2015-04-04 16:45:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:20:00 +0930(2015-04-04 16:50:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:25:00 +0930(2015-04-04 16:55:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:30:00 +0930(2015-04-04 17:00:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:35:00 +0930(2015-04-04 17:05:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:40:00 +0930(2015-04-04 17:10:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:45:00 +0930(2015-04-04 17:15:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:50:00 +0930(2015-04-04 17:20:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:55:00 +0930(2015-04-04 17:25:00 +0000) is not in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:05:00 +0930(2015-04-04 17:35:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:10:00 +0930(2015-04-04 17:40:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:15:00 +0930(2015-04-04 17:45:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:20:00 +0930(2015-04-04 17:50:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:25:00 +0930(2015-04-04 17:55:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:30:00 +0930(2015-04-04 18:00:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:35:00 +0930(2015-04-04 18:05:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:40:00 +0930(2015-04-04 18:10:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:45:00 +0930(2015-04-04 18:15:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:50:00 +0930(2015-04-04 18:20:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 03:55:00 +0930(2015-04-04 18:25:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
2015-04-05 04:00:00 +0930(2015-04-04 18:30:00 +0000) is in 2015-04-05 04:00:00 +0930(2015-04-04 18:30:00 +0000)
transition: 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:05:00 +0930(2015-10-03 14:35:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:10:00 +0930(2015-10-03 14:40:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:15:00 +0930(2015-10-03 14:45:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:20:00 +0930(2015-10-03 14:50:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:25:00 +0930(2015-10-03 14:55:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:30:00 +0930(2015-10-03 15:00:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:35:00 +0930(2015-10-03 15:05:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:40:00 +0930(2015-10-03 15:10:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:45:00 +0930(2015-10-03 15:15:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:50:00 +0930(2015-10-03 15:20:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 00:55:00 +0930(2015-10-03 15:25:00 +0000) is in 2015-10-04 00:00:00 +0930(2015-10-03 14:30:00 +0000)
2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:05:00 +0930(2015-10-03 15:35:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:10:00 +0930(2015-10-03 15:40:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:15:00 +0930(2015-10-03 15:45:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:20:00 +0930(2015-10-03 15:50:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:25:00 +0930(2015-10-03 15:55:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:30:00 +0930(2015-10-03 16:00:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:35:00 +0930(2015-10-03 16:05:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:40:00 +0930(2015-10-03 16:10:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:45:00 +0930(2015-10-03 16:15:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:50:00 +0930(2015-10-03 16:20:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 01:55:00 +0930(2015-10-03 16:25:00 +0000) is in 2015-10-04 01:00:00 +0930(2015-10-03 15:30:00 +0000)
2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:05:00 +1030(2015-10-03 16:35:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:10:00 +1030(2015-10-03 16:40:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:15:00 +1030(2015-10-03 16:45:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:20:00 +1030(2015-10-03 16:50:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:25:00 +1030(2015-10-03 16:55:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:30:00 +1030(2015-10-03 17:00:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:35:00 +1030(2015-10-03 17:05:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:40:00 +1030(2015-10-03 17:10:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:45:00 +1030(2015-10-03 17:15:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:50:00 +1030(2015-10-03 17:20:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 03:55:00 +1030(2015-10-03 17:25:00 +0000) is in 2015-10-04 03:00:00 +1030(2015-10-03 16:30:00 +0000)
2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:05:00 +1030(2015-10-03 17:35:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:10:00 +1030(2015-10-03 17:40:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:15:00 +1030(2015-10-03 17:45:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:20:00 +1030(2015-10-03 17:50:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:25:00 +1030(2015-10-03 17:55:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:30:00 +1030(2015-10-03 18:00:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:35:00 +1030(2015-10-03 18:05:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:40:00 +1030(2015-10-03 18:10:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:45:00 +1030(2015-10-03 18:15:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:50:00 +1030(2015-10-03 18:20:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 04:55:00 +1030(2015-10-03 18:25:00 +0000) is in 2015-10-04 04:00:00 +1030(2015-10-03 17:30:00 +0000)
2015-10-04 05:00:00 +1030(2015-10-03 18:30:00 +0000) is in 2015-10-04 05:00:00 +1030(2015-10-03 18:30:00 +0000)

On above result, "2015-04-05 02:00:00" appear twice on +1030 and +0930 before transition.
beginningOfHour should return +1030 value when receiver was +1030

Swift 2.0, cocoapods 0.39.0 pod issue

Using swift 2.0 with Cocoapods 0.39.0 I'm getting the issue below:

Pre-downloading: Timepiece from https://github.com/naoty/Timepiece.git, branch swift-2
fatal: No names found, cannot describe anything.
[!] Unable to satisfy the following requirements:

Thx

Timezones

First off. I like the progress of this library. It's very elegant. So to my issue....I think this is more a feature request then an issue. I was thinking would be very nice to be able to set / get Timezones. What do you think?

syntactic sugar for week days

With something like this:

enum Weekday { case monday, tuesday, wednesday, thursday, friday, saturday, sunday }

extension Date {
   static func next(weekday: Weekday) -> Date {
        // some code
    }

   static func last(weekday: Weekday) -> Date {
        // some code
    }
}

you can write things like:

func wait(until: Date) {}

wait(until: .next(.wednesday))

0.6.0 is not compatible with 0.5.0

0.6.0 can run in Swift3 only, so it's not compatible with 0.5.0.
If you would like to follow semantic versioning as Carthage suggested, you can change the version to 1.0.0.
If you don't, we can't use ~> operator of Cartfile.

You can see about the versioning in the document https://github.com/Carthage/Carthage if you would like to know more.

Thanks.

What do you think?

What if Timepiece had a between method that took a beginDate and endDate and returned true if the date was between them?

like so ...

extension NSDate {
 func between(beginDate: NSDate, endDate: NSDate) -> Bool {
      return  (self < beginDate) && (self > endDate)
    }
}

What do you think?

Mac OS X support.

Hi! Could you add Carthage support for Mac OS X? I don't know how to do it, but if you explain to me I can try and do a pull request.

Thanks.

How to subtract 2 dates and get minutes?

Great library, thanks a mil!! Works beautifully for watch complications ;)

I'm trying to subtract 2 dates and get minutes. I couldn't find how to do this. What I was hoping for was something like this:

(date1 - date2).minutes
(date1 - date2).hours
(date1 - date2).days

This doesn't compile for Timepiece of course, so what I'm ending up doing is this:

NSCalendar.currentCalendar().components(.Minute,
  fromDate: date1, toDate: date2, options: []).minute`

Is something like this possible with Timepiece?

Any way to get the beginning of the week easily?

If it's not currently possible, maybe we can add something like

let now = NSDate()
let monday = now.beginningOfWeek

Or possibly

let monday = now.last(NSDate.Monday)

I just had a couple of ideas on how that may look, but wanted to ask if there's was already a way first.

Invalid Bundle Structure - The binary file 'Timepiece.framework/libswiftRemoteMirror.dylib' is not permitted

I'm using Timeline with Carthage. Now, when I try to upload my app to iTunes I get the following error:

Invalid Bundle Structure - The binary file 'MyApp.app/Frameworks/Timepiece.framework/libswiftRemoteMirror.dylib' is not permitted. Your app can’t contain standalone executables or libraries, other than the CFBundleExecutable of supported bundles. Refer to the Bundle Programming Guide at https://developer.apple.com/go/?id=bundle-structure for information on the iOS app bundle structure."
ERROR ITMS-90206: "Invalid Bundle. The bundle at 'MyApp.app/Frameworks/Timepiece.framework' contains disallowed file 'Frameworks'."

Documentation

Absolutely love the library but sometimes unsure on how certain methods work, and it seems like a few of things don't seem to have clear/any documentation. Maybe write some more? I'm happy to help.

Value of type 'Int' has no member 'weeks', 'seconds', …

Hi I have the weird error, that whenever I add 'Timepiece', I get the error, that the type 'Int' does not have a member for week, seconds etc. However, I can see the Pod files in the workspace in the Pods-project. (Especially the Int+Timepiece.swift-file is there.) All my other Pods work...

Do you have an idea what it could be?

My Podfile looks like this.. not really spectacular.

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '9.0'

xcodeproj 'MyProject'

use_frameworks!

target 'MyProject' do
    pod 'GoogleMaps'
    pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git'
    pod 'Alamofire', '~> 3.1.5'
    pod 'Hex', '~> 3.0'
    pod 'AFNetworking', '~> 2.6.0' #'~> 2.4'
    pod 'BlockRSSParser', '~> 2.1'
    pod 'Parse'
    pod 'Timepiece'
end

changing timezone

Hi,

I noticed that in the newer versions the change method is removed. Normally to change the timezone i would do something like this:

Date().change(timeZone: TimeZone(name: "UTC")!)

How can I do this with the latest Timepiece?

How to modify a date.

First of all thank you for your library. It's been super helpful!

I am upgrading my code to Swift 3 and updated Timepiece from v0.4.3 to the current version and I can't seem to find an method on Date equivalent to change(year:, month:, day:) that was available for NSDate instances.

Is there a recommended way to do this now?

libtool argument error

Sorry I am very new to swift/xcode. I installed Timepiece via cocoapods. I went to build without changing any code or anything in xcode and I get this error.

error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character `X' in: -Xlinker

Its my first app. I am building a swift iphone app if that matters. Is there some tool a new system would need to configure or install? Is this a cocoapod issue? I cant seem to find any configs or code on my system about it. I found a few different issues about libtool in cocoapads but I dont think they were related.

Thanks for any help!

System details and full error:
OSX 10.10.1
xcode Version 6.1.1 (6A2008a)

Libtool /Users/becker/Library/Developer/Xcode/DerivedData/HungerZone-gmzvspvrbhkznafqlbjwnrtnpobg/Build/Products/Debug-iphonesimulator/libPods-Timepiece.a normal x86_64
    cd /Users/becker/trash/HungerZone/Pods
    export IPHONEOS_DEPLOYMENT_TARGET=8.0
    export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static -arch_only x86_64 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk -L/Users/becker/Library/Developer/Xcode/DerivedData/HungerZone-gmzvspvrbhkznafqlbjwnrtnpobg/Build/Products/Debug-iphonesimulator -filelist /Users/becker/Library/Developer/Xcode/DerivedData/HungerZone-gmzvspvrbhkznafqlbjwnrtnpobg/Build/Intermediates/Pods.build/Debug-iphonesimulator/Pods-Timepiece.build/Objects-normal/x86_64/Pods-Timepiece.LinkFileList -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator -Xlinker -add_ast_path -Xlinker /Users/becker/Library/Developer/Xcode/DerivedData/HungerZone-gmzvspvrbhkznafqlbjwnrtnpobg/Build/Intermediates/Pods.build/Debug-iphonesimulator/Pods-Timepiece.build/Objects-normal/x86_64/Pods_Timepiece.swiftmodule -framework Foundation -o /Users/becker/Library/Developer/Xcode/DerivedData/HungerZone-gmzvspvrbhkznafqlbjwnrtnpobg/Build/Products/Debug-iphonesimulator/libPods-Timepiece.a
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character `X' in: -Xlinker
Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-sacLT] [-no_warning_for_no_symbols]
Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -dynamic [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-o output] [-install_name name] [-compatibility_version #] [-current_version #] [-seg1addr 0x#] [-segs_read_only_addr 0x#] [-segs_read_write_addr 0x#] [-seg_addr_table <filename>] [-seg_addr_table_filename <file_system_path>] [-all_load] [-noall_load]

"No such module 'Timepiece'"

Hi, I'm pretty new to Swift/pods. I added timepiece to my pod file, installed it and can verify it's added to my app. But attempting to import Timepiece at the top of my appDelegate with the rest of my other imports gives me an error "No such module 'Timepiece'"

Any idea what I'm missing here?

Thanks!

Support for tvOS

With the new Apple TV, the support for tvOS would be great. :)

time ago support

Be nice to have time ago support with this library. If there is one I didn't see where it was.

But being able to say date.timeAgo that outputs "4 minutes ago" etc...

Carthage: Problems during Build for iOS

I'm using Carthage to get Timepiece included into my project. However, there is a problem during build:

ld: warning: directory not found for option '-F/Users/gschneider/Development/ws.42/hfmain/Configuration/build/Debug-iphoneos'
ld: warning: directory not found for option '-F/Carthage/Build/iOS'
ld: building for iOS simulator, but linking against dylib built for OSX, file '/Users/gschneider/Development/ws.42/hfmain/Configuration/Carthage/Checkouts/Timepiece/Build/Products/Release/Timepiece.framework/Timepiece' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any idea how to fix that? I just perform carthage update and proceeded according to the Carthage instructions.

Just an idea: Parallel APIs for non-English languages

I find this style of natural-language API exciting, when it is executed well, as it is in this case. Great stuff!

While browsing the API intro, I had random thought: while this API is very natural to English-language speakers — and it seems aimed directly at "sounding" like natural English — wouldn't it make equal sense to add similar APIs for other human languages?

For example:

let  = NSDate() // now
let 来週 =+ 1.週間 // next week
let 明後日 =+ 2.日間 // day after tomorrow

// shortcuts #1
let 今日 = NSDate.今日() // today
let 明日 = NSDate.明日() // tomorrow
let 昨日 = NSDate.昨日() // yesterday

// shortcuts #2
let 一昨日 = 2..// day before yesterday
let 東京オリンピックの年 = 5..// year of Tokyo Olympics

I chose Japanese above as a deliberately extreme example, but it fits almost perfectly as a natural-sounding API. (The only rough edge being that English implies "duration" when you say "2 days", but in Japanese it's explicit — "2日" is not the same as "2日間". But I think this could be easily covered by a small number of redirecting functions.)

Date comparing

Thanks for perfect abstraction like now + 2.days. It will be fantastic if you add dates comparing!

problems installing with CocoaPods

Hello,
I've recently had to reinstall pods in my project and I am getting this error in CocoaPods:

Timepiece-library does not specify a Swift version and none of the targets (Pods) integrating
it have the SWIFT_VERSION attribute set.
Please contact the author or set the SWIFT_VERSION attribute in at least one
of the targets that integrate this pod.
Is it possible to fix? thank you very much

Support for different locales when converting String to NSDate

Currently the NSDateFormatter is initialized with the locale from the current device. Often responses from web servers contain date strings in english locale. Therefore it would be great to have the option to give a locale parameter to the dateFromFormat function.

Change the default calendar

I want to change the default calendar. Because if someone uses Japanese calendar, Date().year returns the year in Japanese but I don't expect the behavior.

I suggest the changes below.

Date.defaultCalendar = .gregorian // Fix the default calendar to gregorian calendar.
Date().year // It'll always returns the year in gregorian calendar.

What do you think? If you agree to my suggestion, I'll make a pull request.

Weekend

Hello.
Very nice lib. it rocks man!
Just wandering if there is any quick workaround to take the dates of the next weekend (saturday-sunday)
like Date.today()

How can I check format of string

Is there a simple way how can I check a format of string variable?

Example:

if(stringVariable.format == format("dd/MM/yyyy"){
print("format of stringVariable is the same as date!")
}

Can you add more date math examples to readme?

I'm dim. I'm not 100% sure how to use this library to do something like (I'm shortcutting the syntax)

let christmas = Date(year: today().year, month:12, day:25)
let daysUntilChristmas = christmas - today ????? //give us some examples please

Swift 2.0 branch: deployment target

I think the deployment target set on the branch is iOS 9 only. It should at least be iOS 8. I have no disagreement with leaving out iOS 7.

Optionals instead of forced unwrap for ago/later?

Thanks for a great library and updating the code base. It much slimmer and leveraging more native API's 👍

I noticed in the updates there are a lot of optionals now, which are a bit inconvenient although I understand why. Any ideas on safely getting around this and not have Timepiece return optionals for at least ago/later calculations?

For example, 46.days.later return a Date? object because it is using Calendar.current.date(byAdding: -self, to: Date()).

Expanding this, the source is really this:

var dayComponents = DateComponents()
dayComponents.day = 46
return Calendar.current.date(byAdding: dayComponents, to: Date()) //Optional

How is it possible the Calendar.current.date(byAdding:to:) would ever be nil, such as what scenario could this happen? I can't think of any if the DateComponent is instantiated in a controlled manner by the library and both components are always valid. So would it be safe enough for the sake of convenience to force unwrap it like this?:

var later: Date {
    return Calendar.current.date(byAdding: self, to: Date())!
}

Question: How to import this?

What is the best way to import this library after installing with pods?

Do you use import Timepiece in every swift file that needs to use this? Is there a way to globally import it for everything?

Thanks

endOfDay handles date differently for iOS 10 as it does for iOS 11

the first screenshot is the dates being handled by iOS 11. The second screenshot is the dates being handles by iOS 10.

First, there is a row where the exact timestamps pulled from parse are printed, then a row showing what happens to the timestamps after .endOfDay is called and then I try to calculate the number of days between the two dates.

`

     if let start = self.transaction.pickupDate{

        if let end = self.transaction.returnDate{

            print("start date: \(start),  end date: \(end)")

            let days = abs(end.endOfDay.daysFrom(start.endOfDay))

            print("Start Date: \(start.endOfDay), End Date: \(end.endOfDay),  number of days: \(days)")

        }

    }

`

You can see clearly that endOfDay is doing something really weird in iOS 11

screen shot 2018-02-21 at 10 16 01 pm

screen shot 2018-02-21 at 10 14 54 pm

endOfDay might be from an older version of Timepiece that I have. But basically, it does the following: return change(hour: 23, minute: 59, second: 59)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.