friendly_time 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20119292c999eaf4d0d9438a7b027949b109608d
4
+ data.tar.gz: d8ee74ea5960fbdbcc825a8e8cabb29ccd37f141
5
+ SHA512:
6
+ metadata.gz: 7a4932ba530bb3115c402bfb838597c73d6f26792d979ca8e2e3a0842b19ee43066623585949025c4f548d58460fe166522c977b80838316f1159254aadfc172
7
+ data.tar.gz: f730bdd8bcd46eb32bb0785c419d49daaa24dc0210867ed176ee5ed4c007559ba935551ac930cad00b6ca9e560f909ee6beae969b63514bb7e25f54494b58d65
data/bin/friendly_time ADDED
@@ -0,0 +1,38 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/friendly_time.rb'
5
+ require_relative '../lib/version.rb'
6
+
7
+ options = {:from_time=>(Time.now.to_i * 1000), :to_time=>-1}
8
+
9
+ option_parser = OptionParser.new do |opts|
10
+ executable_name = File.basename($PROGRAM_NAME)
11
+ opts.banner = "\tProvides casual time strings, such as 'about a minute ago'
12
+
13
+ Usage: #{executable_name} [options]"
14
+
15
+ opts.on("-f TIME_IN_MILLIS", "--from_time TIME_IN_MILLIS", "the time to measure from. If omitted, the current time is used.", /^-?[0-9]+$/) do |time_in_millis|
16
+ options[:from_time] = time_in_millis.to_i
17
+ end
18
+
19
+ opts.on("-t TIME_IN_MILLIS", "--to_time TIME_IN_MILLIS", "the time to measure to. Required.", /^-?[0-9]+$/) do |time_in_millis|
20
+ options[:to_time] = time_in_millis.to_i
21
+ end
22
+
23
+ opts.on("-v", "--version", "version number. All other options will be ignored.") do
24
+ puts "Version #{Friendly_Time::VERSION}"
25
+ exit
26
+ end
27
+ end
28
+
29
+ option_parser.parse!
30
+
31
+ if options[:to_time].eql?(-1)
32
+ STDERR.puts "ERROR: the '--to_time' argument is required"
33
+ puts option_parser.help
34
+ # raise OptionParser::MissingArgument
35
+ exit 1
36
+ end
37
+
38
+ puts FriendlyTime::FriendlyTimeProvider.friendly_time options[:from_time], options[:to_time]
@@ -0,0 +1,160 @@
1
+ module FriendlyTime
2
+ class FriendlyTimeProvider
3
+ @one_minute_in_seconds = 60
4
+ @one_hour_in_seconds = @one_minute_in_seconds * 60
5
+ @one_day_in_seconds = @one_hour_in_seconds * 24
6
+ @one_week_in_seconds = @one_day_in_seconds * 7
7
+ @one_month_in_seconds = @one_day_in_seconds * 28
8
+ @one_year_in_seconds = @one_month_in_seconds * 12
9
+
10
+ def self.friendly_time(from_time, to_time)
11
+ timeInSecs = (from_time - to_time) / 1000
12
+
13
+ if timeInSecs.abs < @one_minute_in_seconds
14
+ if timeInSecs > 0
15
+ "seconds ago"
16
+ else
17
+ "in less than a minute"
18
+ end
19
+ elsif timeInSecs.abs < @one_minute_in_seconds * 2
20
+ if timeInSecs > 0
21
+ "about a minute ago"
22
+ else
23
+ "in about a minute"
24
+ end
25
+ elsif timeInSecs.abs < @one_minute_in_seconds * 10
26
+ if timeInSecs > 0
27
+ "minutes ago"
28
+ else
29
+ "in a few minutes"
30
+ end
31
+ elsif timeInSecs.abs < @one_minute_in_seconds * 15
32
+ if timeInSecs > 0
33
+ "about 10 minutes ago"
34
+ else
35
+ "in about 10 minutes"
36
+ end
37
+ elsif timeInSecs.abs < @one_minute_in_seconds * 20
38
+ if timeInSecs > 0
39
+ "about 15 minutes ago"
40
+ else
41
+ "in about 15 minutes"
42
+ end
43
+ elsif timeInSecs.abs < @one_minute_in_seconds * 25
44
+ if timeInSecs > 0
45
+ "about 20 minutes ago"
46
+ else
47
+ "in about 20 minutes"
48
+ end
49
+ elsif timeInSecs.abs < @one_minute_in_seconds * 30
50
+ if timeInSecs > 0
51
+ "about 25 minutes ago"
52
+ else
53
+ "in about 25 minutes"
54
+ end
55
+ elsif timeInSecs.abs < @one_minute_in_seconds * 45
56
+ if timeInSecs > 0
57
+ "about half an hour ago"
58
+ else
59
+ "in about half an hour"
60
+ end
61
+ elsif timeInSecs.abs < @one_hour_in_seconds
62
+ if timeInSecs > 0
63
+ "about 45 minutes ago"
64
+ else
65
+ "in about 45 minutes"
66
+ end
67
+ elsif timeInSecs.abs < @one_hour_in_seconds * 2
68
+ if timeInSecs > 0
69
+ "about an hour ago"
70
+ else
71
+ "in about an hour"
72
+ end
73
+ elsif timeInSecs.abs < @one_hour_in_seconds * 3
74
+ if timeInSecs > 0
75
+ "a couple of hours ago"
76
+ else
77
+ "in a couple of hours"
78
+ end
79
+ elsif timeInSecs.abs < @one_hour_in_seconds * 12
80
+ if timeInSecs > 0
81
+ "a few hours ago"
82
+ else
83
+ "in a few hours"
84
+ end
85
+ elsif timeInSecs.abs < @one_hour_in_seconds * 18
86
+ if timeInSecs > 0
87
+ "about 12 hours ago"
88
+ else
89
+ "in about 12 hours"
90
+ end
91
+ elsif timeInSecs.abs < @one_day_in_seconds
92
+ if timeInSecs > 0
93
+ "about 18 hours ago"
94
+ else
95
+ "in about 18 hours"
96
+ end
97
+ elsif timeInSecs.abs < @one_day_in_seconds * 2
98
+
99
+ if timeInSecs > 0
100
+ "yesterday"
101
+ else
102
+ "tomorrow"
103
+ end
104
+ elsif timeInSecs.abs < @one_day_in_seconds * 3
105
+ if timeInSecs > 0
106
+ "a couple of days ago"
107
+ else
108
+ "in a couple of days"
109
+ end
110
+
111
+ elsif timeInSecs.abs < @one_week_in_seconds
112
+ if timeInSecs > 0
113
+ "days ago"
114
+ else
115
+ "in a few days"
116
+ end
117
+ elsif timeInSecs.abs < @one_week_in_seconds * 2
118
+ if timeInSecs > 0
119
+ "about a week ago"
120
+ else
121
+ "in about a week"
122
+ end
123
+ elsif timeInSecs.abs < @one_week_in_seconds * 3
124
+ if timeInSecs > 0
125
+ "a couple of weeks ago"
126
+ else
127
+ "in a couple of weeks"
128
+ end
129
+ elsif timeInSecs.abs < @one_month_in_seconds * 2
130
+ if timeInSecs > 0
131
+ "weeks ago"
132
+ else
133
+ "in a few weeks"
134
+ end
135
+ elsif timeInSecs.abs < @one_year_in_seconds
136
+ if timeInSecs > 0
137
+ "months ago"
138
+ else
139
+ "in a few months"
140
+ end
141
+ elsif timeInSecs.abs < @one_year_in_seconds * 2
142
+ if timeInSecs > 0
143
+ "about a year ago"
144
+ else
145
+ "in about a year"
146
+ end
147
+ elsif timeInSecs.abs < @one_year_in_seconds * 3
148
+ if timeInSecs > 0
149
+ "a couple of years ago"
150
+ else
151
+ "in a couple of years"
152
+ end
153
+ elsif timeInSecs > 0
154
+ "years ago"
155
+ else
156
+ "years from now"
157
+ end
158
+ end
159
+ end
160
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Friendly_Time
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: friendly_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bellabling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - barry.drinkwater@gmail.com
16
+ executables:
17
+ - friendly_time
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/friendly_time
22
+ - lib/friendly_time.rb
23
+ - lib/version.rb
24
+ homepage: https://github.com/bellabling/friendly-time-ruby
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.6.8
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Provides casual time strings, such as "about a minute ago"
48
+ test_files: []