Git Product home page Git Product logo

timelapse's Introduction

TimeLapse

Version Swift Platform License

Date extension that compares another Date with current time, converts it in seconds and returns a formatted time lapse description.

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/aguilarpgc/TimeLapse.git", from: "0.5.0")
]

Example

Alt Text

Usage

Default

import Foundation
import TimeLapse

let currentDate  = Date()
let fakePastDate = Calendar.current.date(byAdding: .second, value: -600, to: currentDate)! // 10 minutes ago

let timeLapse = fakePastDate.elapsedTime(until: currentDate)

print(timeLapse) // 10min

Date

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"

let fakeCurrentDate = dateFormatter.date(from: "24/12/2018 23:59:59")!

// some test dates
let date0  = dateFormatter.date(from: "24/12/2018 23:59:58")!
let date1  = dateFormatter.date(from: "24/12/2018 23:59:09")!
let date2  = dateFormatter.date(from: "24/12/2018 23:52:59")!
let date3  = dateFormatter.date(from: "24/12/2018 18:59:59")!
let date4  = dateFormatter.date(from: "23/12/2018 23:59:59")!
let date5  = dateFormatter.date(from: "09/12/2018 23:59:59")!
let date6  = dateFormatter.date(from: "24/11/2018 23:59:59")!
let date7  = dateFormatter.date(from: "24/12/2016 23:59:59")!

date0.elapsedTime(until: fakeCurrentDate)
date1.elapsedTime(until: fakeCurrentDate)
date2.elapsedTime(until: fakeCurrentDate)
date3.elapsedTime(until: fakeCurrentDate)
date4.elapsedTime(until: fakeCurrentDate)
date5.elapsedTime(until: fakeCurrentDate)
date6.elapsedTime(until: fakeCurrentDate)
date7.elapsedTime(until: fakeCurrentDate)
  • Just Now : just now
  • Seconds : 50s
  • Minutes : 7min
  • Hours : 5h
  • Days : 1d
  • Weeks : 2w
  • Months : 1mo
  • Years : 2y

Int (Seconds)

2.shortTimeLapse()
10.shortTimeLapse()
1800.shortTimeLapse()
43200.shortTimeLapse()
172_800.shortTimeLapse()
604_800.shortTimeLapse()
15_552_000.shortTimeLapse()
31_536_000.shortTimeLapse()
  • Just Now : just now
  • Seconds : 10s
  • Minutes : 30min
  • Hours : 12h
  • Days : 2d
  • Weeks : 1w
  • Months : 6mo
  • Years : 1y

Customize Format

Conforms to the protocol TimeLapseFormat and assign custom format to TimeLapse.format

Example

struct CustomTimeLapseFormat: TimeLapseFormat {

    var now    : String = "now"
    var second : String = "sec"
    var minute : String = "m"
    var hour   : String = "hour"
    var day    : String = "d"
    var week   : String = "w"
    var month  : String = "mon"
    var year   : String = "y"

}
Assign for usage:
TimeLapse.format = CustomTimeLapseFormat()
Result:
0.shortTimeLapse()
10.shortTimeLapse()
1800.shortTimeLapse()
7200.shortTimeLapse()
604_799.shortTimeLapse()
2_591_999.shortTimeLapse()
31_535_999.shortTimeLapse()
630_720_000.shortTimeLapse()
  • Just Now : now
  • Seconds : 10sec
  • Minutes : 30m
  • Hours : 2hour
  • Days : 6d
  • Weeks : 4w
  • Months : 12mon
  • Years : 20y
Restore to default format
TimeLapse.enableDefaultFormat()

License

This program is free software; you can redistribute it and/or modify it under the terms of the MIT License.

timelapse's People

Contributors

aguilarpgc avatar

Watchers

 avatar

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.