|
| 1 | +const Applet = imports.ui.applet; |
| 2 | +// const Mainloop = imports.mainloop; |
| 3 | +const GLib = imports.gi.GLib; |
| 4 | +const Settings = imports.ui.settings; |
| 5 | + |
| 6 | +const APPLET_UUID = "nepali-date@bishalacharya"; |
| 7 | + |
| 8 | +const APPLET_DIR = imports.ui.appletManager.appletMeta[APPLET_UUID].path; |
| 9 | + |
| 10 | + |
| 11 | +const SETTINGS = { |
| 12 | + 'language': 'en', |
| 13 | + 'format': 'Month Day, Year', |
| 14 | +}; |
| 15 | + |
| 16 | +// Get current Nepal Time |
| 17 | +function getNepaliNow() { |
| 18 | + let tz = GLib.TimeZone.new("Asia/Kathmandu"); |
| 19 | + return GLib.DateTime.new_now(tz); |
| 20 | +} |
| 21 | +// Month names |
| 22 | +const monthsEnglish = ['Baisakh','Jestha','Ashar','Shrawan','Bhadra','Ashoj','Kartik','Mangsir','Poush','Magh','Falgun','Chaitra']; |
| 23 | +const monthsNepali = ['बैशाख','जेठ','असार','श्रावण','भदौ','असोज','कार्तिक','मंसिर','पुष','माघ','फाल्गुन','चैत्र']; |
| 24 | + |
| 25 | +// Helper: convert English digits to Nepali |
| 26 | +function toNepaliDigits(str) { |
| 27 | + const map = "०१२३४५६७८९"; |
| 28 | + return str.replace(/\d/g, d => map[d]); |
| 29 | +} |
| 30 | + |
| 31 | +// Nepali BS calendar data |
| 32 | +const nepcal = { |
| 33 | + 74: { |
| 34 | + mon_days: [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
| 35 | + tot_days: 365, |
| 36 | + }, |
| 37 | + 75: { |
| 38 | + mon_days: [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
| 39 | + tot_days: 365, |
| 40 | + }, |
| 41 | + 76: { |
| 42 | + mon_days: [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
| 43 | + tot_days: 365, |
| 44 | + }, |
| 45 | + 77: { |
| 46 | + mon_days: [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
| 47 | + tot_days: 366, |
| 48 | + }, |
| 49 | + 78: { |
| 50 | + mon_days: [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
| 51 | + tot_days: 365, |
| 52 | + }, |
| 53 | + 79: { |
| 54 | + mon_days: [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
| 55 | + tot_days: 365, |
| 56 | + }, |
| 57 | + 80: { |
| 58 | + mon_days: [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
| 59 | + tot_days: 365, |
| 60 | + }, |
| 61 | + 81: { |
| 62 | + mon_days: [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
| 63 | + tot_days: 366, |
| 64 | + }, |
| 65 | + 82: { |
| 66 | + mon_days: [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
| 67 | + tot_days: 365, |
| 68 | + }, |
| 69 | + 83: { |
| 70 | + mon_days: [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], |
| 71 | + tot_days: 365, |
| 72 | + }, |
| 73 | + 84: { |
| 74 | + mon_days: [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], |
| 75 | + tot_days: 365, |
| 76 | + }, |
| 77 | + 85: { |
| 78 | + mon_days: [31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], |
| 79 | + tot_days: 365, |
| 80 | + }, |
| 81 | + 86: { |
| 82 | + mon_days: [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], |
| 83 | + tot_days: 365, |
| 84 | + }, |
| 85 | + 87: { |
| 86 | + mon_days: [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], |
| 87 | + tot_days: 366, |
| 88 | + }, |
| 89 | + 88: { |
| 90 | + mon_days: [30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], |
| 91 | + tot_days: 365, |
| 92 | + }, |
| 93 | + 89: { |
| 94 | + mon_days: [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], |
| 95 | + tot_days: 365, |
| 96 | + }, |
| 97 | + 90: { |
| 98 | + mon_days: [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], |
| 99 | + tot_days: 365, |
| 100 | + }, |
| 101 | +}; |
| 102 | + |
| 103 | +const months = [ |
| 104 | + 'Baisakh','Jestha','Ashar','Shrawan','Bhadra', |
| 105 | + 'Ashoj','Kartik','Mangsir','Poush','Magh','Falgun','Chaitra' |
| 106 | +]; |
| 107 | + |
| 108 | +// Reference: Nepali BS 2074-01-01 = 2017-04-14 |
| 109 | +const refNep = [2074, 1, 1]; |
| 110 | +const refEng = new Date(2017, 3, 14); |
| 111 | + |
| 112 | +function getNepaliDate() { |
| 113 | + // date from nepali time |
| 114 | + let now = getNepaliNow(); // GLib.DateTime |
| 115 | + |
| 116 | + // Convert GLib.DateTime → JS Date for diff calculations |
| 117 | + let current = new Date(now.get_year(), now.get_month() - 1, now.get_day_of_month()); |
| 118 | + |
| 119 | + let diff = Math.floor((current - refEng) / (1000 * 3600 * 24)); |
| 120 | + let year = refNep[0], month = refNep[1], day = refNep[2]; |
| 121 | + |
| 122 | + let startYear = 74; |
| 123 | + let yDiff = 0, mDiff = 0, dRem = diff; |
| 124 | + let stop = false; |
| 125 | + |
| 126 | + while (!stop) { |
| 127 | + let data = nepcal[startYear]; |
| 128 | + if (!data) break; |
| 129 | + if (dRem > data.tot_days) { |
| 130 | + yDiff++; |
| 131 | + dRem -= data.tot_days; |
| 132 | + startYear++; |
| 133 | + } else { |
| 134 | + for (let i = 0; i < data.mon_days.length; i++) { |
| 135 | + let md = data.mon_days[i]; |
| 136 | + if (dRem >= md) { |
| 137 | + mDiff++; |
| 138 | + dRem -= md; |
| 139 | + } else { |
| 140 | + stop = true; |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + year += yDiff; |
| 148 | + month += mDiff; |
| 149 | + if (month > 12) { |
| 150 | + year += Math.floor((month-1)/12); |
| 151 | + month = ((month-1) % 12) + 1; |
| 152 | + } |
| 153 | + day += dRem; |
| 154 | + |
| 155 | + return { year, month, day }; // return as object for further formatting |
| 156 | + } |
| 157 | + |
| 158 | +class NepaliDateApplet extends Applet.TextApplet { |
| 159 | + constructor(metadata, orientation, panelHeight, instanceId) { |
| 160 | + super(orientation, panelHeight, instanceId); |
| 161 | + this.set_applet_tooltip("Nepali Date"); |
| 162 | + this._initSettings(metadata, instanceId); |
| 163 | + this._update(); |
| 164 | + GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => { |
| 165 | + this._update(); |
| 166 | + return true; |
| 167 | + }); |
| 168 | + } |
| 169 | + _initSettings(metadata, instanceId) { |
| 170 | + this.settings = new Settings.AppletSettings(this, metadata.uuid, instanceId); |
| 171 | + |
| 172 | + for (const setting of Object.keys(SETTINGS)) { |
| 173 | + this.settings.bindProperty( |
| 174 | + Settings.BindingDirection.IN, |
| 175 | + setting, |
| 176 | + setting, |
| 177 | + this._update.bind(this), |
| 178 | + null |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + _update() { |
| 184 | + let bs = getNepaliDate(); |
| 185 | + |
| 186 | + let monthNames = this.language =="en" ? monthsEnglish : monthsNepali; |
| 187 | + let label = ""; |
| 188 | + |
| 189 | + if(this.format=="YYYY/MM/DD"){ |
| 190 | + let month = ("0" + bs.month).slice(-2); |
| 191 | + let day = ("0" + bs.day).slice(-2); |
| 192 | + label = `${bs.year}/${month}/${day}`; |
| 193 | + } |
| 194 | + else if(this.format=="YYYY Month DD") |
| 195 | + label = `${bs.year} ${monthNames[bs.month-1]} ${bs.day}`; |
| 196 | + else if(this.format="Month DD, YYYY") |
| 197 | + label = `${monthNames[bs.month-1]} ${bs.day}, ${bs.year}`; |
| 198 | + |
| 199 | + if(this.language=="ne") |
| 200 | + label = toNepaliDigits(label); |
| 201 | + |
| 202 | + this.set_applet_label(label); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +function main(metadata, orientation, panelHeight, instanceId) { |
| 207 | + return new NepaliDateApplet(metadata,orientation, panelHeight, instanceId); |
| 208 | +} |
0 commit comments