Skip to content

Commit

Permalink
bug fixed next and back day
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelock authored and Shelock committed Aug 6, 2018
1 parent 8fb34ce commit 1c7f729
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 34 deletions.
42 changes: 31 additions & 11 deletions Example/Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -22,20 +22,40 @@
<rect key="frame" x="16" y="20" width="343" height="216"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="bF1-YA-mfc">
<rect key="frame" x="168" y="297" width="39" height="30"/>
<state key="normal" title="Reset"/>
<connections>
<action selector="didTapReset:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Tfb-vj-wxd"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="vsD-If-AKj">
<rect key="frame" x="16" y="297" width="343" height="30"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OfD-49-uQH">
<rect key="frame" x="0.0" y="0.0" width="109" height="30"/>
<state key="normal" title="Back Day"/>
<connections>
<action selector="didTapBackDay:" destination="BYZ-38-t0r" eventType="touchUpInside" id="BCs-9J-DNe"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="bF1-YA-mfc">
<rect key="frame" x="117" y="0.0" width="109" height="30"/>
<state key="normal" title="Reset"/>
<connections>
<action selector="didTapReset:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Tfb-vj-wxd"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="UYe-uq-OBk">
<rect key="frame" x="234" y="0.0" width="109" height="30"/>
<state key="normal" title="Next Day"/>
<connections>
<action selector="didTapNextDay:" destination="BYZ-38-t0r" eventType="touchUpInside" id="6CG-Dh-RWx"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="q0n-fH-WHJ" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="H2M-xA-UZi"/>
<constraint firstItem="bF1-YA-mfc" firstAttribute="centerX" secondItem="q0n-fH-WHJ" secondAttribute="centerX" id="aYT-0M-B9O"/>
<constraint firstItem="bF1-YA-mfc" firstAttribute="top" secondItem="q0n-fH-WHJ" secondAttribute="bottom" constant="61" id="gvS-fO-IHs"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="vsD-If-AKj" secondAttribute="trailing" constant="16" id="bgd-lq-VHR"/>
<constraint firstItem="vsD-If-AKj" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="nds-TM-BAU"/>
<constraint firstItem="q0n-fH-WHJ" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="uoa-iV-Xzs"/>
<constraint firstItem="vsD-If-AKj" firstAttribute="top" secondItem="q0n-fH-WHJ" secondAttribute="bottom" constant="61" id="yYP-y9-RFI"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="q0n-fH-WHJ" secondAttribute="trailing" constant="16" id="zdx-NY-C8f"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
Expand Down
14 changes: 11 additions & 3 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ class ViewController: UIViewController {
super.viewDidLoad()
persianDatePickerView.onChange = {(year, month, day) in
print("\(year)/\(month)/\(day)")
print(self.persianDatePickerView.getGregorianDate()!)
print(self.persianDatePickerView.getPersianDate()!)
print("next day : \(self.persianDatePickerView.nextDay(by: self.persianDatePickerView.getPersianDate()!))")
// print("gregorian Date : \(self.persianDatePickerView.getGregorianDate()!)")
print("persian Date : \(self.persianDatePickerView.getPersianDate())")
}

}
@IBAction func didTapReset(_ sender: Any) {
persianDatePickerView.gotoCurrentDate()
}

@IBAction func didTapNextDay(_ sender: Any) {
self.persianDatePickerView.nextDay(by: self.persianDatePickerView.getPersianDate()!)
}

@IBAction func didTapBackDay(_ sender: Any) {
self.persianDatePickerView.backDay(by: self.persianDatePickerView.getPersianDate()!)
}

}

2 changes: 1 addition & 1 deletion PersianDatePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PersianDatePicker'
s.version = '0.6.0'
s.version = '0.7.0'
s.summary = 'PersianDatePicker is a PickerView. by : farhad faramarzi'


Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "PersianDatePicker/PersianDatePickerView.swift"
timestampString = "555220941.940297"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "75"
endingLineNumber = "75"
landmarkName = "gotoDate(date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "PersianDatePicker/PersianDateDataSource.swift"
timestampString = "555220941.940376"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "50"
endingLineNumber = "50"
landmarkName = "myDateCompononents(date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "PersianDatePicker/PersianDateDataSource.swift"
timestampString = "555220941.940438"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "48"
endingLineNumber = "48"
landmarkName = "myDateCompononents(date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "PersianDatePicker/PersianDateDataSource.swift"
timestampString = "555220941.940493"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "100"
endingLineNumber = "100"
landmarkName = "nextDay(by:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Example/Example/ViewController.swift"
timestampString = "555220941.94055"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "35"
endingLineNumber = "35"
landmarkName = "didTapBackDay(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "PersianDatePicker/PersianDateDataSource.swift"
timestampString = "555220941.940954"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "107"
endingLineNumber = "107"
landmarkName = "backDay(by:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<VariablesViewState
version = "1.0">
<ContextStates>
<ContextState
contextName = "PersianDateDataSource.nextDay(by:):PersianDateDataSource.swift">
<PersistentStrings>
<PersistentString
value = "call.date(byAdding: .day, value: 1, to: d)!">
</PersistentString>
</PersistentStrings>
</ContextState>
</ContextStates>
</VariablesViewState>
66 changes: 52 additions & 14 deletions PersianDatePicker/PersianDateDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,70 @@ struct PersianDateDataSource {
return (year: comps.year!, month: comps.month!, day: comps.day!)
}

func getPersianDate(year: Int, month: Int, day: Int) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")

let persianDate = "\(year)/\(month)/\(day)"
let date = dateFormatter.date(from: persianDate)
return date
func myDateCompononents(date: Date) -> (year: Int, month: Int, day: Int) {
let call = Calendar(identifier: .gregorian)
let comps = call.dateComponents([.year, .month, .day], from: date)
return (year: comps.year!, month: comps.month!, day: comps.day!)
}

func getPersianDate(year: Int, month: Int, day: Int) -> String? {
return "\(year)/\(month)/\(day)"
}

func convertToGregorian(year: Int, month: Int, day: Int) -> Date? {
guard let persianDate = getPersianDate(year: year, month: month, day: day) else {return nil}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
guard let pDate = dateFormatter.date(from: persianDate) else {return nil}

let gregorianDateString = dateFormatter.string(from: persianDate)
dateFormatter.calendar = Calendar(identifier: .persian)
let gregorianDateString = dateFormatter.string(from: pDate)
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
return dateFormatter.date(from: gregorianDateString)
}

func nextDay(by date: Date) -> Date {
return cal.date(byAdding: .day, value: 1, to: date)!
func convertToPersian(date: Date) -> (year: Int, month: Int, day: Int) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
dateFormatter.calendar = Calendar(identifier: .persian)
let persianDate = dateFormatter.string(from: date)
let splitDate = persianDate.components(separatedBy: "/")
let year = Int(splitDate[0]) ?? 1300
let month = Int(splitDate[1]) ?? 1
let day = Int(splitDate[2]) ?? 1

return (year: year , month: month, day: day )
}

func convertDate(persianDate: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
dateFormatter.calendar = Calendar(identifier: .persian)

let date = dateFormatter.date(from: persianDate)

let df = DateFormatter()
df.dateFormat = "yyyy/MM/dd"
df.timeZone = TimeZone(abbreviation: "GMT")
df.calendar = Calendar(identifier: .persian)
let gregorianDate = df.string(from: date!)
df.string(from: date!)
return df.date(from: gregorianDate)!
}

func nextDay(by date: String) -> Date {
let d = convertDate(persianDate: date)
var call = Calendar(identifier: .gregorian)
call.timeZone = TimeZone(abbreviation: "GMT")!
return call.date(byAdding: .day, value: 1, to: d)!
}

func backDay(by date: Date) -> Date {
return cal.date(byAdding: .day, value: -1, to: date)!
func backDay(by date: String) -> Date {
let d = convertDate(persianDate: date)
var call = Calendar(identifier: .gregorian)
call.timeZone = TimeZone(abbreviation: "GMT")!
return call.date(byAdding: .day, value: -1, to: d)!
}
}
17 changes: 12 additions & 5 deletions PersianDatePicker/PersianDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public class PersianDatePickerView: UIView {
setDatePicker(year: currentDate.year, month: currentDate.month, day: currentDate.day)
}

public func gotoDate(date: Date) {
let persianDate = persianDateDataSource.convertToPersian(date: date)
setDatePicker(year: persianDate.year, month: persianDate.month, day: persianDate.day)
}

public func setPersianDate(year: Int, month: Int, day: Int) {
setDatePicker(year: year, month: month, day: day)
}
Expand All @@ -79,16 +84,18 @@ public class PersianDatePickerView: UIView {
return persianDateDataSource.convertToGregorian(year: year, month: month, day: day)
}

public func getPersianDate() -> Date? {
public func getPersianDate() -> String? {
return persianDateDataSource.getPersianDate(year: year, month: month, day: day)
}

public func nextDay(by date: Date) -> Date {
return persianDateDataSource.nextDay(by: date)
public func nextDay(by date: String) {
let d = persianDateDataSource.nextDay(by: date)
gotoDate(date: d)
}

public func backDay(by date: Date) -> Date {
return persianDateDataSource.backDay(by:date)
public func backDay(by date: String) {
let d = persianDateDataSource.backDay(by:date)
gotoDate(date: d)
}

private func setDatePicker(year: Int, month: Int, day: Int) {
Expand Down

0 comments on commit 1c7f729

Please sign in to comment.