Skip to content

Commit 563de13

Browse files
committed
changes
1 parent 5a8b6da commit 563de13

File tree

5 files changed

+62
-91
lines changed

5 files changed

+62
-91
lines changed

DateUtil.js

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@ const DateUtil = {
88
reformatDate: function(date) {
99
const CDate = require("./CDate")
1010
var year = date.getYear(), month = date.getMonth(), day = date.getDay(), hour = date.getHour(), minute = date.getMinute(), second = date.getSecond(), milli = date.getMilli();
11-
while (milli < 0) {
12-
second -= 1;
13-
milli += 1000;
14-
}
15-
while (second < 0) {
16-
minute -= 1;
17-
second += 60;
18-
19-
}
20-
while (minute < 0) {
21-
hour -= 1;
22-
minute += 60;
23-
}
24-
while (hour < 0) {
25-
day -= 1;
26-
hour += 24;
27-
}
28-
while (month < 1) {
29-
year -= 1;
30-
month += 12;
31-
}
32-
3311
var milli_correct = milli / 1000.0;
3412
milli = milli % 1000;
3513
second += Math.floor(milli_correct);
@@ -49,47 +27,39 @@ const DateUtil = {
4927
var tempM = month;
5028
var tempY = year % 4 + 1;
5129

52-
while (day > DateUtil.CDate.getDaysInMonth(tempM, tempY)) {
53-
day -= DateUtil.CDate.getDaysInMonth(tempM, tempY);
54-
tempM++;
55-
month++;
56-
if (tempM > 12) {
57-
tempY++;
58-
tempM = 1;
59-
}
60-
if (tempY > 4) tempY = 1;
30+
while (day > DateUtil.CDate.getDaysInMonth(tempM, tempY)) {
31+
day -= DateUtil.CDate.getDaysInMonth(tempM, tempY);
32+
tempM++;
33+
month++;
34+
if (tempM > 12) {
35+
tempY++;
36+
tempM = 1;
6137
}
62-
while (day <= 0) {
63-
day += DateUtil.CDate.getDaysInMonth(tempM, tempY);
64-
tempM--;
65-
month--;
66-
if (tempM < 1) {
67-
tempM = 12;
68-
tempY--;
69-
}
70-
if (tempY < 1) tempY = 4;
38+
if (tempY > 4) tempY = 1;
39+
}
40+
while (day <= 0) {
41+
day += DateUtil.CDate.getDaysInMonth(tempM, tempY);
42+
tempM--;
43+
month--;
44+
if (tempM < 1) {
45+
tempM = 12;
46+
tempY--;
7147
}
48+
if (tempY < 1) tempY = 4;
49+
}
7250

73-
var month_correct = (month - 1) / 12.0;
74-
month = (month - 1) % 12 + 1;
75-
year += Math.floor(month_correct);
51+
var month_correct = (month - 1) / 12.0;
52+
month = (month - 1) % 12 + 1;
53+
year += Math.floor(month_correct);
7654

77-
if (date.year > 0 && year <= 0) year--;
78-
else if (date.year < 0 && year >= 0) year++;
79-
80-
if (month < 1) {
81-
year--
82-
}
83-
84-
return new CDate(year, month, day, hour, minute, second, milli);
85-
55+
if (date.year > 0 && year <= 0) year--;
56+
else if (date.year < 0 && year >= 0) year++;
57+
58+
return new CDate(year, month, day, hour, minute, second, milli);
8659
},
8760

8861
isLeapYear: function(year) {
89-
if (year % 4 != 0) return false;
90-
else if (year % 100 != 0) return true;
91-
else if (year % 400 != 0) return false;
92-
else return true;
62+
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
9363
},
9464

9565
getDaysInMonth: function(monthIn, year) {

package-lock.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "agentdid127-datejs",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "agentdid127's Date system for JavaScript",
55
"main": "index.js",
66
"scripts": {

unix/UnixTimestamp.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ class UnixTimestamp {
1313

1414
toDate() {
1515
var milli;
16-
if (this.format == UnixFormat[0]) {
17-
milli = this.data * 1000;
18-
}
19-
else if (this.format == UnixFormat[1]) {
20-
milli = this.data;
21-
}
22-
16+
if (format.equals(UnixFormat[0])) {
17+
milli = data * 1000;
18+
} else if (format.equals(UnixFormat[1])) {
19+
milli = data;
20+
} else return null;
2321
var second = 0;
2422
var milli_correct = milli / 1000.0;
2523
milli = milli % 1000;
@@ -41,7 +39,7 @@ class UnixTimestamp {
4139
var hour_correct = hour / 24.0;
4240
hour = hour % 24;
4341
day += Math.floor(hour_correct);
44-
42+
4543
var month = 0;
4644
var yCycle = 1;
4745
var mCycle = 1;
@@ -66,17 +64,16 @@ class UnixTimestamp {
6664
if (yCycle < 1) yCycle = 4;
6765
}
6866
month++;
69-
70-
var year = 1969;
71-
72-
var month_correct = (month -1) / 12.0;
73-
month = (month -1) % 24 + 1;
74-
year += Math.floor(month_correct);
67+
var year = 1970;
68+
var month_correct = (month - 1) / 12.0;
69+
month = (month - 1) % 12 + 1;
70+
year += Math.floor(month_correct);
7571

7672

77-
if (year % 4 > 0 && year > 1970) day++;
78-
if (year % 4 != 1 && year < 1970) day++;
79-
if (year <= 0) year--;
73+
if (year % 4 > 0 && year > 1970) day++;
74+
if (year % 4 != 1 && year < 1970) day++;
75+
//if (year < 1970) second--;
76+
if (year <= 0) year--;
8077

8178
return UnixUtils.UnixSupportedDate.dateFromString(DateUtil.CDate.reformatDate(new CDate(year, Math.floor(month), Math.floor(day), Math.floor(hour), Math.floor(minute), Math.floor(second), Math.floor(milli))).toString());
8279
}

unix/UnixUtils.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,38 @@ const UnixUtils = {
2525

2626
fromDate: function(dateIn) {
2727
const DateUtil = require("../DateUtil")
28-
const CDate = require("../CDate")
2928
const UnixTimestamp = require("./UnixTimestamp")
3029
const UnixFormat = require("./UnixFormat")
3130
var date = dateIn
31+
//Get some useful information
3232
var yearTemp = date.getYear();
3333
if (yearTemp < 0) yearTemp++;
34-
var leapYearTemp = Math.floor(1970 / 4);
34+
var leapYearTemp = Math.floor(1970 / 4.0);
3535
var normalYearTemp = 1970 - leapYearTemp;
36-
var leapYears = Math.floor(yearTemp / 4);
36+
var leapYears = Math.floor(yearTemp / 4.0);
3737
var normalYears = (yearTemp - leapYears);
38-
39-
var secondN = date.getSecond() * 1000;
38+
39+
//Seconds through hours
40+
var secondN = date.getSecond() * 1000;
4041
var minuteN = date.getMinute() * (1000 * 60);
4142
var hourN = date.getHour() * (1000 * 60 * 60);
42-
43+
44+
//Days
4345
var dayN = date.getDay();
4446
dayN--;
4547
if (DateUtil.CDate.isLeapYear(date.getYear())) dayN--;
4648
dayN *= (1000 * 60 * 60 * 24);
47-
49+
50+
//Months
4851
var monthN = 0;
4952
for (var i = 1; i < date.getMonth(); i++) monthN += DateUtil.CDate.getDaysInMonth(i, date.getYear());
5053
monthN *= (1000 * 60 * 60 * 24);
51-
54+
55+
//Years
5256
leapYears -= leapYearTemp;
5357
normalYears -= normalYearTemp;
5458
normalYears *= (1000 * 60 * 60 * 24) * 365;
5559
leapYears = leapYears * (1000 * 60 * 60 * 24) * 366;
56-
5760
var yearN = leapYears + normalYears;
5861

5962
return new UnixTimestamp(date.getMilli() + secondN + minuteN + hourN + dayN + monthN + yearN, UnixFormat[1]);

0 commit comments

Comments
 (0)