You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Altova Mapforce 2021Rel3:CSV转JSON时中部时间转东部时间方法

CSV转JSON时的中部时间转东部时间方案

核心逻辑是为原始时间明确绑定中部时区,再转换到东部时区,避免手动加减小时(夏令时切换会导致错误)。以下是几种主流技术栈的实现方式:

Python实现(适合数据处理场景)

如果用Python处理CSV转JSON,推荐使用标准库datetime+zoneinfo(Python 3.9+),无需额外安装第三方包:

import csv
import json
from datetime import datetime
from zoneinfo import ZoneInfo

# 读取CSV并处理时间
processed_data = []
with open('input.csv', 'r', encoding='utf-8') as csv_file:
    reader = csv.DictReader(csv_file)
    for row in reader:
        # 解析原始时间字符串,绑定中部时区(America/Chicago是标准时区标识符)
        central_time = datetime.fromisoformat(row['datetime']).replace(tzinfo=ZoneInfo('America/Chicago'))
        # 转换为东部时区(America/New_York)
        eastern_time = central_time.astimezone(ZoneInfo('America/New_York'))
        # 替换为JSON兼容的ISO格式时间字符串
        row['datetime'] = eastern_time.isoformat()
        processed_data.append(row)

# 写入JSON文件
with open('output.json', 'w', encoding='utf-8') as json_file:
    json.dump(processed_data, json_file, indent=2, ensure_ascii=False)

注:时区标识符会自动处理夏令时切换,比手动加减1小时更可靠。

JavaScript(Node.js)实现

在Node.js环境中,推荐使用轻量库luxon处理时区,比原生方式更严谨:

const fs = require('fs');
const csv = require('csv-parser');
const { DateTime } = require('luxon');

const results = [];

fs.createReadStream('input.csv')
  .pipe(csv())
  .on('data', (row) => {
    // 解析为中部时区时间
    const centralTime = DateTime.fromISO(row.datetime, { zone: 'America/Chicago' });
    // 转换为东部时区并输出ISO格式
    const easternTime = centralTime.setZone('America/New_York').toISO();
    row.datetime = easternTime;
    results.push(row);
  })
  .on('end', () => {
    fs.writeFileSync('output.json', JSON.stringify(results, null, 2));
  });

C#实现

在.NET环境中,使用TimeZoneInfo类完成时区转换:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using CsvHelper;

class Program
{
    static void Main()
    {
        // 定义目标时区
        var centralZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
        var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

        // 读取CSV数据
        List<Record> records;
        using (var reader = new StreamReader("input.csv"))
        using (var csv = new CsvReader(reader, System.Globalization.CultureInfo.InvariantCulture))
        {
            records = csv.GetRecords<Record>().ToList();
        }

        // 转换时区
        foreach (var record in records)
        {
            var centralTime = DateTime.ParseExact(record.DateTime, "yyyy-MM-ddTHH:mm:ss", null);
            var easternTime = TimeZoneInfo.ConvertTime(centralTime, centralZone, easternZone);
            record.DateTime = easternTime.ToString("yyyy-MM-ddTHH:mm:ss");
        }

        // 写入JSON
        var json = JsonSerializer.Serialize(records, new JsonSerializerOptions { WriteIndented = true });
        File.WriteAllText("output.json", json);
    }
}

public class Record
{
    public string DateTime { get; set; }
    // 其他CSV字段...
}

内容的提问来源于stack exchange,提问作者Scott Cantrell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 19:07:28