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

Converting between types using time.Time

PHPz
Release: 2024-02-08 22:54:08
forward
547 people have browsed it

在使用 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
Copy after login

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"`
    // ...
}
Copy after login

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
}
Copy after login

Then you can simply do this:

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

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template