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. 基于Selenium的模拟浏览器采集

    Selenium 也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7.8.9).Mozilla Firefox.Mozil ...

  2. C#中堆和栈的区别分析

    线程堆栈:简称栈 Stack托管堆: 简称堆 Heap 使用.Net框架开发程序的时候,我们无需关心内存分配问题,因为有GC这个大管家给我们料理一切.如果我们写出如下两段代码: 1 代码段1: 2 3 ...

  3. FFmpeg-20160422-snapshot-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...

  4. 插头DP专题

    建议入门的人先看cd琦的<基于连通性状态压缩的动态规划问题>.事半功倍. 插头DP其实是比较久以前听说的一个东西,当初是水了几道水题,最近打算温习一下,顺便看下能否入门之类. 插头DP建议 ...

  5. Linux rpm 命令参数使用详解[介绍和应用]

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两 ...

  6. linq lanbda表达式的用法

    1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq:    from s in Student ...

  7. WPS文字在表格中打字自动跳动

    可以设置表格的属性来实现. 1.选择表格,点击鼠标右键,选择“表格属性” 2.在出现的对话框中,文字环绕选择“无”,“行”的设置为“允许跨页断行”,就可以了.

  8. PHP安全编程:过滤用户输入

    如果你能正确可靠地识别和过滤输入,你的工作就基本完成了.最后一步是使用一个命名约定或其它可以帮助你正确和可靠地区分已过滤和被污染数据的方 法.我推荐一个比较简单的命名约定,因为它可以同时用在面向过程和 ...

  9. SQL语句题库

    一.    填空题 Not Only SQL数据库 泛指  非关系型数据库  . SYS和SYSTEM用户都是Oracle 的系统用户,它们都使用SYSTEM表空间,其中 sys 拥有更大的权限. O ...

  10. 存储过程使用CTE 和 case when

    未用SQL CTE and case when: ALTER PROCEDURE [dbo].[usp_rptDropboxBatchSummary1] )='ALL', )='ALL', )='AL ...