~askonomm/farm

0c66ef5e20255ee845ea1a4ff430927581f4a951 — Asko N├Ámm a month ago aeeb5c5 v1.0.3
Add built-in relative time support
1 files changed, 62 insertions(+), 8 deletions(-)

M Sources/Farm/Farm.swift
M Sources/Farm/Farm.swift => Sources/Farm/Farm.swift +62 -8
@@ 91,14 91,7 @@ public struct Farm {
                                        let dateFormatter = DateFormatter()
                                        dateFormatter.dateFormat = "yyyy-MM-dd"
                                        metadata[yamlDataItem.key] = dateFormatter.string(from: date!)
                                        
                                        if #available(macOS 10.15, *) {
                                            let relativeDateFormatter = RelativeDateTimeFormatter()
                                            relativeDateFormatter.unitsStyle = .full
                                            metadata["relativeDate"] = relativeDateFormatter.localizedString(for: date!, relativeTo: Date())
                                        } else {
                                            metadata["relativeDate"] = "Relative date requires macOS 10.15 or later"
                                        }
                                        metadata["relativeDate"] = relativeTime(date!)
                                    }
                                } else {
                                    metadata[yamlDataItem.key] = yamlDataItem.value as? String


@@ 116,6 109,67 @@ public struct Farm {
        return items
    }
    
    private func relativeTime(_ date: Date) -> String {
        let calendar = Calendar.current
        let now = Date()
        let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year]
        let components = (calendar as NSCalendar).components(unitFlags, from: date, to: now, options: [])
        
        if let year = components.year, year >= 2 {
            return "\(year) years ago"
        }
        
        if let year = components.year, year >= 1 {
            return "Last year"
        }
        
        if let month = components.month, month >= 2 {
            return "\(month) months ago"
        }
        
        if let month = components.month, month >= 1 {
            return "Last month"
        }
        
        if let week = components.weekOfYear, week >= 2 {
            return "\(week) weeks ago"
        }
        
        if let week = components.weekOfYear, week >= 1 {
            return "Last week"
        }
        
        if let day = components.day, day >= 2 {
            return "\(day) days ago"
        }
        
        if let day = components.day, day >= 1 {
            return "Yesterday"
        }
        
        if let hour = components.hour, hour >= 2 {
            return "\(hour) hours ago"
        }
        
        if let hour = components.hour, hour >= 1 {
            return "An hour ago"
        }
        
        if let minute = components.minute, minute >= 2 {
            return "\(minute) minutes ago"
        }
        
        if let minute = components.minute, minute >= 1 {
            return "A minute ago"
        }
        
        if let second = components.second, second >= 3 {
            return "\(second) seconds ago"
        }
        
        return "Just now"
    }
    
    /**
     * Sorts content by meta key, in descending order.
     */