{"id":13880121,"url":"https://github.com/janko/as-duration","last_synced_at":"2025-03-17T01:32:21.377Z","repository":{"id":31077569,"uuid":"34636545","full_name":"janko/as-duration","owner":"janko","description":"Extraction of ActiveSupport::Duration from Rails","archived":false,"fork":false,"pushed_at":"2016-06-24T09:06:35.000Z","size":17,"stargazers_count":126,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-14T02:11:11.508Z","etag":null,"topics":["activesupport","arithmetic","duration","ruby","time"],"latest_commit_sha":null,"homepage":"","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/janko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-26T23:41:03.000Z","updated_at":"2024-02-05T03:12:43.000Z","dependencies_parsed_at":"2022-09-08T20:51:04.143Z","dependency_job_id":null,"html_url":"https://github.com/janko/as-duration","commit_stats":null,"previous_names":["janko-m/as-duration"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janko%2Fas-duration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janko%2Fas-duration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janko%2Fas-duration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janko%2Fas-duration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janko","download_url":"https://codeload.github.com/janko/as-duration/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221670409,"owners_count":16861063,"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":["activesupport","arithmetic","duration","ruby","time"],"created_at":"2024-08-06T08:02:48.013Z","updated_at":"2024-10-27T11:44:15.220Z","avatar_url":"https://github.com/janko.png","language":"Ruby","readme":"# AS::Duration\n\nThis gem is an extraction of `ActiveSupport::Duration` from Rails, along with the\nrelated core extensions.\n\nRuby 2.0 or greater is required.\n\n## Why not simply use ActiveSupport?\n\nIf you're in a Rails project, then you should use `ActiveSupport::Duration`.\nOtherwise there are several reason why you might prefer `as-duration`:\n\n* You simply don't want to have ActiveSupport as a dependency\n* You want to control what you require. You may think that requiring\n  `active_support/core_ext/integer/time` will only require what you want, but\n  in fact it will require a total of **5000 LOC** (a lot of those are additional\n  core extensions which you may not have wanted). `as-duration` has only\n  under **200 LOC**, and only gives you what you've asked for.\n\n## Is it well tested?\n\nIt sure is! I copied all the related tests from Rails, and modified them\nso that they work standalone. So, `as-duration` passes all of Rails' tests.\n\n## Installation\n\n```ruby\ngem 'as-duration'\n```\n\n## Features\n\n*NOTE: In most cases `as-duration` should work exactly like\n`ActiveSupport::Duration`. However, there are a few modifications made, mostly\nremoving some of the magic, see [Modifications](#modifications-to-activesupportduration).*\n\n### Numeric methods\n\nThe following methods are added on `Numeric` (`Float` and `Integer`):\n\n```rb\n# plural versions\n2.seconds\n3.minutes\n4.hours\n5.days\n6.weeks\n7.fortnights\n8.months\n9.years\n\n# singular versions\n1.second\n1.minute\n1.hour\n1.day\n1.week\n1.fortnight\n1.month\n1.year\n```\n\nThe only exception is `#months` and `#years` which are only added to `Integer`\n(to maintain precision in calculations).\n\n### Duration/Time arithmetics\n\nYou can add and subtract durations from `Time` or `Date` objects.\n\n```rb\nTime.now + 2.hours\nDate.today + 1.year\n```\n\nWhen you add seconds/minutes/hours to a Date, the Date is automatically\nconverted to a `Time` object.\n\n```rb\n(Date.today + 1.minute).class #=\u003e Time\n```\n\nAs syntax sugar, you can also call time methods on the duration object:\n\n```rb\n# forward in time\n1.year.from_now\n2.months.since(Date.new(2015,4,27))\n2.months.after(Date.new(2015,4,27))\n2.months.from(Date.new(2015,4,27))\n\n# back in time\n2.hours.ago\n20.minutes.until(Time.now)\n20.minutes.before(Time.now)\n20.minutes.to(Time.now)\n```\n\n### Duration/Duration arithmetics\n\nYou can add and subtract durations:\n\n```rb\n1.week + 1.day\n2.minutes - 1.second\n```\n\nUnlike `ActiveSupport::Duration`, you can't add durations to integers and vice\nversa. You either have to convert the integer to a duration, or\nthe duration to an integer with `AS::Duration#to_i`. This is to help you\nnot to mix different time units.\n\n```rb\n# Bad\n10 + 1.minute  # TypeError\n1.minute + 10  # TypeError\n\n# Good\n10.seconds + 1.minute  # AS::Duration\n1.minute.to_i + 10     # Integer\n```\n\n## Modifications to `ActiveSupport::Duration`\n\nThe behaviour of `ActiveSupport::Duration` has been slightly modified, mostly\nto remove some magic:\n\n* Added `#from`, `#after`, `#before` and `#to` to `AS::Duration`\n* `#from_now` and `#ago` cannot take any arguments, they always use the current\n  time (passing an argument doesn't read well, better to use `#from` and\n  `#until`)\n* Removed support for `DateTime`\n  - `DateTime` was first introduced in Ruby so that you can represent time\n    that the `Time` class at the moment wasn't able to. However, the `Time`\n    class improved over time and removed those limitations, so there is no more\n    need to use `DateTime`\n* Year lasts 365 days instead of 365.25\n* `AS::Duration` doesn't act like an Integer\n  - to compare it with an integer you have to either convert the integer to\n    a duration or convert the duration to an integer (with `#to_i`)\n  - you can only add and subtract two duration objects\n* Removed hash equality\n\n## License\n\n[MIT](LICENSE.txt)\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanko%2Fas-duration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanko%2Fas-duration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanko%2Fas-duration/lists"}