summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java
blob: 32cce65560f7bed6cfbeabb85d4b7794daef5e13 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.uic.barcode.dynamicContent.fdc1;

import java.time.Instant;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.uic.barcode.asn1.datatypes.FieldOrder;
import org.uic.barcode.asn1.datatypes.IntRange;
import org.uic.barcode.asn1.datatypes.Sequence;

// TODO: Auto-generated Javadoc
/**
 * The Class TimeStamp.
 */
@Sequence
public class TimeStamp {
	

	/*
    -- Moment of generation of the dynamic content, expressed in UTC :
    -- * dynamicContentDay is the number of days from issuing date
    --     (UicRailTicketData.issuingDetail.issuingYear and issuingDay)
	--     The range 0..1070 allows a validity equal to that of the validFrom (700) plus 
	--       validUntil (370) elements of the different transport documents of UicRailTicketData.
    -- * dynamicContentTime is the number of seconds of the day
    --     (from 0 = 0:00:00 to 86399 = 23:59:59)
    -- These two elements shall be either both present, either both absent
    dynamicContentDay   INTEGER (0..366),
     *    
     */
	@FieldOrder(order = 0)
	@IntRange(minValue=1, maxValue=366)
	public Long day;
	
    /** The second of day. */
    // dynamicContentTime  INTEGER (0..86399) OPTIONAL,
	@FieldOrder(order = 1)
	@IntRange(minValue=0, maxValue=86399)
	public Long secondOfDay;
	
	
	
	/**
	 * Instantiates a new time stamp and sets the time-stamp to now.
	 */
	public TimeStamp() {
		Instant now = Instant.now();
		day = new Long(now.get(ChronoField.DAY_OF_YEAR));
		secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY));
	}
	
	/**
	 * Sets the the time-stamp to now.
	 */
	public void setNow() {
		Instant now = Instant.now();
		day = new Long(now.get(ChronoField.DAY_OF_YEAR));
		secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY));		
	}
	
	/**
     * Gets the day.
     *
     * @return the day
     */
    public Long getDay() {
		return day;
	}

	/**
	 * Sets the day.
	 *
	 * @param day the new day
	 */
	public void setDay(Long day) {
		this.day = day;
	}

	/**
	 * Gets the time.
	 *
	 * @return the time
	 */
	public Long getTime() {
		return secondOfDay;
	}

	/**
	 * Sets the time.
	 *
	 * @param time the new time
	 */
	public void setTime(Long time) {
		this.secondOfDay = time;
	}
	
	/**
	 * Gets the time.
	 *
	 * @return the date and time of content creation in UTC
	 */
	public Date getTimeAsDate() {
		
		Calendar cal = Calendar.getInstance();
		int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
		
		if (dayOfYear - day.intValue() > 250) {
			cal.add(Calendar.YEAR, 1);
		}
		if (day.intValue() - dayOfYear > 250) {
			cal.add(Calendar.YEAR, -1);
		}
			
		cal.setTimeZone(TimeZone.getTimeZone("UTC"));
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.HOUR,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.DAY_OF_YEAR, day.intValue());
		cal.add(Calendar.SECOND, secondOfDay.intValue());
		
		return cal.getTime();
	}
	
	/**
	 * Sets the date time.
	 *
	 * @param dateUTC the current date and time in  UTC
	 */
	public void setDateTime(Date dateUTC) {
			
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateUTC);
		
		day = Long.valueOf(cal.get(Calendar.DAY_OF_YEAR));
	
		secondOfDay = (long) cal.get(Calendar.SECOND);
		secondOfDay = secondOfDay + 60 * (long) cal.get(Calendar.MINUTE);
		secondOfDay = secondOfDay + 60 * 60 * (long) cal.get(Calendar.HOUR_OF_DAY);			
		
	}

}