英文:
How to decode Flutter iso8601 DateTime to Go Api RFC3339 pgtype.Date and pgtype.Timestamptz
问题
在我的Go API中,我试图使用json解码来解析以下json数据。
{"contract_id":0,"date_established":"2022-04-03T00:00:00.000","expiry_date":null,"extension_expiry_date":null,"description":"fffff"}
我遇到了一个错误:
parsing time "\"2022-04-03T00:00:00.000\"" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "\""" as "Z07:00"
我该如何修复这个错误?
这是我的结构体:
// Contract model
type Contract struct {
ContractId *int `json:"contract_id"`
CompanyId *int `json:"company_id"`
DateEstablished *time.Time `json:"date_established"`
ExpiryDate *time.Time `json:"expiry_date"`
ExtensionExpiryDate *time.Time `json:"extension_expiry_date"`
Description *string `json:"description"`
}
这是我的代码:
func (rs *appResource) contractCreate(w http.ResponseWriter, r *http.Request) {
var contract Contract
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&contract)
英文:
In my Go API I'm trying to use json decode to parse the following json.
{"contract_id":0,"date_established":"2022-04-03T00:00:00.000","expiry_date":null,"extension_expiry_date":null,"description":"fffff"}
I get an error:
parsing time "\"2022-04-03T00:00:00.000\"" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "\"" as "Z07:00"
How can I fix this error?
This is my struct:
// Contract model
type Contract struct {
ContractId *int `json:"contract_id"`
CompanyId *int `json:"company_id"`
DateEstablished *time.Time `json:"date_established"`
ExpiryDate *time.Time `json:"expiry_date"`
ExtensionExpiryDate *time.Time `json:"extension_expiry_date"`
Description *string `json:"description"`
}
Here is my code:
func (rs *appResource) contractCreate(w http.ResponseWriter, r *http.Request) {
var contract Contract
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&contract)
答案1
得分: 3
Go使用RFC 3339来编码时间,如果你控制生成的JSON,只需要将2022-04-03T00:00:00.000
改为2022-04-03T00:00:00.000Z
。
例如,这样可以工作。
type Contract struct {
ContractId *int `json:"contract_id"`
CompanyId *int `json:"company_id"`
DateEstablished *time.Time `json:"date_established"`
ExpiryDate *time.Time `json:"expiry_date"`
ExtensionExpiryDate *time.Time `json:"extension_expiry_date"`
Description *string `json:"description"`
}
func main() {
body := `{"contract_id":0,"date_established":"2022-04-03T00:00:00.000Z","expiry_date":null,"extension_expiry_date":null,"description":"fffff"}`
var contract Contract
reader := strings.NewReader(body)
decoder := json.NewDecoder(reader)
err := decoder.Decode(&contract)
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Printf("Contract: %+v\n", contract)
}
}
如果你无法控制JSON,你需要编写一个自定义的解组方法。
英文:
Go uses RFC 3339 for encoding time, if you control the json being produced you just need to change 2022-04-03T00:00:00.000
to 2022-04-03T00:00:00.000Z
.
For instance this works.
type Contract struct {
ContractId *int `json:"contract_id"`
CompanyId *int `json:"company_id"`
DateEstablished *time.Time `json:"date_established"`
ExpiryDate *time.Time `json:"expiry_date"`
ExtensionExpiryDate *time.Time `json:"extension_expiry_date"`
Description *string `json:"description"`
}
func main() {
body := `{"contract_id":0,"date_established":"2022-04-03T00:00:00.000Z","expiry_date":null,"extension_expiry_date":null,"description":"fffff"}`
var contract Contract
reader := strings.NewReader(body)
decoder := json.NewDecoder(reader)
err := decoder.Decode(&contract)
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Printf("Contract: %+v\n", contract)
}
}
If you don't control the json, you need to write a custom unmarshal method.
答案2
得分: 0
这是我最终在Flutter中使用pgtype.Date
和pgtype.DateTimestamptz
在我的Go API和PostgreSQL中的列类型DATE
和DATETIME
实现的解决方案。我无法在这种数据类型中使用指针。
首先,我在Flutter中创建了一个辅助函数,如下所示:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
extension DateTimeExtension on DateTime {
String format([String pattern = 'dd/MM/yyyy', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatDbDateTime(
[String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatDbDate([String pattern = 'yyyy-MM-dd', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatLocalDate([String pattern = 'dd MMMM yyyy', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatLocalDateTime(
[String pattern = 'dd MMMM yyyy h:mm a', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
}
我在Flutter模型中使用它来格式化日期,用于我的Go API:
if (planAcceptedDate != null) {
data['plan_accepted_date'] = planAcceptedDate!.formatDbDateTime();
} else {
data['plan_accepted_date'] = null;
}
if (invoiceDate != null) {
data['invoice_date'] = invoiceDate!.formatDbDate();
} else {
data['invoice_date'] = null;
}
在我的Flutter视图表单中,我像这样使用它:
Container(
padding: EdgeInsets.all(8.0),
child: Text(
_matter.planAcceptedDate == null
? ''
: _matter.planAcceptedDate!.formatLocalDateTime(),
style: TextStyle(
color: appTextColor,
fontWeight: FontWeight.normal,
fontSize: _user.fontsize,
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: Text(
_matter.invoiceDate == null
? ''
: _matter.invoiceDate!.formatLocalDate(),
style: TextStyle(
color: appTextColor,
fontWeight: FontWeight.normal,
fontSize: _user.fontsize,
),
),
),
我的Go结构体如下所示:
PlanAcceptedDate pgtype.Timestamptz `json:"plan_accepted_date"`
InvoiceDate pgtype.Date `json:"invoice_date"`
英文:
This is the solution I finally implemented in Flutter using pgtype.Date
and pgtype.DateTimestamptz
in my Go api and column types DATE
and DATETIME
in Postgresql. I was unable to use a pointer with this data type.
First I created a helper in Flutter like this:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
extension DateTimeExtension on DateTime {
String format([String pattern = 'dd/MM/yyyy', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatDbDateTime(
[String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatDbDate([String pattern = 'yyyy-MM-dd', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatLocalDate([String pattern = 'dd MMMM yyyy', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
String formatLocalDateTime(
[String pattern = 'dd MMMM yyyy h:mm a', String? locale]) {
if (locale != null && locale.isNotEmpty) {
initializeDateFormatting(locale);
}
return DateFormat(pattern, locale).format(this);
}
}
I used this in my Flutter models to format the date for my Go Api:
if (planAcceptedDate != null) {
data['plan_accepted_date'] = planAcceptedDate!.formatDbDateTime();
} else {
data['plan_accepted_date'] = null;
}
if (invoiceDate != null) {
data['invoice_date'] = invoiceDate!.formatDbDate();
} else {
data['invoice_date'] = null;
}
and in my Flutter view form I use it like this:
Container(
padding: EdgeInsets.all(8.0),
child: Text(
_matter.planAcceptedDate == null
? ''
: _matter.planAcceptedDate!.formatLocalDateTime(),
style: TextStyle(
color: appTextColor,
fontWeight: FontWeight.normal,
fontSize: _user.fontsize,
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: Text(
_matter.invoiceDate == null
? ''
: _matter.invoiceDate!.formatLocalDate(),
style: TextStyle(
color: appTextColor,
fontWeight: FontWeight.normal,
fontSize: _user.fontsize,
),
),
),
My Go struct looks like this:
PlanAcceptedDate pgtype.Timestamptz `json:"plan_accepted_date"`
InvoiceDate pgtype.Date `json:"invoice_date"`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论