package mainimport "fmt"import "time"func main() {    p := fmt.Println    // We'll start by getting the current time.    now := time.Now()    p("time.Now(): ", now)    // You can build a "time" struct by providing the    // year, month, day, etc. Times are always associated    // with a "Location", i.e. time zone.    then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)    p("time.Date(): ", then)    p("==================================")    // You can extract the various components of the time    // value as expected.    p("time.Date().Year(): ", then.Year())    p("time.Date().Month(): ", then.Month())    p("time.Date().Day(): ", then.Day())    p("time.Date().Hour(): ", then.Hour())    p("time.Date().Minute(): ", then.Minute())    p("time.Date().Second(): ", then.Second())    p("time.Date().Nanosecond(): ", then.Nanosecond())    p("time.Date().Location(): ", then.Location())    // The Monday-Sunday "Weekday" is also available.    p("time.Date().Weekday(): ", then.Weekday())    p("==================================")    // These methods compare two times, testing if the    // first occurs before, after, or at the same time    // as the second, respectively.    p("time.Date().Before(now): ", then.Before(now))    p("time.Date().After(now): ", then.After(now))    p("time.Date().Equal(now): ", then.Equal(now))    p("==================================")    // The "Sub" methods returns a "Duration" representing    // the interval between two times.    diff := now.Sub(then)    p("time.Now().Sub(time.Date()): ", diff)    p("==================================")    // We can compute the length of the duration in    // various units.    p("time.Date().Sub(then).Hours(): ", diff.Hours())    p("time.Date().Sub(then).Minutes(): ", diff.Minutes())    p("time.Date().Sub(then).Seconds(): ", diff.Seconds())    p("time.Date().Sub(then).Nanoseconds(): ", diff.Nanoseconds())    p("==================================")    // You can use "Add" to advance a time by a given    // duration, or with a "-" to move backwards by a    // duration.    p("time.Date().Sub(now).Add(now.Sub(then)): ", now.Add(diff))    p("time.Date().Sub(then).Add(-(now.Sub(then))): ", then.Add(-diff))}

golang time and duration的更多相关文章

  1. golang time.Duration()的问题解疑

    原文:  How to multiply duration by integer? 看到golang项目中的一段代码, ---------------------------------------- ...

  2. Golang, 以17个简短代码片段,切底弄懂 channel 基础

    (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...

  3. channel Golang

    Golang, 以17个简短代码片段,切底弄懂 channel 基础 (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的 ...

  4. golang 裸写一个pool池控制协程的大小

    这几天深入的研究了一下golang 的协程,读了一个好文 http://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=2653369770& ...

  5. TODO:Golang UDP连接简单测试慎用Deadline

    TODO:Golang UDP连接简单测试慎用Deadline UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interco ...

  6. Ubuntu14.04+RabbitMQ3.6.3+Golang的最佳实践

    目录 [TOC] 1.RabbitMQ介绍 1.1.什么是RabbitMQ?   RabbitMQ 是由 LShift 提供的一个 Advanced Message Queuing Protocol ...

  7. golang开发缓存组件

    代码地址github:cache 花了一天时间看了下实验楼的cache组件,使用golang编写的,收获还是蛮多的,缓存组件的设计其实挺简单的,主要思路或者设计点如下: 全局struct对象:用来做缓 ...

  8. golang中的race检测

    golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...

  9. [Golang] 一个简易代理池

    晚上写了一个代理池,就是在一个代理网站上爬取代理ip和端口以及测试是否可用.接下来可能考虑扩展成一个比较大的 golang实现的代理池. 简易版代码: package main import ( &q ...

随机推荐

  1. 7.3---直线是否相交(CC150)

    注意:就算斜率相等,但是,如果截距也相等,那么是属于相交,所以要特殊判断. public class CrossLine { public boolean checkCrossLine(double ...

  2. H-Index I & II

    H-Index I Given an array of citations (each citation is a non-negative integer) of a researcher, wri ...

  3. C#之文本操作

    [转载]C#文件操作大全(SamWang) 文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msd ...

  4. catalan number

    http://blog.csdn.net/yutianzuijin/article/details/13161721

  5. unity3D技术之事件函数的执行顺序[转]

    unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703   在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...

  6. iOS App Extensions 推荐文章

    写的非常不错,读完后,基本的extension的套路就清楚了,也是我们的园友写的,感谢他: http://www.cnblogs.com/xdream86/p/3855932.html 下面这个教程是 ...

  7. c++ template

    在类中其中一个函数使用模板,函数定义和实现必须放在头文件里. ca.h template<typename T> void swap2(T &a,T &b) { T c=a ...

  8. 一些笔试题(C/C++)

    1.there are two variables, don't use if.. else or ?: or switch or other judgement statements,find ou ...

  9. Html之head部分详解

    随便打开一个网页,右击查看网页源代码,总能看到<head>-</head>封闭标签,在里面通常会包含5类标签:title.link.script.meta.style.这5类标 ...

  10. maven加载本地lib下的jar包(pom.xml)

    1.将本地jar放置到仓储库在jar包目录下 mvn install:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserve ...