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 ...
随机推荐
- 【Eclipse】在Eclipse工具中自定义类注释
直接上图:这个公司基本都已经定制好了,自己写demo的时候可以适当定制自己的注释 package com.zlg.controller; zlg : 此处输入zlg(对应模版的名称) 然后ALT+/ ...
- css常用效果总结
1.给input的placeholder设置颜色 .phColor::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color:maroo ...
- Install OE and BitBake
LeapFrog Explorers: Install OE and BitBake - eLinux.org http://elinux.org/LeapFrog_Explorers:_In ...
- 左侧导航栏复制粘贴保存html即可
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 第七天 面向对象进阶与socket编程
1.静态方法(用得少)(解除某个函数跟类的关联,加了静态方法后,类便不能将类的参数传给静态方法函数了) class Dog(object): def __init__(self,name): @sta ...
- linux shell脚本通过参数名传递参数值
平常在写shell脚本都是用$1,$2....这种方式来接收参数,然而这种接收参数的方式不但容易忘记且不易于理解和维护.Linux常用的命令都可指定参数名和参数值,然而我们怎样才能给自己的shell脚 ...
- ios NSURLSession completeHandler默认调用quque
注意 , [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSU ...
- Enum:Game of Lines(POJ 3668)
画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...
- 【linux】学习4
文件压缩: gzip :压缩 解压缩 zcat: 读取压缩文件 gzip text1 :压缩text1 得到 text1.gz 原文件不见了 gzip -c text1 > text1.g ...
- iOS-消息推送机制的实现
OS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务器 ...