time_receive 0.1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f43dbf049358afa75fdb3be5f39dcbaa6e7c86ada380505cda9a638a67a846a
4
- data.tar.gz: f669662a4606c28752a9d809af03e70610b8bc18d2cc8b14e39b847ba74852d7
3
+ metadata.gz: bf213c717465c562d2092fd20ba4b51c776d4fa2ff44d923a3f0533da7b8117c
4
+ data.tar.gz: 6581aa3352574e774bc60d97c101af7058f9b50ada18246a3b8a0fcb07180d48
5
5
  SHA512:
6
- metadata.gz: bccb5a95442f89e18e2dd594dd1fc5320485f0867cd589e31db253a34ab126c4eae1c43b0d6fa2e366bc6643786fb022c995d950f658d72960a2c10092e22bc9
7
- data.tar.gz: dd22477c9dc4aa18a931fd9b2c7c9b9e5e7152dd161650d135375f92dae0e046f367dc91309e402061cdc887a375adb977c1e6bc0fcc5916ad0f11c8673a72a4
6
+ metadata.gz: '0802eb0421d45e6539b32151479e5fbfc3a7fde7a7579d3049b54a17b9c5fef8d152334e34e6920e902b5559308df8ddeb60120647bb20c36ce02ff5af0b5a60'
7
+ data.tar.gz: 2a734b5273ecd746f562bbe8ab12a029887711df4359b2f85add02bb79854975e3e0485f194c08def698167b5186d76650f0b51d64629374d4c5ea39a089169f
data/README.md CHANGED
@@ -1,39 +1,96 @@
1
- # TimeReceive
2
1
 
3
- TODO: Delete this and the text below, and describe your gem
4
2
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/time_receive`. To experiment with that code, run `bin/console` for an interactive prompt.
6
3
 
7
- ## Installation
4
+ **time_receive**
8
5
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
6
+ This Ruby gem provides a collection of utility methods for working with time-related tasks, including formatting, parsing, countdown timers, and date calculations.
10
7
 
11
- Install the gem and add to the application's Gemfile by executing:
8
+ **Features:**
12
9
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+ * Format time objects into various string representations using custom formats.
11
+ * Parse time strings into Time objects using a specified format.
12
+ * Create countdown timers that display the remaining time until a user-defined deadline.
13
+ * Check if a time falls on the current date.
14
+ * Calculate the number of days until a target date.
15
+ * Add specific time periods (hours, minutes, or days) to a Time object.
16
+ * Calculate the elapsed time between two Time objects.
14
17
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
18
+ **Installation**
16
19
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
20
+ 1. Add the gem to your Gemfile:
18
21
 
19
- ## Usage
22
+ ```ruby
23
+ gem 'time_receive'
24
+ ```
20
25
 
21
- TODO: Write usage instructions here
26
+ 2. Install the gem:
22
27
 
23
- ## Development
28
+ ```bash
29
+ bundle install
30
+ ```
24
31
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+ **Usage**
26
33
 
27
- To 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+ 1. Require the `time_receive` module in your Ruby code:
28
35
 
29
- ## Contributing
36
+ ```ruby
37
+ require 'time_receive'
38
+ ```
30
39
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/time_receive. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/time_receive/blob/master/CODE_OF_CONDUCT.md).
40
+ 2. Use the provided methods:
32
41
 
33
- ## License
42
+ **Formatting Time:**
34
43
 
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+ ```ruby
45
+ formatted_time = TimeReceive.now("%d/%m/%Y %H:%M") # Output: 28/04/2024 00:58
46
+ ```
36
47
 
37
- ## Code of Conduct
48
+ **Parsing Time String:**
38
49
 
39
- Everyone interacting in the TimeReceive project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/time_receive/blob/master/CODE_OF_CONDUCT.md).
50
+ ```ruby
51
+ time_object = TimeReceive.parse_time("2024-05-09 10:00:00", "%Y-%m-%d %H:%M:%S")
52
+ ```
53
+
54
+ **Countdown Timer:**
55
+
56
+ ```ruby
57
+ deadline = Time.now + 60 * 60 # One hour from now
58
+ TimeReceive.timer(deadline) do
59
+ puts "Time is up!"
60
+ end
61
+ ```
62
+
63
+ **Other Methods:**
64
+
65
+ See the code documentation for details on `today?`, `days_until`, `add_time_period`, and `calculate_elapsed_time`.
66
+
67
+ **Error Handling**
68
+
69
+ `TimeReceive` raises `TimeReceiveError` if invalid arguments are provided to methods. Ensure you pass appropriate types (e.g., `Time` objects, `Date` objects, valid time strings, and supported time periods).
70
+
71
+ **Contributing**
72
+
73
+ We welcome contributions to this project! Feel free to fork the repository, make changes, and submit a pull request.
74
+
75
+ **License**
76
+
77
+ This gem is licensed under the MIT License. See the LICENSE file for details.
78
+ ```
79
+
80
+ **Explanation:**
81
+
82
+ * **Clear and concise title:** The title "time_receive" is straightforward.
83
+ * **Informative description:** The description explains the gem's purpose and key features.
84
+ * **Detailed installation steps:** The guide covers adding the gem to the Gemfile and running `bundle install`.
85
+ * **Comprehensive usage examples:** The examples demonstrate various functionalities with clear explanations.
86
+ * **Error handling guidance:** Users are informed about potential errors and how to avoid them.
87
+ * **Contribution guidelines (optional):** If you plan to accept contributions, include instructions.
88
+ * **License information:** Specify the license under which the gem is distributed.
89
+
90
+ **Additional Tips:**
91
+
92
+ * Consider adding a badge for the Ruby version supported by the gem.
93
+ * Include links to the source code repository (if applicable) and any relevant documentation.
94
+ * Maintain up-to-date documentation as the gem evolves.
95
+
96
+ I hope this enhanced README file serves you well!
data/demo.rb CHANGED
@@ -1,6 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/time_receive'
3
+ require_relative "lib/time_receive"
4
+
5
+ start_time = Time.now
6
+ puts "Start time: #{start_time}"
7
+
8
+ new_time = TimeReceive.add_time_period(start_time, :hours)
9
+ puts "Time after adding 1 hour: #{new_time}"
10
+
11
+ new_time = TimeReceive.add_time_period(start_time, :days)
12
+ puts "Time after adding 1 day: #{new_time}"
13
+
14
+ # Пример использования метода calculate_elapsed_time
15
+ start_time = Time.now
16
+ puts "Start time: #{start_time}"
17
+ sleep 3 # Допустим, что прошло 3 секунды
18
+
19
+ end_time = Time.now
20
+ puts "End time: #{end_time}"
21
+
22
+ elapsed_time = TimeReceive.calculate_elapsed_time(start_time, end_time)
23
+ puts "Elapsed time: #{elapsed_time}"
24
+
25
+ puts TimeReceive.today? # Check if today
26
+
27
+ target_date = Date.parse("2024-05-10")
28
+ days_remaining = TimeReceive.days_until(target_date)
29
+ puts "Days until #{target_date}: #{days_remaining}"
30
+
31
+ user_time = Time.parse("2024-05-01 12:00:00") # Replace with user-provided time
32
+ remaining_seconds = 3720 # 1 hour, 2 minutes, and 0 seconds
33
+
34
+ formatted_time = TimeReceive.format_remaining_time(remaining_seconds)
35
+ puts formatted_time # Output: "01:02:00"
36
+
37
+ TimeReceive.timer(user_time) do
38
+ puts "Время истекло!"
39
+ end
4
40
 
5
41
  current_time = TimeReceive.now
6
42
  puts "Current time (default format): #{current_time}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TimeReceive
4
- VERSION = "0.1.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/time_receive.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'date'
2
- require 'time'
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "time"
3
5
 
4
6
  module TimeReceive
5
7
  class TimeReceiveError < StandardError; end
@@ -11,24 +13,91 @@ module TimeReceive
11
13
 
12
14
  def format(time)
13
15
  raise TimeReceiveError, "Invalid time: #{time}" unless time.is_a?(Time)
14
- time.strftime(@format_string) # Используйте strftime для форматирования времени
16
+
17
+ time.strftime(@format_string)
15
18
  end
16
19
  end
17
20
 
18
21
  module ClassMethods
19
22
  def now(format_string = nil)
20
- formatter = format_string ? TimeFormatter.new(format_string) : TimeFormatter.new("%Y-%m-%d %H:%M:%S")
23
+ formatter = TimeFormatter.new(format_string || "%Y-%m-%d %H:%M:%S")
21
24
  formatter.format(Time.now)
22
25
  end
23
26
 
24
27
  def parse_time(time_string, format_string)
25
- begin
26
- Time.strptime(time_string, format_string)
27
- rescue ArgumentError
28
- raise TimeReceiveError, "Invalid time string: #{time_string}"
28
+ Time.strptime(time_string, format_string)
29
+ rescue ArgumentError
30
+ raise TimeReceiveError, "Invalid time string: #{time_string}"
31
+ end
32
+
33
+ def timer(user_time, &block)
34
+ raise ArgumentError, "Invalid user_time: #{user_time}" unless user_time.is_a?(Time)
35
+
36
+ remaining_seconds = (user_time - Time.now).to_i
37
+
38
+ loop do
39
+ remaining_time_str = format_remaining_time(remaining_seconds)
40
+
41
+ puts "Time until deadline: #{remaining_time_str}"
42
+
43
+ sleep 1 / 1.9
44
+
45
+ remaining_seconds -= 1
46
+
47
+ break if remaining_seconds <= 0
48
+ end
49
+
50
+ block.call if block_given?
51
+ end
52
+
53
+ def format_remaining_time(remaining_seconds)
54
+ hours = remaining_seconds / 3600
55
+ minutes = (remaining_seconds % 3600) / 60
56
+ seconds = remaining_seconds % 60
57
+
58
+ hours_str = hours.to_s.rjust(2, "0")
59
+ minutes_str = minutes.to_s.rjust(2, "0")
60
+ seconds_str = seconds.to_s.rjust(2, "0")
61
+
62
+ "#{hours_str}:#{minutes_str}:#{seconds_str}"
63
+ end
64
+
65
+ def today?
66
+ Date.today == Time.now.to_date
67
+ end
68
+
69
+ def days_until(target_date)
70
+ raise ArgumentError, "Invalid target_date: #{target_date}" unless target_date.is_a?(Date)
71
+
72
+ (target_date - Date.today).to_i
73
+ end
74
+
75
+ def add_time_period(time, period)
76
+ raise ArgumentError, "Invalid time: #{time}" unless time.is_a?(Time)
77
+
78
+ case period
79
+ when :hours
80
+ time + 3600
81
+ when :minutes
82
+ time + 60
83
+ when :days
84
+ time + 86_400
85
+ else
86
+ raise ArgumentError, "Unsupported period: #{period}"
29
87
  end
30
88
  end
89
+
90
+ def calculate_elapsed_time(start_time, end_time)
91
+ raise ArgumentError, "Invalid start_time: #{start_time}" unless start_time.is_a?(Time)
92
+ raise ArgumentError, "Invalid end_time: #{end_time}" unless end_time.is_a?(Time)
93
+
94
+ elapsed_seconds = (end_time - start_time).to_i
95
+ hours = elapsed_seconds / 3600
96
+ minutes = (elapsed_seconds % 3600) / 60
97
+
98
+ "#{hours} hours and #{minutes} minutes"
99
+ end
31
100
  end
32
101
 
33
- self.extend ClassMethods
102
+ extend ClassMethods
34
103
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_receive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MatveySviadysh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-24 00:00:00.000000000 Z
11
+ date: 2024-04-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  The time_receive gem provides a way to ... (brief explanation of what the gem does).
@@ -32,10 +32,13 @@ files:
32
32
  - lib/time_receive.rb
33
33
  - lib/time_receive/version.rb
34
34
  - sig/time_receive.rbs
35
- homepage: https://github.com/bodrovis/lokalise_rails
35
+ - time_receive-0.1.0.gem
36
+ homepage: https://github.com/MatveySviadysh/time_receive
36
37
  licenses:
37
38
  - MIT
38
- metadata: {}
39
+ metadata:
40
+ homepage_uri: https://github.com/MatveySviadysh/time_receive
41
+ source_code_uri: https://github.com/MatveySviadysh/time_receive
39
42
  post_install_message:
40
43
  rdoc_options: []
41
44
  require_paths: