Git Product home page Git Product logo

timeintervalformatstyle's Introduction

TimeIntervalFormatStyle

Simple ParseableFormatStyle conformace for TimeInterval, with and without milliseconds.

Input

Can accept string timestamps formatted with hours, minutes, seconds, and, optionally, milliseconds. The parser will handle missing 0-padding

Example inputs

  • 0:10:45.333 -> Valid
  • 0:2:45.333 -> Valid (0-padding missing OK)
  • 20:10:45.2 -> Valid (0 padding on milliseconds missing)
  • 20:10:45.200 -> Valid (more than 1 hour digit)
  • 0:10:45 -> Valid (no milliseconds)
  • 0:10:45.22323 -> Valid (milliseconds go beyond thousandths place)
  • 0:10:45:220 -> Valid (millisecond delimiter swapped)

  • 10:45.222 -> Invalid (must have hours, even if 0)
  • 0:10:45.aaa -> Invalid (non-digit characters)
  • 0-10:45.000 -> Invalid (wrong delimiter)
  • 1:10:62.222 -> Invalid (too many seconds)

Output

Using showsMilliseconds the client can control if milliseconds are shown or not. All output will be 0-padded and contains exactly 3 places of milliseconds.

  • With milliseconds: 2:03:44.232
  • Without milliseconds: 2:03:44

Usage in SwiftUI

I built this to be able to easily move to/from TimeIntervals in SwiftUI TextFields. A simple example below!

struct TimeIntervalTextField: View {
    @Binding var duration: TimeInterval
    
    var body: some View {
        TextField("Duration:", value: $duration, format: .timeInterval(showMilliseconds: true), prompt: nil)
    }
}

timeintervalformatstyle's People

Contributors

spanage avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

daneden qinc-av

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.