Skip to content

Instantly share code, notes, and snippets.

@NicholasTD07
Last active March 5, 2021 01:26
Show Gist options
  • Save NicholasTD07/49ff72155d9f2defe108 to your computer and use it in GitHub Desktop.
Save NicholasTD07/49ff72155d9f2defe108 to your computer and use it in GitHub Desktop.
I use this to know how many hours I work a day.
#!/usr/bin/env xcrun swift
// Usage: path/to/TimeTracking.swift
// Go to bottom to see how it's used.
// The MIT License (MIT)
// Copyright (c) 2015 Nicholas T.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
typealias TimeInputType = Int // hhmm
private let OnehourInMinutes = 60
struct _Time: Printable {
let hours: Int
let minutes: Int
init(hours: Int, minutes: Int) {
(self.hours, self.minutes) = (hours, minutes)
}
init(_ time: TimeInputType) {
(self.hours, self.minutes) = (time/100, time % 100)
}
var asTimeInput: TimeInputType { return hours * 100 + minutes }
var description: String {
return "\(hours)h \(minutes)m"
}
}
func + (left: _Time, right: _Time) -> _Time {
var sumOfMinutes = left.minutes + right.minutes
var sumOfHours = left.hours + right.hours
if sumOfMinutes > OnehourInMinutes {
sumOfMinutes -= OnehourInMinutes
sumOfHours += 1
}
return _Time(hours: sumOfHours, minutes: sumOfMinutes)
}
func - (left: _Time, right: _Time) -> _Time {
var differenceOfMinutes = left.minutes - right.minutes
var differenceOfHours = left.hours - right.hours
if differenceOfMinutes < 0 {
differenceOfMinutes += OnehourInMinutes
differenceOfHours -= 1
}
return _Time(
hours: differenceOfHours,
minutes: differenceOfMinutes
)
}
struct TimeDuration: Printable {
let from: _Time
let to: _Time
let base: _Time
init(base: _Time) {
self.base = base
self.from = _Time(0000)
self.to = _Time(0000)
}
init(from: TimeInputType,
to: TimeInputType,
base: TimeInputType = TimeInputType()
) {
self.from = _Time(from)
self.to = _Time(to)
self.base = _Time(base)
}
var durationAsTimeInput: TimeInputType {
return duration.asTimeInput
}
var duration: _Time {
let duration = base + to - from
return duration
}
var description: String {
return "\(duration.hours)h \(duration.minutes)m"
}
}
func + (left: TimeDuration, right: TimeDuration) -> TimeDuration {
return TimeDuration(base: left.duration + right.duration)
}
struct ToTime {
let from: TimeInputType
let base: TimeInputType
init(
from: TimeInputType,
base: TimeInputType = TimeInputType()
) {
self.from = from
self.base = base
}
func to(time: TimeInputType) -> TimeDuration {
return TimeDuration(from: from, to: time, base: base)
}
}
struct Time {
static func from(time: TimeInputType, base: TimeInputType = TimeInputType()) -> ToTime {
return ToTime(from: time, base: base)
}
}
extension TimeDuration {
func from(time: TimeInputType) -> ToTime {
return Time.from(time, base: durationAsTimeInput)
}
}
func main() {
let date = "2015-12-03 and 2015-12-04"
let hoursOn3rd = Time.from(1610).to(1620)
let hoursOn4th = Time.from(1420).to(1440)
let hours = hoursOn3rd + hoursOn4th
println("Date: \(date)") // Date: 2015-12-03 and 2015-12-04
println("Time: \(hours)") // Time: 0h 28m
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment