Skip to content

Commit

Permalink
feat(datetime converter): back and forth datetime converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Simonbelete committed Jun 17, 2020
1 parent 29e60c3 commit 53bda66
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 1 deletion.
145 changes: 145 additions & 0 deletions src/datetime.js
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
13 changes: 12 additions & 1 deletion src/index.js
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;
29 changes: 29 additions & 0 deletions src/language.js
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
19 changes: 19 additions & 0 deletions src/local.js
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

0 comments on commit 53bda66

Please sign in to comment.