本文整理汇总了Golang中Time.Date函数的典型用法代码示例。如果您正苦于以下问题:Golang Date函数的具体用法?Golang Date怎么用?Golang Date使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Date函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSortByAsOf
func (p *PluginSuite) TestSortByAsOf(c *C) {
results := []RiskServiceCalculationResult{
{
AsOf: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
Score: ptrToInt(1),
},
{
AsOf: time.Date(2004, time.January, 1, 0, 0, 0, 0, time.UTC),
Score: ptrToInt(2),
},
{
AsOf: time.Date(2002, time.January, 1, 0, 0, 0, 0, time.UTC),
Score: ptrToInt(3),
},
{
AsOf: time.Date(2005, time.January, 1, 0, 0, 0, 0, time.UTC),
Score: ptrToInt(4),
},
{
AsOf: time.Date(1999, time.January, 1, 0, 0, 0, 0, time.UTC),
Score: ptrToInt(5),
},
}
sort.Sort(byAsOfDate(results))
for i, score := range []int{5, 1, 3, 2, 4} {
c.Assert(*results[i].Score, Equals, score)
}
}
示例2: genNextClock
func genNextClock() (time.Time, time.Time) {
now := time.Now().Local()
year, month, day := now.Date()
hour, _, _ := now.Clock()
return time.Date(year, month, day, hour, 0, 0, 0, time.Local),
time.Date(year, month, day, hour+1, 0, 0, 0, time.Local)
}
示例3: TestEC2RoleProviderExpiryWindowIsExpired
func TestEC2RoleProviderExpiryWindowIsExpired(t *testing.T) {
server := initTestServer("2014-12-16T01:51:37Z", false)
defer server.Close()
p := &ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(&ec2metadata.Config{Endpoint: aws.String(server.URL + "/latest")}),
ExpiryWindow: time.Hour * 1,
}
p.CurrentTime = func() time.Time {
return time.Date(2014, 12, 15, 0, 51, 37, 0, time.UTC)
}
assert.True(t, p.IsExpired(), "Expect creds to be expired before retrieve.")
_, err := p.Retrieve()
assert.Nil(t, err, "Expect no error")
assert.False(t, p.IsExpired(), "Expect creds to not be expired after retrieve.")
p.CurrentTime = func() time.Time {
return time.Date(2014, 12, 16, 0, 55, 37, 0, time.UTC)
}
assert.True(t, p.IsExpired(), "Expect creds to be expired.")
}
示例4: TestRepositoriesService_ListCommits
func TestRepositoriesService_ListCommits(t *testing.T) {
setup()
defer teardown()
// given
mux.HandleFunc("/repos/o/r/commits", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r,
values{
"sha": "s",
"path": "p",
"author": "a",
"since": "2013-08-01T00:00:00Z",
"until": "2013-09-03T00:00:00Z",
})
fmt.Fprintf(w, `[{"sha": "s"}]`)
})
opt := &CommitsListOptions{
SHA: "s",
Path: "p",
Author: "a",
Since: time.Date(2013, time.August, 1, 0, 0, 0, 0, time.UTC),
Until: time.Date(2013, time.September, 3, 0, 0, 0, 0, time.UTC),
}
commits, _, err := client.Repositories.ListCommits("o", "r", opt)
if err != nil {
t.Errorf("Repositories.ListCommits returned error: %v", err)
}
want := []*RepositoryCommit{{SHA: String("s")}}
if !reflect.DeepEqual(commits, want) {
t.Errorf("Repositories.ListCommits returned %+v, want %+v", commits, want)
}
}
示例5: TestMultipleResultMerge
func TestMultipleResultMerge(t *testing.T) {
t1 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 2, 18, 0, 0, 0, time.UTC)
cd := &CallDescriptor{Direction: utils.OUT, Category: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
cc1, _ := cd.getCost()
if cc1.Cost != 61 {
//ils.LogFull(cc1)
t.Errorf("expected 61 was %v", cc1.Cost)
for _, ts := range cc1.Timespans {
t.Log(ts.RateInterval)
}
}
t1 = time.Date(2012, time.February, 2, 18, 00, 0, 0, time.UTC)
t2 = time.Date(2012, time.February, 2, 18, 01, 0, 0, time.UTC)
cd = &CallDescriptor{Direction: utils.OUT, Category: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
cc2, _ := cd.getCost()
if cc2.Cost != 30 {
t.Errorf("expected 30 was %v", cc2.Cost)
for _, ts := range cc1.Timespans {
t.Log(ts.RateInterval)
}
}
cc1.Merge(cc2)
if len(cc1.Timespans) != 2 || cc1.Timespans[0].GetDuration().Seconds() != 60 {
t.Error("wrong resulted timespans: ", len(cc1.Timespans))
}
if cc1.Cost != 91 {
t.Errorf("Exdpected 91 was %v", cc1.Cost)
}
}
示例6: TestDebit2
func TestDebit2(t *testing.T) {
cd := &engine.CallDescriptor{
Direction: "*out",
Category: "call",
Tenant: "cgrates.org",
Subject: "12345",
Account: "12345",
Destination: "447956933443",
TimeStart: time.Date(2014, 3, 4, 6, 0, 0, 0, time.UTC),
TimeEnd: time.Date(2014, 3, 4, 6, 0, 10, 0, time.UTC),
}
if cc, err := cd.Debit(); err != nil {
t.Error(err)
} else if cc.Cost != 0.01 {
t.Error("Wrong cost returned: ", cc.Cost)
}
acnt, err := acntDb2.GetAccount("cgrates.org:12345")
if err != nil {
t.Error(err)
}
if len(acnt.BalanceMap) != 2 {
t.Error("Wrong number of user balances found", acnt.BalanceMap)
}
if acnt.BalanceMap[utils.VOICE][0].Value != 20 {
t.Error("Account does not have expected minutes in balance", acnt.BalanceMap[utils.VOICE][0].Value)
}
for _, blnc := range acnt.BalanceMap[utils.MONETARY] { // Test negative balance for default one
if blnc.Weight == 10 && blnc.Value != 0 {
t.Errorf("Balance with weight: %f, having value: %f ", blnc.Weight, blnc.Value)
} else if blnc.Weight == 0 && blnc.Value != -0.01 {
t.Errorf("Balance with weight: %f, having value: %f ", blnc.Weight, blnc.Value)
}
}
}
示例7: hasReachDailyTweetLimit
func hasReachDailyTweetLimit() (bool, error) {
var from time.Time
var to time.Time
now := time.Now()
if now.Hour() >= WAKE_UP_HOUR {
from = time.Date(now.Year(), now.Month(), now.Day(), WAKE_UP_HOUR, 0, 0, 0, now.Location())
} else {
yesterday := now.Add(-time.Duration(24) * time.Hour)
from = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), WAKE_UP_HOUR, 0, 0, 0, yesterday.Location())
}
if now.Hour() < GO_TO_BED_HOUR {
to = time.Date(now.Year(), now.Month(), now.Day(), GO_TO_BED_HOUR, 0, 0, 0, now.Location())
} else {
tomorrow := now.Add(time.Duration(24) * time.Hour)
to = time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), GO_TO_BED_HOUR, 0, 0, 0, tomorrow.Location())
}
count, err := db.GetNumberOfTweetsBetweenDates(from, to)
if err != nil {
return true, err
}
return count >= MAX_TWEET_IN_A_DAY, nil
}
示例8: TestData_CreateShardGroup
// Ensure that a shard group can be created on a database for a given timestamp.
func TestData_CreateShardGroup(t *testing.T) {
var data meta.Data
if err := data.CreateNode("node0"); err != nil {
t.Fatal(err)
} else if err = data.CreateNode("node1"); err != nil {
t.Fatal(err)
} else if err = data.CreateDatabase("db0"); err != nil {
t.Fatal(err)
} else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 2, Duration: 1 * time.Hour}); err != nil {
t.Fatal(err)
}
// Create shard group.
if err := data.CreateShardGroup("db0", "rp0", time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)); err != nil {
t.Fatal(err)
}
// Verify the shard group was created.
if sgi, _ := data.ShardGroupByTimestamp("db0", "rp0", time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)); !reflect.DeepEqual(sgi, &meta.ShardGroupInfo{
ID: 1,
StartTime: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
EndTime: time.Date(2000, time.January, 1, 1, 0, 0, 0, time.UTC),
Shards: []meta.ShardInfo{
{ID: 1, OwnerIDs: []uint64{1, 2}},
},
}) {
t.Fatalf("unexpected shard group: %#v", sgi)
} else if !sgi.Shards[0].OwnedBy(1) || !sgi.Shards[0].OwnedBy(2) || sgi.Shards[0].OwnedBy(3) {
// Verify shard is correctly owned-by the node.
t.Fatalf("new shard is not owned by correct node")
}
}
示例9: TestParseTime
func TestParseTime(t *testing.T) {
RegisterTestingT(t)
var res time.Time
var err error
res, err = ParseTime("0")
Ω(err).Should(BeNil())
Ω(res).Should(Equal(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)))
res, err = ParseTime("1445486493")
Ω(err).Should(BeNil())
Ω(res).Should(Equal(time.Date(2015, 10, 22, 4, 1, 33, 0, time.UTC)))
res, err = ParseTime("2524608000")
Ω(err).Should(BeNil())
Ω(res).Should(Equal(time.Date(2050, 1, 1, 0, 0, 0, 0, time.UTC)))
res, err = ParseTime("252460800000")
Ω(err).Should(BeNil())
Ω(res).Should(Equal(time.Date(1978, 1, 1, 0, 0, 0, 0, time.UTC)))
res, err = ParseTime("2016-02-03T15:04:05Z")
Ω(err).Should(BeNil())
Ω(res).Should(Equal(time.Date(2016, 2, 3, 15, 4, 5, 0, time.UTC)))
}
示例10: TestParseTs
// The assertions in this test should also be caught by the integration tests on
// various drivers.
func TestParseTs(t *testing.T) {
defer leaktest.AfterTest(t)()
var parseTsTests = []struct {
strTimestamp string
expected time.Time
}{
// time.RFC3339Nano for github.com/lib/pq.
{"2006-07-08T00:00:00.000000123Z", time.Date(2006, 7, 8, 0, 0, 0, 123, time.FixedZone("UTC", 0))},
// The format accepted by pq.ParseTimestamp.
{"2001-02-03 04:05:06.123-07", time.Date(2001, time.February, 3, 4, 5, 6, 123000000, time.FixedZone("", -7*60*60))},
}
for i, test := range parseTsTests {
parsed, err := parseTs(test.strTimestamp)
if err != nil {
t.Errorf("%d could not parse [%s]: %v", i, test.strTimestamp, err)
continue
}
if !parsed.Equal(test.expected) {
t.Errorf("%d parsing [%s] got [%s] expected [%s]", i, test.strTimestamp, parsed, test.expected)
}
}
}
示例11: cliInit
func cliInit(c *cli.Context) (dates map[string]time.Time) {
var err error
dates = make(map[string]time.Time)
if len(c.String("from")) > 0 {
dates["fromDate"], err = time.Parse("2006-01-02", c.String("from"))
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
} else {
dates["fromDate"] = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.UTC)
}
if len(c.String("to")) > 0 {
dates["toDate"], err = time.Parse(isoDateFmt, c.String("to"))
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
} else {
dates["toDate"] = time.Date(time.Now().Year(), 12, 31, 0, 0, 0, 0, time.UTC)
}
return dates
}
示例12: TestOCSPDecode
func TestOCSPDecode(t *testing.T) {
responseBytes, _ := hex.DecodeString(ocspResponseHex)
resp, err := ParseResponse(responseBytes, nil)
if err != nil {
t.Error(err)
}
expected := Response{
Status: Good,
SerialNumber: big.NewInt(0x1d0fa),
RevocationReason: Unspecified,
ThisUpdate: time.Date(2010, 7, 7, 15, 1, 5, 0, time.UTC),
NextUpdate: time.Date(2010, 7, 7, 18, 35, 17, 0, time.UTC),
}
if !reflect.DeepEqual(resp.ThisUpdate, expected.ThisUpdate) {
t.Errorf("resp.ThisUpdate: got %d, want %d", resp.ThisUpdate, expected.ThisUpdate)
}
if !reflect.DeepEqual(resp.NextUpdate, expected.NextUpdate) {
t.Errorf("resp.NextUpdate: got %d, want %d", resp.NextUpdate, expected.NextUpdate)
}
if resp.Status != expected.Status {
t.Errorf("resp.Status: got %d, want %d", resp.Status, expected.Status)
}
if resp.SerialNumber.Cmp(expected.SerialNumber) != 0 {
t.Errorf("resp.SerialNumber: got %x, want %x", resp.SerialNumber, expected.SerialNumber)
}
if resp.RevocationReason != expected.RevocationReason {
t.Errorf("resp.RevocationReason: got %d, want %d", resp.RevocationReason, expected.RevocationReason)
}
}
示例13: TestWalk
func TestWalk(t *testing.T) {
m := NewModel("testdata")
files, ignores := m.Walk(false)
if l1, l2 := len(files), len(testdata); l1 != l2 {
t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
}
for i := range testdata {
if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
}
if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
}
t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
if mt := files[i].Modified; mt < t0 || mt > t1 {
t.Errorf("Unrealistic modtime %d for test %d", mt, i)
}
}
if !reflect.DeepEqual(ignores, correctIgnores) {
t.Errorf("Incorrect ignores\n %v\n %v", correctIgnores, ignores)
}
}
示例14: TestPSQLRemStoredCdrs
func TestPSQLRemStoredCdrs(t *testing.T) {
if !*testLocal {
return
}
cgrIdB1 := utils.Sha1("bbb1", time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC).String())
if err := psqlDb.RemStoredCdrs([]string{cgrIdB1}); err != nil {
t.Error(err.Error())
}
if storedCdrs, _, err := psqlDb.GetStoredCdrs(new(utils.CdrsFilter)); err != nil {
t.Error(err.Error())
} else if len(storedCdrs) != 7 {
t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs)
}
tm, _ := utils.ParseTimeDetectLayout("2013-11-08T08:42:20Z", "")
cgrIdA1 := utils.Sha1("aaa1", tm.String())
tm, _ = utils.ParseTimeDetectLayout("2013-11-08T08:42:22Z", "")
cgrIdA2 := utils.Sha1("aaa2", tm.String())
tm, _ = utils.ParseTimeDetectLayout("2013-11-07T08:42:24Z", "")
cgrIdA3 := utils.Sha1("aaa3", tm.String())
tm, _ = utils.ParseTimeDetectLayout("2013-11-07T08:42:21Z", "")
cgrIdA4 := utils.Sha1("aaa4", tm.String())
tm, _ = utils.ParseTimeDetectLayout("2013-11-07T08:42:25Z", "")
cgrIdA5 := utils.Sha1("aaa5", tm.String())
cgrIdB2 := utils.Sha1("bbb2", time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC).String())
cgrIdB3 := utils.Sha1("bbb3", time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC).String())
if err := psqlDb.RemStoredCdrs([]string{cgrIdA1, cgrIdA2, cgrIdA3, cgrIdA4, cgrIdA5,
cgrIdB2, cgrIdB3}); err != nil {
t.Error(err.Error())
}
if storedCdrs, _, err := psqlDb.GetStoredCdrs(new(utils.CdrsFilter)); err != nil {
t.Error(err.Error())
} else if len(storedCdrs) != 0 {
t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs)
}
}
示例15: TestSimpleReaderTime
func TestSimpleReaderTime(t *testing.T) {
b := new(bytes.Buffer)
writer := NewWriter(b, true)
now := time.Now()
writer.Serialize(now)
writer.Serialize(now)
datetime := time.Date(2014, 1, 21, 12, 13, 14, 0, time.Local)
writer.Serialize(datetime)
date := time.Date(2014, 1, 21, 0, 0, 0, 0, time.Local)
writer.Serialize(date)
tim := time.Date(1, 1, 1, 19, 23, 19, 123000, time.UTC)
writer.Serialize(tim)
// go 1.1 has a bug, 1.2 fixed
// writer.Serialize(datetime.String())
reader := NewReader(b, true)
var x time.Time
var err error
if err = reader.Unserialize(&x); err != nil {
t.Error(err.Error())
}
if x != now {
t.Error(x)
}
var p *time.Time
if err = reader.Unserialize(&p); err != nil {
t.Error(err.Error())
}
if *p != now {
t.Error(*p)
t.Error(now)
}
if err = reader.Unserialize(&x); err != nil {
t.Error(err.Error())
}
if x != datetime {
t.Error(x)
}
if err = reader.Unserialize(&x); err != nil {
t.Error(err.Error())
}
if x != date {
t.Error(x)
}
if err = reader.Unserialize(&x); err != nil {
t.Error(err.Error())
}
if x != tim {
t.Error(x)
}
// go 1.1 has a bug, 1.2 fixed
/*
if err = reader.Unserialize(&x); err != nil {
t.Error(err.Error())
}
if x != datetime {
t.Error(x)
t.Error(datetime)
}
*/
}