Sha256: 46bbd3f645c87a69d2943fad2806d1cf8490ab135fab2f5b89d99045be99c487

Contents?: true

Size: 1.13 KB

Versions: 9

Compression:

Stored size: 1.13 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

      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

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

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

      def timezone_for(time:)
        time.zone
      end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dsu-2.0.8 lib/dsu/support/time_formatable.rb
dsu-2.0.7 lib/dsu/support/time_formatable.rb
dsu-2.0.6 lib/dsu/support/time_formatable.rb
dsu-2.0.5 lib/dsu/support/time_formatable.rb
dsu-2.0.4 lib/dsu/support/time_formatable.rb
dsu-2.0.3 lib/dsu/support/time_formatable.rb
dsu-2.0.2 lib/dsu/support/time_formatable.rb
dsu-2.0.1 lib/dsu/support/time_formatable.rb
dsu-2.0.0.alpha.1 lib/dsu/support/time_formatable.rb