Sha256: 412ddcd251c5a2834f61ac97433036b0a0572425979d1b9150e336db256fa60e

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'time'
require 'active_support/core_ext/numeric/time'

module Dsu
  module Support
    # This module provides functions for formatting Time objects
    # to display in the console.
    module TimeFormatable
      module_function

      # TODO: I18n.
      def formatted_time(time:)
        time = time.localtime if time.utc?

        today_yesterday_or_tomorrow = if time.today?
          'Today'
        elsif time.yesterday?
          'Yesterday'
        elsif time.tomorrow?
          'Tomorrow'
        end

        time_zone = timezone_for(time: time)

        return time.strftime("%A, %Y-%m-%d #{time_zone}") unless today_yesterday_or_tomorrow

        time.strftime("%A, (#{today_yesterday_or_tomorrow}) %Y-%m-%d #{time_zone}")
      end

      # TODO: I18n.
      def mm_dd(time:, separator: '/')
        time.strftime("%m#{separator}%d")
      end

      # TODO: I18n.
      def mm_dd_yyyy(time:, separator: '/')
        time.strftime("%m#{separator}%d#{separator}%Y")
      end

      def timezone_for(time:)
        time.zone
      end

      # TODO: I18n.
      def yyyy_mm_dd_or_through_for(times:)
        return yyyy_mm_dd(time: times[0]) if times.one?

        times = [yyyy_mm_dd(time: times.min), yyyy_mm_dd(time: times.max)]

        I18n.t('information.dates.through', from: times[0], to: times[1])
      end

      # TODO: I18n.
      def yyyy_mm_dd(time:, separator: '-')
        time.strftime("%Y#{separator}%m#{separator}%d")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dsu-2.1.4 lib/dsu/support/time_formatable.rb
dsu-2.1.3 lib/dsu/support/time_formatable.rb
dsu-2.1.2 lib/dsu/support/time_formatable.rb
dsu-2.1.1 lib/dsu/support/time_formatable.rb