Home  >  Article  >  Backend Development  >  Converting between types using time.Time

Converting between types using time.Time

PHPz
PHPzforward
2024-02-08 22:54:08479browse

在使用 time.Time 的类型之间进行转换

Converting between time.Time types is a common operation in Go language programming. The time.Time type is the standard library for processing time in the Go language and can represent specific values ​​of date and time. In actual development, we often need to convert the time.Time type to a string or convert a string to the time.Time type. This process may involve time zone processing, time format conversion, etc. In this article, we will introduce how to convert time.Time type in Go language, as well as some common considerations. Whether you are a beginner in the Go language or a developer with some experience, you can benefit from it.

Question content

I'm trying to create a migration script from Jira to GitLab. The Jira API library I'm using reads issue creation time from Jira using the following type:

// Time represents the Time definition of JIRA as a time.Time of go
type Time time.Time

The GitLab API client allows the creation of issues with a creation time using a field of type *time.Time.

type CreateIssueOptions struct {
    CreatedAt                          *time.Time `url:"created_at,omitempty" json:"created_at,omitempty"`
    DueDate                            *ISOTime   `url:"due_date,omitempty" json:"due_date,omitempty"`
    // ...
}

How to convert from Jira time to GitLab time? I've been trying different options but can't understand how it's supposed to work.

Workaround

If you have a Jira structure:

type SomeStruct struct {
   ...
   T Time
}

Then you can simply do this:

tm:=time.Time(someStruct.T)
if !tm.IsZero() {
   createIssue.CreatedAt=&tm
}

The above is the detailed content of Converting between types using time.Time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete