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 +4 -4
- data/README.md +77 -20
- data/demo.rb +37 -1
- data/lib/time_receive/version.rb +1 -1
- data/lib/time_receive.rb +78 -9
- data/time_receive-0.1.0.gem +0 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf213c717465c562d2092fd20ba4b51c776d4fa2ff44d923a3f0533da7b8117c
|
4
|
+
data.tar.gz: 6581aa3352574e774bc60d97c101af7058f9b50ada18246a3b8a0fcb07180d48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
4
|
+
**time_receive**
|
8
5
|
|
9
|
-
|
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
|
-
|
8
|
+
**Features:**
|
12
9
|
|
13
|
-
|
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
|
-
|
18
|
+
**Installation**
|
16
19
|
|
17
|
-
|
20
|
+
1. Add the gem to your Gemfile:
|
18
21
|
|
19
|
-
|
22
|
+
```ruby
|
23
|
+
gem 'time_receive'
|
24
|
+
```
|
20
25
|
|
21
|
-
|
26
|
+
2. Install the gem:
|
22
27
|
|
23
|
-
|
28
|
+
```bash
|
29
|
+
bundle install
|
30
|
+
```
|
24
31
|
|
25
|
-
|
32
|
+
**Usage**
|
26
33
|
|
27
|
-
|
34
|
+
1. Require the `time_receive` module in your Ruby code:
|
28
35
|
|
29
|
-
|
36
|
+
```ruby
|
37
|
+
require 'time_receive'
|
38
|
+
```
|
30
39
|
|
31
|
-
|
40
|
+
2. Use the provided methods:
|
32
41
|
|
33
|
-
|
42
|
+
**Formatting Time:**
|
34
43
|
|
35
|
-
|
44
|
+
```ruby
|
45
|
+
formatted_time = TimeReceive.now("%d/%m/%Y %H:%M") # Output: 28/04/2024 00:58
|
46
|
+
```
|
36
47
|
|
37
|
-
|
48
|
+
**Parsing Time String:**
|
38
49
|
|
39
|
-
|
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
|
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}"
|
data/lib/time_receive/version.rb
CHANGED
data/lib/time_receive.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
|
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 =
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
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:
|
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-
|
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
|
-
|
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:
|