-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgregorian_to_jalali.qml
77 lines (59 loc) · 2.31 KB
/
gregorian_to_jalali.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Language: QML
* Date: 2022/01/7
* Name: gregorian_to_jalali.qml
* Repository: https://github.com/BaseMax/gregorian_to_jalali
*/
import QtQuick
import QtQuick.Window
Window {
id: appRoot
width: 640
height: 480
visible: true
QtObject {
id: objects
property var result : [];
property var array : [];
property int temp;
property int days ;
}
function gregorian_to_jalali(year, month, day) {
objects.array = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
objects.days = 0;
if(year <= 1600) {
year -= 621;
objects.result["year"] = 0;
} else {
year -= 1600;
objects.result["year"] = 979;
}
objects.temp = (year > 2) ? (year + 1) : year;
objects.days = (parseInt((objects.temp + 3) / 4)) + (365 * year) - (parseInt((objects.temp + 99) / 100)) - 80 + objects.array[month - 1] + (parseInt((objects.temp + 399) / 400)) + day;
objects.result["year"] += 33 * (parseInt(objects.days / 12053));
objects.days %= 12053;
objects.result["year"] += 4 * (parseInt(objects.days / 1461));
objects.days %= 1461;
if(objects.days > 365){
objects.result["year"] += parseInt((objects.days - 1) / 365);
objects.days = (objects.days-1) % 365;
}
objects.result["month"] = (objects.days < 186) ? 1 + parseInt(objects.days / 31) : 7 + parseInt((objects.days - 186) / 30);
objects.result["day"] = 1 + ((objects.days < 186) ? (objects.days % 31) : ((objects.days - 186) % 30));
return objects.result;
}
function gregorian_to_jalali_str (year, month, day) {
let result = gregorian_to_jalali(year, month, day);
if(objects.result["month"] < 10)
objects.result["month"] = "0" + objects.result["month"];
if(objects.result["day"] < 10)
objects.result["day"] = "0" + objects.result["day"];
return objects.result["year"] + "/" + objects.result["month"] + "/" + objects.result["day"];
}
Component.onCompleted: {
var date = gregorian_to_jalali_str(2022, 01, 07);
console.log(gregorian_to_jalali_str(2022, 01, 07));
console.log(gregorian_to_jalali_str(2022, 1, 7));
appRoot.title = date;
}
}