title: Round time to nearest thirty seconds
body: <p>I have an app that presents data that expires every 30 seconds (precisely, at h/m/s 11:30:00, 11:30:30, 11:31:00, etc).</p>
  
  <p>I can get the current time, but I am unsure on how to calculate the time between now and the nearest thirty seconds.</p>
  
  <p>Anything I've found is in Objective-C, and I've been unable to convert it.</p>
  
  <p>Here's what I tried:</p>
  
  <pre><code>func nearestThirtySeconds() -&gt; Date? {
          var components = NSCalendar.current.dateComponents([.second], from: self)
          let second = components.second ?? 30
          components.second = second &gt;= 30 ? 60 - second : -second
          return Calendar.current.date(byAdding: components, to: self)
      }
  </code></pre>
  
  <p>But this returns the nearest minute (I think, it always returns a definite minute)</p>
  
  <p>Any ideas?</p>