Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c79f437eb09c
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6d1a6a71880b
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Oct 26, 2017

  1. Relocate and document Time::DayOfWeek

    Consolidate `Time::DayOfWeek` definition inside `Time`, adding documentation
    and examples on its usage.
    
    Refactor day question methods (ie `#monday?`) macro to use Enum's constants
    and question methods, avoiding duplication of definitions.
    
    This change does not introduce performance penalties by itself, but a
    synthetic benchmark might present it otherwise:
    
        day_of_week.value == index: 626.33M (   1.6ns) (±10.72%)       fastest
             day_of_week.question?: 621.56M (  1.61ns) (±10.35%)  1.01× slower
    
        dow.value == index: 622.68M (  1.61ns) (± 9.82%)  1.00× slower
             dow.question?: 624.23M (   1.6ns) (±10.82%)       fastest
    
    Benchmark code:
    
        require "benchmark"
    
        time = Time.new(2016, 2, 15)
        dow = time.day_of_week
    
        # benchmark against `day_of_week` being computed each time
        Benchmark.ips do |results|
          results.report("day_of_week.value == index:") { time.day_of_week.value == 1 }
          results.report("day_of_week.question?:") { time.day_of_week.monday? }
        end
    
        # benchmark against cached `day_of_week`
        Benchmark.ips do |results|
          results.report("dow.value == index:") { dow.value == 1 }
          results.report("dow.question?:") { dow.monday? }
        end
    luislavena committed Oct 26, 2017
    Copy the full SHA
    2006b44 View commit details

Commits on Oct 27, 2017

  1. Merge pull request #5188 from luislavena/refactor-day-of-the-week

    Relocate and document Time::DayOfWeek
    asterite authored Oct 27, 2017
    Copy the full SHA
    6d1a6a7 View commit details
Showing with 25 additions and 11 deletions.
  1. +25 −2 src/time.cr
  2. +0 −9 src/time/day_of_week.cr
27 changes: 25 additions & 2 deletions src/time.cr
Original file line number Diff line number Diff line change
@@ -103,6 +103,29 @@ struct Time
# :nodoc:
MAX_SECONDS = 315537897599_i64

# `DayOfWeek` represents the day.
#
# ```
# time = Time.new(2016, 2, 15)
# time.day_of_week # => Monday
# ```
#
# Alternatively, you can use question methods:
#
# ```
# time.friday? # => false
# time.monday? # => true
# ```
enum DayOfWeek
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
end

# `Kind` represents a specified time zone.
#
# Initializing a `Time` instance with specified `Kind`:
@@ -621,10 +644,10 @@ struct Time
Time.new(year, month, day, 12, 0, 0, nanosecond: 0, kind: kind)
end

{% for name, index in %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) %}
{% for name in DayOfWeek.constants %}
# Does `self` happen on {{name.id}}?
def {{name.id.downcase}}? : Bool
day_of_week.value == {{index}}
day_of_week.{{name.id.downcase}}?
end
{% end %}

9 changes: 0 additions & 9 deletions src/time/day_of_week.cr

This file was deleted.