-
Notifications
You must be signed in to change notification settings - Fork 68
/
EpochMillisToJavaLocalDateTime.java
62 lines (54 loc) · 2.38 KB
/
EpochMillisToJavaLocalDateTime.java
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
/*
* Copyright (c) 2022 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.datamappers.functions.long_localdate;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.function.LongFunction;
/**
* Converts epoch millis to a
* java.time.{@link LocalDateTime} object, using either the system
* default timezone or the timezone provided. If the specified ZoneId is not
* the same as the time base of the epoch millis instant, then conversion
* errors will occur.
*
* Short form ZoneId values like 'CST' can be used, although US Domestic names
* which specify the daylight savings hours are not supported. The full list of
* short Ids at @see <a href="https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/time/ZoneId.html#SHORT_IDS">JavaSE ZoneId Ids</a>
*
* Any timezone specifier may be used which can be read by {@link ZoneId#of(String)}
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class EpochMillisToJavaLocalDateTime implements LongFunction<LocalDateTime> {
ZoneId timezone;
@Example({"EpochMillisToJavaLocalDateTime()","Yields the LocalDateTime for the system default ZoneId"})
public EpochMillisToJavaLocalDateTime() {
this.timezone = ZoneId.systemDefault();
}
@Example({"EpochMillisToJavaLocalDateTime('ECT')","Yields the LocalDateTime for the ZoneId entry for 'Europe/Paris'"})
public EpochMillisToJavaLocalDateTime(String zoneid) {
this.timezone = ZoneId.of(zoneid);
}
@Override
public LocalDateTime apply(long value) {
return Instant.ofEpochMilli(value).atZone(timezone).toLocalDateTime();
}
}