Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with pad options in timeComponents()/timeComponentsSinceNow() in Date/DateInRegion and .string() of TimeInterval. New formatter options struct. #274

Closed
digidhamu opened this issue Oct 2, 2016 · 2 comments
Assignees
Labels
Milestone

Comments

@digidhamu
Copy link

digidhamu commented Oct 2, 2016

I am not sure how SwiftDate API works to replace Swift 3 function given below. Basically, this function converts seconds to format hh:mm:ss.

In this case, below Swift 3 code is working fine. But in SwiftDate, not padding Zero e.g. 40:01 rather returns 40:1.

Some more sample
1:01 - 1:1 (no zero)
12:01 -> 12:1 (no zero)
12:20 -> 12:20 (only this works)

Could you please advise anything wrong in using API and even I had tried DateZeroBehaviour as arguments.

Swift 3 Style

open class func secondsToTimeString(_ seconds: TimeInterval) -> String {
  let formatter = DateComponentsFormatter()
  formatter.zeroFormattingBehavior = .pad

  if seconds >= 3600 {
    formatter.allowedUnits = [.hour, .minute, .second]
  } else {
    formatter.allowedUnits = [.minute, .second]
  }  
    return formatter.string(from: seconds) ?? ""
}

SwiftDate Style

open class func secondsToTimeString(_ seconds: TimeInterval) -> String {
  let refDate = Date()
  let toDate = refDate + Int(seconds).seconds

  do {
    return try (refDate - toDate).string(unitStyle: .positional, max: 4, zero: nil, separator: ":", locale: nil)!
  } catch {
    return ""
  }
}
@malcommac malcommac added the bug label Oct 2, 2016
@malcommac malcommac added this to the 4.0.3 milestone Oct 2, 2016
@malcommac malcommac self-assigned this Oct 2, 2016
@malcommac
Copy link
Owner

You are right.
I've fixed it in 6d811aa.
It will be ready in upcoming 4.0.3 release (I'll release it later tomorrow).

Your function will become:

open class func secondsToTimeString(_ seconds: TimeInterval) -> String {
  let refDate = Date()
  let toDate = refDate + Int(seconds).seconds
  do {
    return try (refDate - toDate).string(options: ComponentsFormatterOptions(zero: .pad))
  } catch {
    return ""
  }
}

These function are now deprecated:

in Date:

func timeComponentsSinceNow(unitStyle:,max:,zero:,separator:) throws -> String
func timeComponents(to:,in:,unitStyle:,max:,zero:,separator:) throws -> String

replaced by:

func timeComponentsSinceNow(options:,shared:) throws -> String
func timeComponents(to:options:shared:) throws -> String

in DateInRegion:

func timeComponentsSinceNow(unitStyle:,max:,zero:,separator:) throws -> String
func timeComponents(toDate,unitStyle:,max:,zero:,separator:) throws -> String

replaced by:

func timeComponentsSinceNow(options:) throws -> String
func timeComponents(toDate:,options:) throws -> String

in TimeInterval:

func string(unitStyle:,max:,zero:,separator:,locale:) throws -> String?

replaced by:

func string(options:,shared:) throws -> String

malcommac added a commit that referenced this issue Oct 2, 2016
@malcommac malcommac changed the title SwiftDate API for converting from Seconds to hh:mm:ss format Bug with pad options in timeComponents()/timeComponentsSinceNow() in Date/DateInRegion and .string() of TimeInterval. New formatter options struct. Oct 2, 2016
@digidhamu
Copy link
Author

Great @malcommac. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants