timefly 0.0.1
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 +7 -0
- data/lib/timefly.rb +94 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a12788c0f91b1428dd6a96f6a87b912d046470f5
|
4
|
+
data.tar.gz: 6376d09a077d3cb880fb5d7f0fbe6d8d70224291
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 928fb0377cbcc991cd69f032effbd21970f53256e6371bf9b1d95941a0e23443bb504df22a962952d20344e995d7326c8cb28f61d50981c05a7e24579172002c
|
7
|
+
data.tar.gz: 92b1b9d5ddc08c9b5e1663915588100687792b9e016c7e38ab3e29931263069140851c01bc61b114b407964266f8898b9c00b66f82e5a9904a62a6d80b1a8f32
|
data/lib/timefly.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
class Timefly
|
2
|
+
|
3
|
+
attr_accessor :origin_time
|
4
|
+
|
5
|
+
# initialize with either String, Time or Date instance
|
6
|
+
# Arguments:
|
7
|
+
# origin_time: (String/Time/Date)
|
8
|
+
def initialize(origin_time)
|
9
|
+
self.origin_time = origin_time
|
10
|
+
process
|
11
|
+
end
|
12
|
+
|
13
|
+
# returns the age in years from the date of birth
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
# >> dob = Time.new(1987,8,2)
|
17
|
+
# >> Timefly.new(dob).age
|
18
|
+
# => 27
|
19
|
+
# >> Timefly.new('1987.08.02').age # dob can be of format YYYY.MM.DD, YYYY-MM-DD and YYYY/MM/DD
|
20
|
+
# => 27
|
21
|
+
# >> Timefly.new('1987.08.02').age({ format: '%y years, %m months' })
|
22
|
+
# => 27 years, 10 months
|
23
|
+
#
|
24
|
+
# Arguments:
|
25
|
+
# options: (Hash) { format :(String) }
|
26
|
+
# eg, '%y years, %m months'. %y will give years, and %m will give months
|
27
|
+
def age(options = {})
|
28
|
+
if options[:format].nil?
|
29
|
+
years_from_origin_time
|
30
|
+
else
|
31
|
+
options[:format]
|
32
|
+
.gsub(/\%y/, years_from_origin_time.to_s)
|
33
|
+
.gsub(/\%m/, months_diff_from_origin_time_month.to_s)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# This method tries to convert the origin_time to Time
|
40
|
+
def process
|
41
|
+
if origin_time.is_a? String
|
42
|
+
convert_string_origin_time
|
43
|
+
elsif !origin_time.is_a?(Time) && !origin_time.is_a?(Date)
|
44
|
+
fail("#{origin_time.class.name} is not a supported origin_time")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#convert dob to Time if it is in String
|
49
|
+
def convert_string_origin_time
|
50
|
+
#TODO: improve this method
|
51
|
+
separator = '.'
|
52
|
+
if origin_time.include?('/')
|
53
|
+
separator = '/'
|
54
|
+
elsif origin_time.include?('-')
|
55
|
+
separator = '-'
|
56
|
+
end
|
57
|
+
dob_arr = origin_time.split(separator).map{ |d| d.to_i }
|
58
|
+
self.origin_time = Time.new(dob_arr[0], dob_arr[1], dob_arr[2])
|
59
|
+
end
|
60
|
+
|
61
|
+
# This method gets the months difference since the origin_time month
|
62
|
+
def months_diff_from_origin_time_month
|
63
|
+
origin_time_month = origin_time.strftime('%m').to_i
|
64
|
+
now_month = Time.now.strftime('%m').to_i
|
65
|
+
if origin_time_month == now_month
|
66
|
+
0
|
67
|
+
elsif origin_time_month > now_month
|
68
|
+
12-(origin_time_month-now_month).abs
|
69
|
+
else
|
70
|
+
now_month-origin_time_month
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# This method gets the years elapsed since origin_time
|
75
|
+
def years_from_origin_time
|
76
|
+
origin_time_years = origin_time.strftime('%Y').to_i
|
77
|
+
now_years = Time.now.strftime('%Y').to_i
|
78
|
+
origin_time_month = origin_time.strftime('%m').to_i
|
79
|
+
now_month = Time.now.strftime('%m').to_i
|
80
|
+
if origin_time_month == now_month
|
81
|
+
dob_date = origin_time.strftime('%d').to_i
|
82
|
+
now_date = Time.now.strftime('%d').to_i
|
83
|
+
if now_date > dob_date
|
84
|
+
now_years - origin_time_years - 1
|
85
|
+
else
|
86
|
+
now_years - origin_time_years
|
87
|
+
end
|
88
|
+
elsif origin_time_month > now_month
|
89
|
+
now_years - origin_time_years - 1
|
90
|
+
else
|
91
|
+
now_years - origin_time_years
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timefly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gaurav Singha Roy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A simple library which makes it easier to get time related data, eg,
|
28
|
+
age from Date of birth, elapsed time in beautiful format, etc.
|
29
|
+
email: neogauravsvnit@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/timefly.rb
|
35
|
+
homepage: https://github.com/aaalo/timefly
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.4.8
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Timefly
|
59
|
+
test_files: []
|