ethiopic_date 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/ethiopic_date.gemspec +19 -0
- data/lib/ethiopic_date/version.rb +3 -0
- data/lib/ethiopic_date.rb +152 -0
- metadata +62 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 G/yohannes Zenebe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# EthiopicDate
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ethiopic_date'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ethiopic_date
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ethiopic_date/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ethiopic_date"
|
8
|
+
gem.version = EthiopicDate::VERSION
|
9
|
+
gem.authors = ["G/yohannes Zenebe"]
|
10
|
+
gem.email = ["gebreyohannes@gemhalo.org"]
|
11
|
+
gem.description = %q{Simple gem to convert between Ethiopian and Gregorian dates}
|
12
|
+
gem.summary = %q{Ethiopian date to Gregorian and Vice versa convertor}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "ethiopic_date/version"
|
3
|
+
module EthiopicDate
|
4
|
+
# A Ruby implementation of Ethiopic Calendar based on the Mathematical algorithm
|
5
|
+
# from http://ethiopic.org/Calendars/
|
6
|
+
|
7
|
+
# Constants used
|
8
|
+
public
|
9
|
+
Nmonths = 12
|
10
|
+
MonthDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
11
|
+
#Ethiopic: Julian date offset
|
12
|
+
JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856 # ዓ/ም
|
13
|
+
|
14
|
+
#Coptic : Julian date offset
|
15
|
+
JD_EPOCH_OFFSET_COPTIC = 1824665
|
16
|
+
|
17
|
+
JD_EPOCH_OFFSET_GREGORIAN = 1721426
|
18
|
+
JD_EPOCH_OFFSET_AMETE_ALEM = -285019 # ዓ/ዓ
|
19
|
+
|
20
|
+
|
21
|
+
#Changes from in_date:EthiopicDate to GregorianDate
|
22
|
+
#
|
23
|
+
#@api public
|
24
|
+
#@param in_date always must be year,month, day in that order
|
25
|
+
#@return GregorianDate is returned
|
26
|
+
#@example fromEthiopicToGregorian(2004,5,21)
|
27
|
+
|
28
|
+
def fromEthiopicToGregorian(year,month,day)
|
29
|
+
#TODO : Handle Exceptions when there is a wrong input
|
30
|
+
if (year <=0)
|
31
|
+
era=JD_EPOCH_OFFSET_AMETE_ALEM
|
32
|
+
else
|
33
|
+
era=JD_EPOCH_OFFSET_AMETE_MIHRET
|
34
|
+
end
|
35
|
+
jdn = jdn_from_ethiopic( year, month, day,era )
|
36
|
+
return gregorian_from_jdn(jdn)
|
37
|
+
end
|
38
|
+
|
39
|
+
#Changes from in_date:GregorianDate to EthiopicDate
|
40
|
+
#
|
41
|
+
#@api public
|
42
|
+
#@param year,month, day in that order
|
43
|
+
#@return EthiopicDate is returned
|
44
|
+
#@example fromEthiopicToGregorian(2012,5,21)
|
45
|
+
def fromGregorianToEthiopic(year,month,day)
|
46
|
+
#TODO : Handle Exceptions when there is a wrong input
|
47
|
+
ethiopic_date = {:year=>-1,:month=>-1,:day => -1 }
|
48
|
+
jdn = jdn_from_gregorian(year,month,day)
|
49
|
+
if jdn >=JD_EPOCH_OFFSET_AMETE_MIHRET + 365
|
50
|
+
era= JD_EPOCH_OFFSET_AMETE_MIHRET
|
51
|
+
else
|
52
|
+
era= JD_EPOCH_OFFSET_AMETE_ALEM
|
53
|
+
end
|
54
|
+
r = (jdn - era).modulo(1461)
|
55
|
+
n = (r.modulo(365) ) + (365 * (r/1460 ))
|
56
|
+
ethiopic_date[:year] =4 * ((jdn - era)/1461) + r/365 - r/1460
|
57
|
+
ethiopic_date[:month] =(n/30) + 1
|
58
|
+
ethiopic_date[:day] =(n.modulo(30)) + 1
|
59
|
+
return [ethiopic_date[:year],ethiopic_date[:month],ethiopic_date[:day]].join("-").to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
#Date format for Ethiopic date
|
64
|
+
#
|
65
|
+
#@api public
|
66
|
+
#@return a formated Ethiopic date string
|
67
|
+
#@example ethiopic_date_format('2004-5-21') will be ጥር 21 ቀን 2004ዓ/ም
|
68
|
+
def ethiopic_date_format(in_date)
|
69
|
+
date_string=in_date.split("-")
|
70
|
+
year=date_string[0].to_i
|
71
|
+
month=date_string[1].to_i
|
72
|
+
day=date_string[2].to_i
|
73
|
+
month_name =""
|
74
|
+
case month
|
75
|
+
when 1 then month_name=" መስከረም "
|
76
|
+
when 2 then month_name=" ጥቅምት "
|
77
|
+
when 3 then month_name=" ህዳር "
|
78
|
+
when 4 then month_name=" ታህሳስ "
|
79
|
+
when 5 then month_name=" ጥር "
|
80
|
+
when 6 then month_name=" የካቲት "
|
81
|
+
when 7 then month_name=" መጋቢት "
|
82
|
+
when 8 then month_name=" ሚያዝያ "
|
83
|
+
when 9 then month_name=" ግንቦት "
|
84
|
+
when 10 then month_name=" ሰኔ "
|
85
|
+
when 11 then month_name=" ሐምሌ "
|
86
|
+
when 12 then month_name=" ነሃሴ "
|
87
|
+
when 13 then month_name=" ጳጉሜን "
|
88
|
+
end
|
89
|
+
date="#{month_name} #{day} ቀን #{year}ዓ/ም"
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
#Calculates the jdn from given Gregorian calendar
|
94
|
+
#
|
95
|
+
#@api private
|
96
|
+
#@return jdn
|
97
|
+
def jdn_from_gregorian(year,month,day)
|
98
|
+
s = ( year/4 ) - ( year - 1)/4 - ( year/100 ) + ( year - 1)/100 + ( year/ 400 ) - ( year - 1)/400
|
99
|
+
t = ( 14 - month)/12
|
100
|
+
n = 31 * t * (month - 1) + (1 - t) * (59 + s + 30 * (month - 3) + ( (3*month - 7)/ 5) ) + day - 1
|
101
|
+
j = JD_EPOCH_OFFSET_GREGORIAN + 365 * (year - 1) + ( year - 1)/4 - ( year - 1)/100 + ( year - 1)/400 + n
|
102
|
+
return j
|
103
|
+
end
|
104
|
+
#Calculates the jdn from given Ethiopic calendar
|
105
|
+
#
|
106
|
+
#@api private
|
107
|
+
#@return jdn
|
108
|
+
def jdn_from_ethiopic(year,month,day,era)
|
109
|
+
jdn = ( era + 365 ) + (365 * ( year - 1 )) + (year/4 ) + (30 * month) + (day - 31)
|
110
|
+
return jdn
|
111
|
+
end
|
112
|
+
|
113
|
+
def gregorian_from_jdn(jdn)
|
114
|
+
date = {:year=>-1,:month=>-1,:day => -1 }
|
115
|
+
r2000 = (jdn - JD_EPOCH_OFFSET_GREGORIAN)%730485
|
116
|
+
r400 = (jdn - JD_EPOCH_OFFSET_GREGORIAN)%146097
|
117
|
+
r100 = r400%36524
|
118
|
+
r4 = r100%1461
|
119
|
+
|
120
|
+
n = (r4%365) + 365*(r4/1460)
|
121
|
+
s = r4/1095
|
122
|
+
aprime = 400 * ((jdn - JD_EPOCH_OFFSET_GREGORIAN)/146097)+ (100 *( r400/36524 ))+ (4 *( r100/1461 ))+ ( r4/365 ) - (r4/1460 ) - (r2000/730484 )
|
123
|
+
date[:year] = aprime + 1
|
124
|
+
t = (364+s-n)/306
|
125
|
+
date[:month] = t * ((n/31) + 1 ) + ( 1 - t ) * (((5*(n-s)+13)/153) + 1 )
|
126
|
+
n += 1 - (r2000/730484)
|
127
|
+
date[:day] = n
|
128
|
+
|
129
|
+
|
130
|
+
if ( (r100 == 0) && (n == 0) && (r400 != 0) )
|
131
|
+
date[:month] = 12
|
132
|
+
date[:day] = 31
|
133
|
+
else
|
134
|
+
MonthDays[2] = isGregorianLeap(date[:year]) ? 29 : 28
|
135
|
+
for i in 1..Nmonths
|
136
|
+
if (n <= MonthDays[i])
|
137
|
+
date[:day] = n
|
138
|
+
break
|
139
|
+
end
|
140
|
+
n -= MonthDays[i]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
gregorian_date=Date.new(date[:year],date[:month],date[:day])
|
144
|
+
|
145
|
+
return gregorian_date
|
146
|
+
end
|
147
|
+
|
148
|
+
def isGregorianLeap(year)
|
149
|
+
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ethiopic_date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- G/yohannes Zenebe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-11-09 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Simple gem to convert between Ethiopian and Gregorian dates
|
17
|
+
email:
|
18
|
+
- gebreyohannes@gemhalo.org
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- LICENSE.txt
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- ethiopic_date.gemspec
|
32
|
+
- lib/ethiopic_date.rb
|
33
|
+
- lib/ethiopic_date/version.rb
|
34
|
+
homepage: ""
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.8
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Ethiopian date to Gregorian and Vice versa convertor
|
61
|
+
test_files: []
|
62
|
+
|