naoty / Timepiece

Intuitive date handling in Swift
MIT License
2.63k stars 147 forks source link

Change the default calendar #92

Closed satoshi-takano closed 6 years ago

satoshi-takano commented 6 years ago

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.

naoty commented 6 years ago

Thank you for suggestion! 😄

I disagree with Date.defaultCalendar. It is confusing because Date object is a just value and independent of the calendar setting in iOS.

Instead, below method will fill your need.

Date().changed(on: .japanese)

Currently, changed() calculates date components with Calendar.current, and so on calendar: Calendar argument will be useful for you.

satoshi-takano commented 6 years ago

Thank you for quick response! 😀 I think your idea is good way to change the calendar. I'll try it.

naoty commented 6 years ago

Sorry for less description. This method is not implemented yet. So, if you want it, please send a pull request!

satoshi-takano commented 6 years ago

Thank you for the description! I decided to use Calendar and DateComponent directly to get year in Gregorian calendar. Because I noticed that Timepiece’s conveniences is based on Extension and I can’t add calendar as a stored property(also, I don’t want use Associated Object).