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

Golang中Oracle时间字符串转time.Time的问题求助

Converting Oracle Time Strings to time.Time in Go

Hey there! I get that figuring out time parsing in Go can feel tricky when you're just starting out, especially with database-specific formats like Oracle's. Let's break this down step by step to get your time string converted properly.

First, the key thing to remember with Go's time.Parse function is that it uses a reference time layout (Mon Jan 2 15:04:05 MST 2006) to map your input string to a time.Time value. You need to create a layout string that exactly matches the structure of the time string you're getting from Oracle—including separators, capitalization (though month/day abbreviations are case-insensitive), and fractional seconds if present.

Common Oracle Timestamp Format Example

Let's say your Oracle time string looks like this (a super common format for Oracle timestamps):

"15-SEP-24 09.30.45.123456789 AM"

Here's how you'd parse it correctly:

package main

import (
	"fmt"
	"time"
)

func main() {
	// Sample Oracle time string from your database
	oracleTimeStr := "15-SEP-24 09.30.45.123456789 AM"
	// Layout string that mirrors Oracle's time structure exactly
	layout := "02-Jan-03 03.04.05.999999999 PM"

	// Parse the string into a time.Time value
	parsedTime, err := time.Parse(layout, oracleTimeStr)
	if err != nil {
		fmt.Printf("Oops, parsing failed: %v\n", err)
		return
	}

	// Verify and use the parsed time
	fmt.Println("Parsed time:", parsedTime)
	fmt.Println("Formatted to standard RFC3339:", parsedTime.Format(time.RFC3339))
}

Key Tips for Your Use Case:

  • Match Separators Exactly: Oracle uses . to separate hours/minutes/seconds instead of :—make sure your layout uses . too, otherwise parsing will fail.
  • Fractional Seconds: If your Oracle string includes nanoseconds (like .123456789), use .999999999 in your layout to capture all those digits.
  • Two-Digit Years: The 03 in the layout handles two-digit years. Go automatically maps values 00-69 to 2000-2069, and 70-99 to 1970-1999. If you need explicit century control, you can modify the input string to use four-digit years first, or add custom logic with time.ParseInLocation.
  • Time Zones: If your Oracle string includes a time zone (e.g., EST), add MST to your layout and consider using time.ParseInLocation to avoid ambiguity between local time and UTC.

If your specific Oracle time format is different from the example above, just share the exact string you're working with, and we can tweak the layout to fit perfectly!

内容的提问来源于stack exchange,提问作者Sumit Agarwal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:25:31