NKTimeIntervalTextTransformation
描述
本框架提供将两个日期之间的时间间隔的数值转换为有意义口头形式的手段。转换基于用户设置的规则。
框架包含
- 结构
NKTextTimeIntervalConfiguration
,包含将数值转换为字符串格式的规则,根据指定的规则进行转换; TimeInterval
扩展,它根据特定区间计算时间组件的数量,以Double
的形式(使用Double
而不是Int
的目的是允许框架用户独立设置对分数值进行四舍五入或分行的规则);Date
类扩展,根据日期比较,根据指定规则生成时间间隔的字符串表示形式。
界面
NKTimeComponentDeclensionRule
/**
Closure describes declension rule for numerals depending on needs of application or language
**Example:**
{ timeComponents -> (String, Bool) in
if <condition> { return (<time_component_name>, <needs_display_numerical_value>) }
else { return (<time_component_name>, <needs_display_numerical_value>) }
}
- parameters:
- Double: integer part represents the number of complete time components in the considered time interval
- returns:
`(String, Bool)`, where `String` value is name of time component the required declination, `Bool` value sets need to display numerical value of time component in specified string format of time interval
*/
public typealias NKTimeComponentDeclensionRule = (Double) -> (String, Bool)?
NKTextTimeIntervalConfiguration
public struct NKTextTimeIntervalConfiguration {
/**
Return string value if the compared dates are equal
default: "" (empty string)
*/
public var zeroTimeIntervalPlaceholder: String
/*
Output formats is similar to dateFormat of DateFormatter class: the corresponding key value is set to the corresponding value
As example if set `pastFormat = "\(NKTextTimeIntervalConfiguration.numberValueKey) \(NKTextTimeIntervalConfiguration.timeComponentValueKey) ago"` in result will be something like "12 seconds ago"
*/
/**
Key for specifying location of numerical value of time component in string representation of time interval
*/
public static let numberValueKey = "<VALUE>"
/**
Key for specifying location of numerical value of time component in string representation of time interval
*/
public static let timeComponentValueKey = "<TIME_COMPONENT>"
/**
Time intreval output format as string for positive value
If `date1.compare(date2) == .OrderedDescending` => `date1.timeIntervalSince(date2) > 0`
default: "\(NKTextTimeIntervalConfiguration.numberValueKey)"
*/
public var pastFormat: String
/**
Time intreval output format as string for negative value
If `date1.compare(date2) == .OrderedAscending` => `date1.timeIntervalSince(date2) < 0`
default: "\(NKTextTimeIntervalConfiguration.numberValueKey)"
*/
public var futureFormat: String
/*
Declension Rules for each time component
*/
///Declension Rule for seconds
public var seconds: NKTimeComponentDeclensionRule?
///Declension Rule for minutes
public var minutes: NKTimeComponentDeclensionRule?
///Declension Rule for hours
public var hours: NKTimeComponentDeclensionRule?
///Declension Rule for days
public var days: NKTimeComponentDeclensionRule?
///Declension Rule for weeks
public var weeks: NKTimeComponentDeclensionRule?
///Declension Rule for months
public var months: NKTimeComponentDeclensionRule?
///Declension Rule for years
public var years: NKTimeComponentDeclensionRule?
///Declension Rule for centuries
public var centuries: NKTimeComponentDeclensionRule?
/**
Base Init method
*/
public init()
/**
Method calculating count of maximum available time component in input time interval and returns it's string representation based on Declension Rules
Count of maximum available time components calculates in in extension of the class TimeInterval wich implements NKTimeIntervalDateComponents protocol
If `interval` equal zero method returns `zeroTimeIntervalPlaceholder`
If some declension rule isn't setted method use previous rule
If there is no declension rules from maximum available time component to minimum (seconds) method returns nil
- parameters:
- interval: Seconds beetween two dates
- returns: Optional String
*/
public func stringRepresentation(for interval: TimeInterval) -> String?
/**
Method creats time interval string representation based on input rule and time component count
If `value` is equal zero returns `zeroTimeIntervalPlaceholder`
If `value > 0` will be used `pastFormat`, else - `futureFormat`
If `rule` is equal zero returns `nil`
- parameters:
- rule: Declension rule
- value: Time component count (Double used if it needs to make representation for floating point value)
- returns: Optional String
*/
public func formatedString(for rule: NKTimeComponentDeclensionRule?, with value: Double) -> String?
}
//MARK: - NKTextTimeIntervalConfiguration default configurations
/**
This extension contains static methods that generate configurations based on rules for declining the numerals of some languages
*/
public extension NKTextTimeIntervalConfiguration {
/**
Public static method generate default configuration based on rules for declining the numerals of English
*/
public static func defaultEnglish() -> NKTextTimeIntervalConfiguration
/**
Public static method generate default configuration based on rules for declining the numerals of Russian
*/
public static func defaultRussian() -> NKTextTimeIntervalConfiguration
/**
Public static method generate default configuration based on rules for declining the numerals of Ukrainian
*/
public static func defaultUkrainian() -> NKTextTimeIntervalConfiguration
}
NKTimeIntervalDateComponents
和 TimeInterval
扩展
协议 protocol NKTimeIntervalDateComponents {
var seconds: Double { get }
var minutes: Double { get }
var hours: Double { get }
var days: Double { get }
var weeks: Double { get }
var months: Double { get }
var years: Double { get }
var centuries: Double { get }
}
extension TimeInterval: NKTimeIntervalDateComponents { }
NKTimeIntervalTextTransformable
和 Date
扩展
协议 /**
Protocol declares methods for obtaining a time interval between two dates in a string representation using NKTextTimeIntervalConfiguration
*/
public protocol NKTimeIntervalTextTransformable {
/**
Protocol declares methods for obtaining a time interval between two dates in a string representation using NKTextTimeIntervalConfiguration
*/
static func timeIntervalFromNow(to date: Date, with config: NKTextTimeIntervalConfiguration) -> String?
func timeInterval(to date: Date, with config: NKTextTimeIntervalConfiguration) -> String?
}
/**
Implementation of NKTimeIntervalTextTransformable protocol in extension of the class Date
*/
extension Date: NKTimeIntervalTextTransformable {}
示例
要运行示例项目,请克隆仓库,然后首先从示例目录运行 pod install
。
安装
NKTimeIntervalTextTransformation 通过 CocoaPods 提供使用。要安装,只需将以下行添加到您的 Podfile 中
pod 'NKTimeIntervalTextTransformation'
要求
iOS 10.0
Swift 5.0
版本历史
v.0.1.0
v.0.1.1
- 清理及其他小修改
v.0.1.2
- 修复问题
作者
nkopilovskii, [email protected]
许可证
NKTimeIntervalTextTransformation遵循MIT许可证。更多信息请参阅LICENSE文件。