-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datetime converter): back and forth datetime converter
- Loading branch information
1 parent
29e60c3
commit 53bda66
Showing
4 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
var Local = require('./local'); | ||
var Language = require('./language'); | ||
var JDNConverter = require('./JDN_converter'); | ||
var TimeConverter = require('./time_converter'); | ||
|
||
/** | ||
* Create an new DateTime object for the given Date object | ||
* | ||
* @param {Date} date | ||
* @public | ||
*/ | ||
|
||
function DateTime (date, timezone = 'Africa/Addis_Ababa') { | ||
|
||
if( !(date instanceof Date) ) | ||
throw 'Datetime must be instance of Date'; | ||
|
||
this.date = date; | ||
|
||
if( timezone != null ) | ||
this.timezone( timezone ); | ||
|
||
} | ||
|
||
/** | ||
* Convert time UTC/GMT timezone to Ethiopic time zone | ||
* i.e UTC + 3 Hours | ||
* GMT + 3 Hours | ||
* | ||
* @param {string} timezone | ||
* @return null | ||
*/ | ||
|
||
DateTime.prototype.timezone = function ( timezone ) { | ||
|
||
date_str = this.date.toLocaleString("en-US", {timeZone: timezone}); | ||
|
||
this.date = new Date( date_str ); | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @return dictionary | ||
*/ | ||
|
||
DateTime.prototype.gregorianToEthiopic = function () { | ||
|
||
year = this.date.getFullYear(); | ||
|
||
month = this.date.getMonth(); | ||
|
||
day = this.date.getDate(); | ||
|
||
hours = this.date.getHours(); | ||
|
||
jdn = JDNConverter.gregorianToJDN ( year, month, day ); | ||
|
||
era = JDNConverter.getEraFromJDN ( jdn ); | ||
|
||
date = JDNConverter.jdnToEthiopic ( jdn, era ); | ||
|
||
hours = TimeConverter.utcToEthiopic( TimeConverter.to12hrFormat(hours) ); | ||
|
||
var datetime = { | ||
'year' : date['year'], | ||
'month' : date['month'], | ||
'day' : date['day'], | ||
'hour': hours, | ||
'minute': this.date.getMinutes(), | ||
'second' : this.date.getSeconds() | ||
} | ||
|
||
return datetime; | ||
} | ||
|
||
/** | ||
* | ||
* @return dictionary | ||
*/ | ||
DateTime.prototype.ethiopicToGregorian = function () { | ||
|
||
year = this.date.getFullYear(); | ||
|
||
month = this.date.getMonth(); | ||
|
||
day = this.date.getDate(); | ||
|
||
hours = this.date.getHours(); | ||
|
||
jdn = JDNConverter.ethCopticToJDN ( year, month, day ); | ||
|
||
date = JDNConverter.jdnToGregorian ( jdn ); | ||
|
||
hours = TimeConverter.ethiopicToUtc( TimeConverter.to12hrFormat(hours) ); | ||
|
||
var datetime = { | ||
'year' : date['year'], | ||
'month' : date['month'], | ||
'day' : date['day'], | ||
'hour': hours, | ||
'minute': this.date.getMinutes(), | ||
'second' : this.date.getSeconds() | ||
} | ||
|
||
return datetime; | ||
} | ||
|
||
/** | ||
* Converter | ||
* | ||
* @param {Local} local | ||
* @param {Language} language | ||
* @return dictionay | ||
*/ | ||
|
||
DateTime.prototype.convert = function ( local = Local.ETHIOPIC, lanugage = Language.AMHARIC ) { | ||
|
||
if( Object.values(Local).indexOf(local) == -1 ) | ||
throw 'Local must be instance of Alfa Gee`z\'s Local'; | ||
|
||
if( Object.values(Local).indexOf(local) == -1 ) | ||
throw 'Language must be instance of Alfa Gee`z\'s Language'; | ||
|
||
this.local = local; | ||
this.lanugage = language; | ||
|
||
if( local == Local.ETHIOPIC ) | ||
new_datetime = this.gregorianToEthiopic(); | ||
else if( local == Local.GREGORIAN ) | ||
new_datetime = this.ethiopicToGregorian(); | ||
|
||
this.new_datetime = new_datetime | ||
|
||
return new_datetime; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
|
||
module.exports = DateTime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
console.log("hello World"); | ||
var alfaGeezDatetime = require('./src/datetime'); | ||
|
||
var alfaGeezLocal = require('./src/local'); | ||
|
||
var alfaGeezLang = require('./src/language'); | ||
|
||
|
||
module.exports.DateTime = alfaGeezDatetime; | ||
|
||
module.exports.Local = alfaGeezLocal; | ||
|
||
module.exports.Language = alfaGeezLang; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Language Lists | ||
* @var {array} lang | ||
* @public | ||
*/ | ||
|
||
const lang = { | ||
AMHARIC : { | ||
'id' : 1, | ||
|
||
'name' : 'amharic', | ||
|
||
'months' : [ | ||
'መስከረም', 'ጥቅምት', 'ኅዳር', 'ታኅሣሥ', 'ጥር', 'የካቲት', 'መጋቢት', 'ሚያዝያ', 'ግንቦት', 'ሰኔ', 'ሐምሌ', 'ነሐሴ', 'ጳጉሜን' | ||
], | ||
|
||
'days' : [ | ||
'ሰኞ', 'ማክሰኞ', 'ረቡዕ', 'ሐሙስ', 'ዓርብ', 'ቅዳሜ', 'እሑድ' | ||
], | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
|
||
module.exports = lang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Localization Lists | ||
* @var {int} local | ||
* @public | ||
*/ | ||
|
||
const local = { | ||
ETHIOPIC : 1, | ||
GREGORIAN : 2 | ||
} | ||
|
||
|
||
|
||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
|
||
module.exports = local |