Sha256: 03430ad1443ec9490ca946d8797a9f692c99acf13dcce4200e4963b47767078e

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'time'

module Contrast
  module Utils
    # Timer is class that can track state about when an event starts and how long it takes
    # Also contains utility methods to get time values in milliseconds
    class Timer
      # @return [Integer] the ms of the Time that this instance represents
      attr_reader :start_ms

      # Create a wrapper for the current time
      #
      # @param time [Time]
      def initialize time = Time.now
        @start_at = time
        @start_ms = (@start_at.to_f * 1000).to_i
        @events = {}
      end

      # @return [Integer] time, in ms
      def self.now_ms
        (Time.now.to_f * 1000).to_i
      end

      # Return current time in iso8601 format.
      #
      # @return[String]
      def self.time_now
        Time.now.utc.iso8601(2)
      end

      # Converts time given in ms format form TS to HttpDate.
      # Returns time format for If-Modified-Since: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
      # Note: The Time class treats GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
      #
      # @param time [Integer] time in ms.
      def self.ms_to_httpdate time
        Time.at(time / 1000).httpdate unless time.nil?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
contrast-agent-7.6.1 lib/contrast/utils/timer.rb
contrast-agent-7.6.0 lib/contrast/utils/timer.rb
contrast-agent-7.5.0 lib/contrast/utils/timer.rb
contrast-agent-7.4.1 lib/contrast/utils/timer.rb
contrast-agent-7.4.0 lib/contrast/utils/timer.rb