1
zj
2024-06-13 8eea5be3b36875bd4ffe70e6c3a5bb07b1d829bf
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
package com.yami.trading.huobi.data;
 
import cn.hutool.core.collection.CollectionUtil;
import com.yami.trading.common.util.ApplicationUtil;
import com.yami.trading.service.data.ItemTypeTimezoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
 
@Component
public class TimeZoneConverterService {
 
    @Autowired
    private ItemTypeTimezoneService itemTypeTimezoneService;
 
    /**
     * Converts a given timestamp to a specified time zone.
     *
     * @param timestamp The timestamp to be converted.
     * @param timeZone  The target time zone (e.g., "Asia/Tokyo", "Asia/Shanghai").
     * @return The formatted date-time string in the target time zone.
     */
    public String convertTimeZone(long timestamp, String timeZone) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of(timeZone));
        return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(zonedDateTime);
    }
 
    public String convertTimeZone(String timeZone) {
        ZonedDateTime now = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        ZonedDateTime zonedTime = now.withZoneSameInstant(ZoneId.of(timeZone));
        return formatter.format(zonedTime);
    }
 
    /**
     * 币对开盘类型算字符串
     * @param itemCloseType
     * @return
     */
    public String converNowTimeStrByType(String itemCloseType) {
        String timeZomne = getTimeZoneByItemCloseType(itemCloseType);
        return convertTimeZone(timeZomne);
    }
 
    /**
     * 毫秒和币对开盘类型算字符串
     * @param timestamp
     * @param itemCloseType
     * @return
     */
    public String convertTsStrByType(long timestamp, String itemCloseType) {
        String timeZomne = getTimeZoneByItemCloseType(itemCloseType);
        return convertTimeZone(timestamp, timeZomne);
    }
 
    public String getTimeZoneByItemCloseType(String itemCloseType){
        Map<String, String> itemTypeTimezoneMap = itemTypeTimezoneService.getItemTypeTimezoneMap();
        return itemTypeTimezoneMap.getOrDefault(itemCloseType, "Asia/Singapore");
    }
 
    // Example usage
}