fr.quatrevieux.araknemu.util.DofusDate.DofusDate()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
c 0
b 0
f 0
cc 1
crap 1
rs 10
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.util;
21
22
import java.util.Calendar;
23
24
/**
25
 * Handle date for Dofus server
26
 */
27
public final class DofusDate {
28
    public static final int YEAR_OFFSET = -1370;
29
30
    private final Calendar calendar;
31
32 1
    public DofusDate() {
33 1
        calendar = Calendar.getInstance();
34 1
        calendar.add(Calendar.YEAR, YEAR_OFFSET);
35 1
    }
36
37
    /**
38
     * Get the date to milliseconds
39
     */
40
    public long toMilliseconds() {
41 1
        return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET);
42
    }
43
44
    public int year() {
45
        return calendar.get(Calendar.YEAR);
46
    }
47
48
    public int month() {
49
        return calendar.get(Calendar.MONTH) + 1;
50
    }
51
52
    public int day() {
53
        return calendar.get(Calendar.DAY_OF_MONTH);
54
    }
55
56
    /**
57
     * Create a new past date from duration in milliseconds
58
     *
59
     * @param duration The duration in milliseconds
60
     */
61
    public static DofusDate fromDuration(long duration) {
62 1
        final DofusDate date = new DofusDate();
63
64 1
        date.calendar.setTimeInMillis(date.calendar.getTimeInMillis() - duration);
65
66 1
        return date;
67
    }
68
}
69