golang time and duration
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的更多相关文章
- golang time.Duration()的问题解疑
原文: How to multiply duration by integer? 看到golang项目中的一段代码, ---------------------------------------- ...
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- channel Golang
Golang, 以17个简短代码片段,切底弄懂 channel 基础 (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的 ...
- golang 裸写一个pool池控制协程的大小
这几天深入的研究了一下golang 的协程,读了一个好文 http://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=2653369770& ...
- TODO:Golang UDP连接简单测试慎用Deadline
TODO:Golang UDP连接简单测试慎用Deadline UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interco ...
- Ubuntu14.04+RabbitMQ3.6.3+Golang的最佳实践
目录 [TOC] 1.RabbitMQ介绍 1.1.什么是RabbitMQ? RabbitMQ 是由 LShift 提供的一个 Advanced Message Queuing Protocol ...
- golang开发缓存组件
代码地址github:cache 花了一天时间看了下实验楼的cache组件,使用golang编写的,收获还是蛮多的,缓存组件的设计其实挺简单的,主要思路或者设计点如下: 全局struct对象:用来做缓 ...
- golang中的race检测
golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...
- [Golang] 一个简易代理池
晚上写了一个代理池,就是在一个代理网站上爬取代理ip和端口以及测试是否可用.接下来可能考虑扩展成一个比较大的 golang实现的代理池. 简易版代码: package main import ( &q ...
随机推荐
- nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoSupport': ...
- phpcms中action值的含义
action值的含义:lists 内容数据(文章?)列表relation 内容相关文章hits 内容数据点击排行榜category 内容栏目列表position 内容推荐位列表
- PHP的反射类ReflectionClass、ReflectionMethod使用实例
PHP5 具有完整的反射API,添加对类.接口.函数.方法和扩展进行反向工程的能力. 反射是什么? 它是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类.方法.属性.参数等的详细信息,包括 ...
- session的生命周期
Session存储在服务器端,一般为了防止在服务器的内存中(为了高速存取),Sessinon在用户访问第一次访问服务器时创建,需要注意只有访问JSP.Servlet等程序时才会创建Session,只访 ...
- [k]web页面-browser兼容问题-1
1:空的a标签在IE7/8下不能点击(2015-05-22) html代码: <ul class='oUl'><li><a href="#"> ...
- Java中 final static super this instanceof 关键字用法
一.final关键字 final可以修饰变量.方法及类: 1.当定义一个final变量时,jvm会将其分配到常量池中,其所修饰的对象只能赋值一次,对基本类型来说是其值不可变,引用类型(包括作为函数形参 ...
- mach 和 array 方法
- strcpy C++实现
#include <iostream> #include <assert.h> using namespace std; char *strcpy(char *strDest, ...
- ios nsdataformatter奇怪的问题
用nsdataformatter在中文格式下测试, a 标识的是上午,下午,不是AM,pm. 我在24小时格式的机器上测试,存入了一个 时间,却解析不出来了! static NSString *Hom ...
- nginx 原理&知识
2015年6月4日 17:04:20 星期四 发现两个关于nginx原理的系列文章, 非常好 http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html ...