Coder Social home page Coder Social logo

Comments (6)

weifox1990 avatar weifox1990 commented on June 12, 2024

from carbon.

huayi avatar huayi commented on June 12, 2024

complete code:

import (
	"fmt"
	"github.com/golang-module/carbon/v2"
	"testing"
	"time"
	"xorm.io/xorm"
)

type LogAccess struct {
	Id       string        `xorm:"id pk" json:"id"`
	AccessAt carbon.Carbon `json:"access_at"`
	LogoutAt time.Time     `json:"logout_at"`
}

func TestGet(t *testing.T) {
	var err error
	driver := "mssql"
	source := "server=192.168.2.xxx;port=1433;user id=xx;password=xxx;database=xxx"
	db, err = xorm.NewEngine(driver, source)
	if err != nil {
		fmt.Println(err.Error())
	}
	db.ShowSQL(true)
	db.DatabaseTZ = time.Local
	db.TZLocation = time.Local
	acc := &LogAccess{}
	db.ID("1").Get(acc)
	fmt.Println("carbon time:", acc.AccessAt)
	fmt.Println("carbon loc: ", acc.AccessAt.Location())
	fmt.Println("time.Time:", acc.LogoutAt)
}

print:

== RUN   TestGet
[xorm] [info]  2023/12/30 09:01:31.904778 [SQL] SELECT [id], [access_at], [logout_at] FROM [log_access] WHERE [id]=? ORDER BY [id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY [1] - 151.112768ms
carbon time: 2023-12-29 23:56:46
carbon loc:  UTC
time.Time: 2023-12-29 23:56:46 +0800 CST
--- PASS: TestGet (5.62s)

from carbon.

weifox1990 avatar weifox1990 commented on June 12, 2024
c := acc.AccessAt.SetTimezome("PRC")
fmt.Println("carbon time:", c)
fmt.Println("carbon loc: ", c.Location())

from carbon.

Issues-translate-bot avatar Issues-translate-bot commented on June 12, 2024

The issue body's language is not English, translate it automatically, please use English next time. ๐Ÿ‘ฏ๐Ÿ‘ญ๐Ÿป๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘๐Ÿ‘ซ๐Ÿง‘๐Ÿฟโ€๐Ÿคโ€๐Ÿง‘๐Ÿป๐Ÿ‘ฉ๐Ÿพโ€๐Ÿคโ€๐Ÿ‘จ๐Ÿฟ๐Ÿ‘ฌ๐Ÿฟ


fmt.Println("carbon loc: ", acc.AccessAt.SetTimezome("PRC").Location())

from carbon.

huayi avatar huayi commented on June 12, 2024

Thank you. This method is indeed effective.
In fact, I want it to default to using the local time zone.
I think it would be better if it were set to local time by default in database function func (c *Carbon) Scan(v interface{}).
Thank you again.

from carbon.

gouguoyin avatar gouguoyin commented on June 12, 2024

Feat in v2.3.2 and v2.3.2 Released

from carbon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.