英文:
How can i add two date forms with time.Parse?
问题
我正在尝试添加两种不同的日期格式以增加测试的灵活性,但是我遇到了以下错误:
当我尝试解析日期时收到的错误消息如下所示:
2021/10/21 12:15:35 parsing time "1982/01/08" as "2006-01-02": cannot parse "/01/08" as "-"
BD.go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// Users 结构体包含一个用户数组
type Users struct {
Users []User `json:"users"`
}
// User 结构体包含姓名、类型和社交链接列表
type User struct {
Firstname string `json:"fname"`
Secondname string `json:"lname"`
Date string `json:"date"`
}
func Birthday() {
// 打开json文件
jsonFile, err := os.Open("users.json")
// 如果os.Open返回错误,则处理错误
if err != nil {
fmt.Println(err)
}
fmt.Println("成功打开users.json")
// 延迟关闭json文件,以便稍后解析
defer jsonFile.Close()
// 将打开的json文件读取为字节数组
byteValue, _ := ioutil.ReadAll(jsonFile)
// 初始化Users数组
var users Users
// 将包含json文件内容的字节数组解组为上面定义的'users'
json.Unmarshal(byteValue, &users)
// 遍历users数组中的每个用户,并打印用户类型、姓名和Facebook链接作为示例
// 编写一个函数列出今天过生日的人
// var yr int
for i := 0; i < len(users.Users); i++ {
date, err := time.Parse("2006/01/02", users.Users[i].Date)
date1, _ := time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
fmt.Println(date)
} else if err != nil {
fmt.Println(date1)
} else {
log.Fatal(err)
}
// 检查日期是否是闰年,例如:29不是闰年,但28是!
if date.Day()%400 == 0 || (date.Day()%4 == 0 && date.Day()%100 != 0) {
fmt.Println("用户名:", users.Users[i].Firstname)
fmt.Println("用户姓氏:", users.Users[i].Secondname)
fmt.Println("用户日期:", users.Users[i].Date)
fmt.Println(users.Users[i].Date, "是闰年 ✨✨✨")
} else if date1.Day()%400 == 0 || (date1.Day()%4 == 0 && date1.Day()%100 != 0) {
fmt.Println("用户名:", users.Users[i].Firstname)
fmt.Println("用户姓氏:", users.Users[i].Secondname)
fmt.Println("用户日期:", users.Users[i].Date)
fmt.Println(users.Users[i].Date, "是闰年 ✨✨✨")
} else {
fmt.Println("用户名:", users.Users[i].Firstname)
fmt.Println("用户姓氏:", users.Users[i].Secondname)
fmt.Println("用户日期:", users.Users[i].Date)
fmt.Println(users.Users[i].Date, "不是闰年 💥💥💥")
}
}
}
func main() {
Birthday()
}
.JSON 文件
{
"users": [
{
"Fname": "Johnny",
"Lname":"mane",
"date":"1982/01/08"
},
{
"Fname": "Wayne",
"Lname":"Bruce",
"date":"1965/01/30"
},
{
"Fname": "Gaga",
"Lname":"Lady",
"date":"1986/03/28"
},
{
"Fname": "radio",
"Lname":"head",
"date":"1988/02/29"
},
{
"Fname": "Mario",
"Lname":"torres",
"date":"1996/09/28"
},
{
"Fname": "robert",
"Lname":"Alex",
"date":"1991/12/01"
},
{
"Fname": "Julia",
"Lname":"sevak",
"date":"1991-03-07"
},
{
"Fname": "feb",
"Lname":"robert",
"date":"1995-01-31"
}
]
}
英文:
I'm trying to add two different date forms for more flexibility during testing, but I got this error below:
The error I received when I tried to parse dates --> 2021/10/21 12:15:35 parsing time "1982/01/08" as "2006-01-02": cannot parse "/01/08" as "-"
BD.go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// Users struct which contains
// an array of users
type Users struct {
Users []User `json:"users"`
}
// User struct which contains a name
// a type and a list of social links
type User struct {
Firstname string `json:"fname"`
Secondname string `json:"lname"`
Date string `json:"date"`
}
func Birthday() {
// Open our jsonFile
jsonFile, err := os.Open("users.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
var users Users
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &users)
// we iterate through every user within our users array and
// print out the user Type, their name, and their facebook url
// as just an example
// write a function to list out the people whose birthday is today.
// var yr int
for i := 0; i < len(users.Users); i++ {
date, err := time.Parse("2006/01/02", users.Users[i].Date)
date1, _ := time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
fmt.Println(date)
} else if err != nil {
fmt.Println(date1)
} else {
log.Fatal(err)
}
// check if the date is a leap year, ex: 29 is not a leap year but 28th is !
if date.Day()%400 == 0 || (date.Day()%4 == 0 && date.Day()%100 != 0) {
fmt.Println("User First Name: " + users.Users[i].Firstname)
fmt.Println("User Second Name: " + users.Users[i].Secondname)
fmt.Println("User Date: " + users.Users[i].Date)
fmt.Println(users.Users[i].Date, " is a Leap Year ✨✨✨ ")
} else if date1.Day()%400 == 0 || (date1.Day()%4 == 0 && date1.Day()%100 != 0) {
fmt.Println("User First Name: " + users.Users[i].Firstname)
fmt.Println("User Second Name: " + users.Users[i].Secondname)
fmt.Println("User Date: " + users.Users[i].Date)
fmt.Println(users.Users[i].Date, " is a Leap Year ✨✨✨ ")
} else {
fmt.Println("User First Name: " + users.Users[i].Firstname)
fmt.Println("User Second Name: " + users.Users[i].Secondname)
fmt.Println("User Date: " + users.Users[i].Date)
fmt.Println(users.Users[i].Date, " is Not a Leap Year 💥💥💥 ")
}
}
}
func main() {
Birthday()
}
.JSON file
{
"users": [
{
"Fname": "Johnny",
"Lname":"mane",
"date":"1982/01/08"
},
{
"Fname": "Wayne",
"Lname":"Bruce",
"date":"1965/01/30"
},
{
"Fname": "Gaga",
"Lname":"Lady",
"date":"1986/03/28"
},
{
"Fname": "radio",
"Lname":"head",
"date":"1988/02/29"
},
{
"Fname": "Mario",
"Lname":"torres",
"date":"1996/09/28"
},
{
"Fname": "robert",
"Lname":"Alex",
"date":"1991/12/01"
},
{
"Fname": "Julia",
"Lname":"sevak",
"date":"1991-03-07" -->
},
{
"Fname": "feb",
"Lname":"robert",
"date":"1995-01-31". --->
}
]
}
答案1
得分: 3
你的错误处理似乎有问题。你需要在第一次尝试解析日期时检查错误。如果失败了,尝试使用不同的格式解析:
date, err := time.Parse("2006/01/02", users.Users[i].Date)
if err != nil {
// 第一次尝试失败,所以再试一次...
date, err = time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
// 两次尝试都失败了
}
}
// 一切正常
英文:
Your error handling seems wrong. You need to check for the error on the first attempt parsing the date. If it failed, try parsing with a different layout:
date, err := time.Parse("2006/01/02", users.Users[i].Date)
if err != nil {
// first attempt failed, so try again...
date, err = time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
// both attempts failed
}
}
// all ok
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论