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. json_decode()和json_encode()的使用方法

    json_decode对JSON格式的字符串进行编码 json_encode对变量进行 JSON 编码 JS中对JSON的解析 一.JSON字符串转换为JSON对象     要运用上面的str1,必须 ...

  2. 对xml文件的简单解析

    package com.eprobj.demo; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; impor ...

  3. PHPCMS V9教程之快速入门

    这篇文章要为大家来介绍PHPCMS V9这个系统的一些基本知识,PHPCMS是基于面向对象的,严格的安装MVC开发模式开发的CMS系统,同时他还是一个非 常不错的PHP框架.下面我们一起看一下PHPC ...

  4. Response.Redirect()、Server.Execute和Server.Transfer的区别

    1.Response.Redirect(): Response.Redirect方法导致浏览器链接到一个指定的URL. 当Response.Redirect()方法被调用时,它会创建一个应答,应答头中 ...

  5. 使用Java中File类批量创建文件和批量修改文件名

    批量创建文件 int cont = 1; String s = "E:\\学习资料\\Java笔记-"; while(cont<100){ File f = new File ...

  6. C++复制控制

    1.复制构造函数可用于: (1)根据另一个同类型的对象显示或隐式初始化一个对象 string str1="test";   //隐式 string str2=str1; //显示 ...

  7. DataTemplate应用

    在WPF中,决定数据外观的是DataTemplate,即DataTemplate是数据内容的表现形式,一条数据显示成什么样子,是简单的文本还是直观的图形,就是由DataTemplate决定的.下面通过 ...

  8. Kinect SDK 安装失败

    错误提示:Kinect Management failed to start. 原因: 1, Kinect Management 服务相依的 Plug and Play的服务没有启动. 2,系统安装了 ...

  9. ABAP 数值类型转换

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'       EXPORTING         input  = wa_zz-werks       IMPOR ...

  10. [转载]Masonry介绍与使用实践(快速上手Autolayout)

    原博地址 http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/ 前言 1 MagicNumber -> aut ...