{"id":13879777,"url":"https://github.com/Freaky/monotime","last_synced_at":"2025-07-16T15:33:05.270Z","repository":{"id":49760779,"uuid":"151284149","full_name":"Freaky/monotime","owner":"Freaky","description":"A sensible interface to monotonic time in Ruby","archived":false,"fork":false,"pushed_at":"2023-10-19T23:37:20.000Z","size":138,"stargazers_count":156,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-08T09:22:04.638Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Freaky.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-10-02T16:10:08.000Z","updated_at":"2024-07-30T07:20:41.000Z","dependencies_parsed_at":"2024-02-19T02:46:28.079Z","dependency_job_id":null,"html_url":"https://github.com/Freaky/monotime","commit_stats":{"total_commits":119,"total_committers":3,"mean_commits":"39.666666666666664","dds":0.03361344537815125,"last_synced_commit":"3732624980a4b5496951cae186619f417e27daa3"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fmonotime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fmonotime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fmonotime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fmonotime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Freaky","download_url":"https://codeload.github.com/Freaky/monotime/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226064568,"owners_count":17568035,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T08:02:32.766Z","updated_at":"2024-11-24T08:31:47.733Z","avatar_url":"https://github.com/Freaky.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"\n# Monotime\n\nA sensible interface to Ruby's monotonic clock, inspired by Rust.\n\n[![Gem Version](https://badge.fury.io/rb/monotime.svg)](https://badge.fury.io/rb/monotime)\n[![Build Status](https://github.com/Freaky/monotime/actions/workflows/ci.yml/badge.svg)](https://github.com/Freaky/monotime/actions)\n[![Inline docs](http://inch-ci.org/github/Freaky/monotime.svg?branch=master)](http://inch-ci.org/github/Freaky/monotime)\n[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/monotime)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'monotime'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install monotime\n\n`Monotime` is tested on Ruby 2.7+, TruffleRuby, and JRuby.\n\n## Usage\n\n```ruby\nrequire 'monotime'\n# or, to automatically include Monotime::* in the global scope,\n# as used by these examples:\nrequire 'monotime/include'\n```\n\n`Monotime` offers a `Duration` type for describing spans of time, and an\n`Instant` type for describing points in time.  Both operate at nanosecond\nresolution to the limits of whatever your Ruby implementation supports.\n\nFor example, to measure an elapsed time, either create an `Instant` to mark the\nstart point, perform the action and then ask for the `Duration` that has elapsed\nsince:\n\n```ruby\nstart = Instant.now\ndo_something\nelapsed = start.elapsed\n```\n\nOr use a convenience method:\n\n```ruby\nelapsed = Duration.measure { do_something }\n# or\nreturn_value, elapsed = Duration.with_measure { compute_something }\n```\n\n`Duration` offers formatting:\n\n```ruby\nDuration.millis(42).to_s       # =\u003e \"42ms\"\nDuration.nanos(12345).to_s     # =\u003e \"12.345μs\"\nDuration.secs(1.12345).to_s(2) # =\u003e \"1.12s\"\n```\n\nConversions:\n\n```ruby\nDuration.secs(10).millis    # =\u003e 10000.0\nDuration.micros(12345).secs # =\u003e 0.012345\n```\n\nAnd basic mathematical operations:\n\n```ruby\n(Duration.millis(42) + Duration.secs(1)).to_s  # =\u003e \"1.042s\"\n(Duration.millis(42) - Duration.secs(1)).to_s  # =\u003e \"-958ms\"\n(Duration.secs(42) * 2).to_s                   # =\u003e \"84s\"\n(Duration.secs(42) / 2).to_s                   # =\u003e \"21s\"\n```\n\n`Instant` does some simple maths too:\n\n```ruby\n# Instant - Duration =\u003e Instant\n(Instant.now - Duration.secs(1)).elapsed.to_s      # =\u003e \"1.000014627s\"\n\n# Instant - Instant =\u003e Duration\n(Instant.now - Instant.now).to_s                   # =\u003e \"-5.585μs\"\n```\n\n`Duration` and `Instant` are also `Comparable` with other instances of their\ntype, and can be used in hashes, sets, and similar structures.\n\n## Sleeping\n\n`Duration` can be used to sleep a thread, assuming it's positive (time travel\nis not yet implemented):\n\n```ruby\n# Equivalent\nsleep(Duration.secs(1).secs)  # =\u003e 1\nDuration.secs(1).sleep        # =\u003e 1\n```\n\nSo can `Instant`, taking a `Duration` and sleeping until the given `Duration`\npast the time the `Instant` was created, if any. This can be useful for\nmaintaining a precise cadence between tasks:\n\n```ruby\ninterval = Duration.secs(60)\nstart = Instant.now\nloop do\n  do_stuff\n  start.sleep(interval)\n  start += interval\nend\n```\n\nOr you can declare an `Instant` in the future and sleep to that point:\n\n```ruby\ninterval = Duration.secs(60)\ndeadline = Instant.now + interval\nloop do\n  do_stuff\n  deadline.sleep\n  deadline += interval\nend\n```\n\n`Instant#sleep` returns a `Duration` which was slept, or a negative `Duration`\nindicating that the desired sleep point was in the past.\n\n## Duration duck typing\n\nOperations taking a `Duration` can also accept any type which implements\n`#to_nanos`, returning an (Integer) number of nanoseconds the value represents.\n\nFor example, to treat built-in numeric types as second durations, you could do:\n\n```ruby\nclass Numeric\n  def to_nanos\n    Integer(self * 1_000_000_000)\n  end\nend\n\n(Duration.secs(1) + 41).to_s  # =\u003e \"42s\"\n(Instant.now - 42).to_s       # =\u003e \"42.000010545s\"\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at \u003chttps://github.com/Freaky/monotime\u003e.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## See Also\n\n### Core Ruby\n\nFor a zero-dependency alternative upon which `monotime` is based, see\n[`Process.clock_gettime`](https://www.rubydoc.info/stdlib/core/Process:clock_gettime).\n\n`Process::CLOCK_MONOTONIC` is a safe default, but other options may offer higher\nresolution or alternative behaviour in light of system suspend/resume or NTP\nfrequency skew.\n\n### Other Gems\n\n[hitimes](https://rubygems.org/gems/hitimes) is a popular and mature alternative\nwhich also includes a variety of features for gathering statistics about\nmeasurements.\n\n[concurrent-ruby](https://rubygems.org/gems/concurrent-ruby) includes\n`Concurrent.monotonic_time`, which is at the time of writing a trivial proxy to\nthe aforementioned `Process::clock_gettime` with `Process::CLOCK_MONOTONIC`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFreaky%2Fmonotime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFreaky%2Fmonotime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFreaky%2Fmonotime/lists"}