<commit_before>//
//  TimestampLogFormatter.swift
//  CleanroomLogger
//
//  Created by <NAME> on 1/4/16.
//  Copyright © 2016 Gilt Groupe. All rights reserved.
//

import Foundation

/**
 Encapsulates the various formatting styles that can be used by the
 `TimestampLogFormatter`.
 */
public enum TimestampStyle
{
    /** Specifies a timestamp style that uses the date format string
     "yyyy-MM-dd HH:mm:ss.SSS zzz". */
    case `default`

    /** Specifies a UNIX timestamp indicating the number of seconds elapsed
    since January 1, 1970. */
    case unix

    /** Specifies a custom date format. */
    case custom(String)
}

extension TimestampStyle
{
    fileprivate var dateFormat: String? {
        switch self {
        case .default:          return "yyyy-MM-dd HH:mm:ss.SSS zzz"
        case .unix:             return nil
        case .custom(let fmt):  return fmt
        }
    }

    fileprivate var formatter: DateFormatter? {
        guard let format = dateFormat else {
            return nil
        }

        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter
    }

    fileprivate func string(from date: Date, using formatter: DateFormatter?)
        -> String
    {
        switch self {
        case .unix:     return "\(date.timeIntervalSince1970)"
        default:        return formatter!.string(from: date)
        }
    }
}

/**
 A `LogFormatter` that returns a string representation of a `LogEntry`'s
 `timestamp` property.

 This is typically combined with other `LogFormatter`s within a
 `ConcatenatingLogFormatter`.
 */
public struct TimestampLogFormatter: LogFormatter
{
    /** The `TimestampStyle` that determines how the receiver will format
     its output. */
    public let style: TimestampStyle

    private let formatter: DateFormatter?<commit_msg>avoid the evil string interpolation<commit_after>//
//  TimestampLogFormatter.swift
//  CleanroomLogger
//
//  Created by <NAME> on 1/4/16.
//  Copyright © 2016 Gilt Groupe. All rights reserved.
//

import Foundation

/**
 Encapsulates the various formatting styles that can be used by the
 `TimestampLogFormatter`.
 */
public enum TimestampStyle
{
    /** Specifies a timestamp style that uses the date format string
     "yyyy-MM-dd HH:mm:ss.SSS zzz". */
    case `default`

    /** Specifies a UNIX timestamp indicating the number of seconds elapsed
    since January 1, 1970. */
    case unix

    /** Specifies a custom date format. */
    case custom(String)
}

extension TimestampStyle
{
    fileprivate var dateFormat: String? {
        switch self {
        case .default:          return "yyyy-MM-dd HH:mm:ss.SSS zzz"
        case .unix:             return nil
        case .custom(let fmt):  return fmt
        }
    }

    fileprivate var formatter: DateFormatter? {
        guard let format = dateFormat else {
            return nil
        }

        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter
    }

    fileprivate func string(from date: Date, using formatter: DateFormatter?)
        -> String
    {
        switch self {
        case .unix:     return String(describing: date.timeIntervalSince1970)
        default:        return formatter!.string(from: date)
        }
    }
}

/**
 A `LogFormatter` that returns a string representation of a `LogEntry`'s
 `timestamp` property.

 This is typically combined with other `LogFormatter`s within a
 `ConcatenatingLogFormatter`.
 */
public struct TimestampLogFormatter: LogFormatter
{
    /** The `TimestampStyle` that determines how the receiver will format
     its output. */
    public let style: TimestampStyle

    private let formatter: DateFormatter?
{'fraction_non_alphanumeric': 0.08461331177580167, 'fraction_numerical': 0.009161950956615468, 'mean_word_length': 2.4594594594594597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7538880', 'n_tokens_mistral': 1055, 'n_tokens_neox': 989, 'n_words': 356}