http://blog.chinaunix.net/uid-24774106-id-4006530.html
编程离不开时间,时间管理,严格的说分成两块,一个是当前的时刻,对应的是一个点,还有是一段时间间隔。本文简单的讲讲go的时间相关的编程,比较简单,高手可以一笑而过。
golang对时间的支持,是package time做的事儿,里面有好多的函数,我就不一一举例学习,毕竟这是官方文档干的事情。我们初步的学习下常用的函数。
第一个是UNIX epoch time,确切的说就是自1970-01-01 00:00:00 GMT以来的秒数,不知道如何获取的,可以在shell下执行 date
+%s
-
manu@manu-hacks:~/code/go/self$
date +%s
- 1385131172
-
#include <time.h>
-
- time_t now = time(NULL);
-
type Time struct {
-
// sec gives the number of seconds elapsed since
-
// January 1, year 1 00:00:00 UTC.
-
sec int64
-
-
// nsec specifies a non-negative nanosecond
-
// offset within the second named by Seconds.
-
// It must be in the range [0, 999999999].
-
nsec int32
-
-
// loc specifies the Location that should be used to
-
// determine the minute, hour, month, day, and year
-
// that correspond to this Time.
-
// Only the zero Time has a nil Location.
-
// In that case it is interpreted to mean UTC.
-
loc *Location
- }
-
now := time.Now()
-
获取UNIX epoch time:
- var epoch_seconds int64 = now.Unix()
-
func (t Time) Year() int
-
- cur_year := now.Year()
-
func (t Time) Month() Month
-
- cur_month := now.Month()
-
- if cur_month == time.November {
-
...
- }
-
const (
-
January Month = 1 + iota
-
February
-
March
-
April
-
May
-
June
-
July
-
August
-
September
-
October
-
November
-
December
- )
-
func (t Time) Date() (year int, month
Month, day int)
-
- year,mon,day = now.Date()
-
- func (t Time) Hour() int
- cur_hour := now.Hour()
-
time还提供了Clock()的同时返回 hour,minute,second = now.Clock().
在C语言中,我们用gmtime_r获取UTC时间,localtime_r获取本地时间,Golang我们也可以做到
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<time.h>
-
-
-
int main()
-
{
-
time_t now = time(NULL);
-
printf("elapsed %d second since 1970-01-01 00:00:00\n",now);
-
-
struct tm now_utc_tm ={0};
-
if (gmtime_r(&now,&now_utc_tm) != NULL)
-
{
-
printf("UTC time is %d-%02d-%02d %02d:%02d:%02d %s\n",
-
now_utc_tm.tm_year+1900,now_utc_tm.tm_mon,
-
now_utc_tm.tm_mday,now_utc_tm.tm_hour,
-
now_utc_tm.tm_min,now_utc_tm.tm_sec,now_utc_tm.tm_zone);
-
}
-
-
struct tm now_local_tm = {0} ;
-
if(localtime_r(&now,&now_local_tm) != NULL)
-
{
-
printf("local time is %d-%02d-%02d %02d:%02d:%02d %s\n",
-
now_local_tm.tm_year+1900,now_local_tm.tm_mon,
-
now_local_tm.tm_mday,now_local_tm.tm_hour,
-
now_local_tm.tm_min, now_local_tm.tm_sec,now_local_tm.tm_zone);
-
}
-
-
return 0;
-
- }
-
package main
-
-
import "fmt"
-
import "time"
-
-
-
-
func main(){
-
-
now := time.Now()
-
year,mon,day := now.UTC().Date()
-
hour,min,sec := now.UTC().Clock()
-
zone,_ := now.UTC().Zone()
-
fmt.Printf("UTC time is %d-%d-%d %02d:%02d:%02d %s\n",
-
year,mon,day,hour,min,sec,zone)
-
-
year,mon,day = now.Date()
-
hour,min,sec = now.Clock()
-
zone,_ = now.Zone()
-
fmt.Printf("local time is %d-%d-%d %02d:%02d:%02d %s\n",
-
year,mon,day,hour,min,sec,zone)
- }
-
C版本的输出
-
------------------
-
UTC time is 2013-10-22
15:49:18 GMT
-
local time is 2013-10-22
23:49:18 CST
-
-
go版本的输出
-
---------------------
-
UTC time is 2013-11-22
15:51:22 UTC
- local time is 2013-11-22 23:51:22 CST
我们另一个关心的话题,是时间间隔,比如我们profile一个以非常耗时的function,我们会在函数开始前记下时刻值,函数结束后,再次记录下时刻值,然后两者的差值,就是函数运行时间。
这表明Time是可以相减的,
-
start_time := time.Now()
-
expensive_function
-
end_time :=time.Now()
-
- var duration Duration = end_time.Sub(start_time)
-
type Duration int64
-
-
const (
-
Nanosecond Duration = 1
-
Microsecond = 1000 * Nanosecond
-
Millisecond = 1000 * Microsecond
-
Second = 1000 * Millisecond
-
Minute = 60 * Second
-
Hour = 60 * Minute
- )
- now := time.Now()
-
time.Sleep(3*time.Second);
- end_time := time.Now()
-
-
var dur_time time.Duration = end_time.Sub(now)
-
var elapsed_min float64 = dur_time.Minutes()
-
var elapsed_sec float64 = dur_time.Seconds()
-
var elapsed_nano int64 = dur_time.Nanoseconds()
-
fmt.Printf("elasped %f minutes or \nelapsed %f seconds or \nelapsed %d nanoseconds\n",
- elapsed_min,elapsed_sec,elapsed_nano)
-
elasped 0.050005 minutes or
-
elapsed 3.000292 seconds or
- elapsed 3000292435 nanoseconds
第二部分描述Duration明显用到了Sleep()函数,这个函数是以纳秒为单位的,相当于C语言中的nanosleep()
-
#include <time.h>
- nanosleep(): _POSIX_C_SOURCE >= 199309L
-
- int nanosleep(const struct timespec *req, struct timespec *rem);
#include
unsigned int sleep(unsigned int seconds);
如果sleep 3秒中需要写成:
- time.Sleep(3000000000)
- time.Sleep(3*time.Second);
这个time package还有很多其他的内容,我就不一一赘述了。
参考文献:
1 golang package time
有疑问加站长微信联系(非本文作者)