Skip to content

Commit

Permalink
Rename dateWith to adjust to keep all adjustments with the same callout.
Browse files Browse the repository at this point in the history
  • Loading branch information
melvitax committed Mar 1, 2017
1 parent fb0817b commit f6d192b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion AFDateHelper.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Pod::Spec.new do |s|
s.name = "AFDateHelper"
s.version = "4.0.2"
s.version = "4.1.0"
s.summary = "Date Extension for Swift 3.0"
s.description = <<-DESC
A Swift Date extension for creating, modifying and comparing dates.
Expand Down
3 changes: 1 addition & 2 deletions Playground.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ now.adjust(.nthWeekday, offset: 1)
now.adjust(.week, offset: 1)
now.adjust(.month, offset: 1)
now.adjust(.year, offset: 1)

now.dateWith(hour: 12, minute: 0, second: 0)
now.adjust(hour: 12, minute: 0, second: 0)


/***********************
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Adjust dates

```Swift
let twoHoursBefore = date.adjust(.hour, offset: -2)
let atNoon = date.dateWith(hour: 12, minute: 0, second: 0)
let atNoon = date.adjust(hour: 12, minute: 0, second: 0)
```

Create dates for...
Expand Down Expand Up @@ -308,10 +308,10 @@ case month
case year
```

Use the `dateWith(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date` function to change the date components.
Use the `adjust(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date` function to change the date components.

```Swift
let atNoon = date.dateWith(hour: 12, minute: 0, second: 0)
let atNoon = date.adjust(hour: 12, minute: 0, second: 0)
```

### Create Dates For...
Expand Down
20 changes: 10 additions & 10 deletions Sources/DateHelper.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// AFDateHelper.swift
// https://github.com/melvitax/DateHelper
// Version 4.0.2
// Version 4.1.0
//
// Created by Melvin Rivera on 7/15/14.
// Copyright (c) 2014. All rights reserved.
Expand Down Expand Up @@ -342,35 +342,35 @@ public extension Date {
func dateFor(_ type:DateForType) -> Date {
switch type {
case .startOfDay:
return dateWith(hour: 0, minute: 0, second: 0)
return adjust(hour: 0, minute: 0, second: 0)
case .endOfDay:
return dateWith(hour: 23, minute: 59, second: 29)
return adjust(hour: 23, minute: 59, second: 29)
case .startOfWeek:
let firstDayOfWeek = Calendar.current.firstWeekday
return dateWith(hour: 0, minute: 0, second: 0, day: firstDayOfWeek)
return adjust(hour: 0, minute: 0, second: 0, day: firstDayOfWeek)
case .endOfWeek:
let lastDayOfWeek = Calendar.current.firstWeekday+6
return dateWith(hour: 0, minute: 0, second: 0, day: lastDayOfWeek)
return adjust(hour: 0, minute: 0, second: 0, day: lastDayOfWeek)
case .startOfMonth:
return dateWith(hour: 0, minute: 0, second: 0, day: 1)
return adjust(hour: 0, minute: 0, second: 0, day: 1)
case .endOfMonth:
let month = (component(.month) ?? 0) + 1
return dateWith(hour: 0, minute: 0, second: 0, day: 0, month: month)
return adjust(hour: 0, minute: 0, second: 0, day: 0, month: month)
case .tomorrow:
return adjust(.day, offset:1)
case .yesterday:
return adjust(.day, offset:-1)
case .nearestMinute(let nearest):
let minutes = (component(.minute)! + nearest/2) / nearest * nearest
return dateWith(hour: nil, minute: minutes, second: nil)
return adjust(hour: nil, minute: minutes, second: nil)
case .nearestHour(let nearest):
let hours = (component(.hour)! + nearest/2) / nearest * nearest
return dateWith(hour: hours, minute: 0, second: nil)
return adjust(hour: hours, minute: 0, second: nil)
}
}

/// Return a new Date object with the new hour, minute and seconds values.
func dateWith(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date {
func adjust(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date {
var comp = Date.components(self)
comp.month = month ?? comp.month
comp.day = day ?? comp.day
Expand Down

0 comments on commit f6d192b

Please sign in to comment.