参考: https://golangcode.com/checking-if-date-has-been-set/

https://stackoverflow.com/questions/20924303/date-time-comparison-in-golang

// utc life
loc, _ := time.LoadLocation("UTC") // setup a start and end time
createdAt := time.Now().In(loc).Add(1 * time.Hour)
expiresAt := time.Now().In(loc).Add(4 * time.Hour) // get the diff
diff := expiresAt.Sub(createdAt)
fmt.Printf("Lifespan is %+v", diff)

---------------------------------------------------------------

Check If a Date/Time Has Been Set with IsZero

Feb 16, 2019 · 175 words · 1 minute read

In Go, we can store and use dates using the time package and although a date in Go cannot be saved as null (because there’s no such thing) there is an unset state. This unset state can be shown as 0001-01-01 00:00:00 +0000 UTC and there’s a simple way we can check if a date variable has been populated, as demonstrated below. It’s also important to note that these are not unix timestamps, which go back as far as 1970, but can handle a large spectrum of dates.

package main

import (
"fmt"
"time"
) func main() { var myDate time.Time // IsZero returns a bool of whether a date has been set, but as the printf shows it will
// still print a zero-based date if it hasn't been set.
if myDate.IsZero() {
fmt.Printf("No date has been set, %s\n", myDate)
} // Demonstrating that by setting a date, IsZero now returns false
myDate = time.Date(2019, time.February, 1, 0, 0, 0, 0, time.UTC)
if !myDate.IsZero() {
fmt.Printf("A date has been set, %s\n", myDate)
}
}

Use the time package to work with time information in Go.

Time instants can be compared using the Before, After, and Equal methods. The Sub method subtracts two instants, producing a Duration. The Add method adds a Time and a Duration, producing a Time.

Play example:

package main

import (
"fmt"
"time"
) func inTimeSpan(start, end, check time.Time) bool {
return check.After(start) && check.Before(end)
} func main() {
start, _ := time.Parse(time.RFC822, "01 Jan 15 10:00 UTC")
end, _ := time.Parse(time.RFC822, "01 Jan 16 10:00 UTC") in, _ := time.Parse(time.RFC822, "01 Jan 15 20:00 UTC")
out, _ := time.Parse(time.RFC822, "01 Jan 17 10:00 UTC") if inTimeSpan(start, end, in) {
fmt.Println(in, "is between", start, "and", end, ".")
} if !inTimeSpan(start, end, out) {
fmt.Println(out, "is not between", start, "and", end, ".")
}
}

golang 时间的比较,time.Time的初始值?的更多相关文章

  1. Memcached Memcached.ClientLibrary.SockIOPool”的类型初始值设定项引发异常

    又一次遭遇"xxx类型初始值设定项引发异常" 下了个c#实现的轻量级IoC开源项目,可是在本地使用时发现一运行就捕捉到"类型初始值设定项引发异常"的异常信息,调 ...

  2. C#变量初始化问题:字段初始值无法引用非静态字段、方法或属性

    http://www.cnblogs.com/bluestorm/p/3432190.html 问题:字段初始值设定项无法引用非静态字段.方法或属性的问题 下面代码出错的原因,在类中定义的字段为什么不 ...

  3. 你好,C++(11)如何用string数据类型表示一串文字?根据初始值自动推断数据类型的auto关键字(C++ 11)

    3.5.2  字符串类型 使用char类型的变量我们可以表示单个字符,那么,我们又该如何表示拥有多个字符的字符串呢? 我们注意到,一个字符串是由多个字符串连起来形成的.很自然地,一种最简单直接的方法就 ...

  4. “NHibernate.Cfg.Configuration 的类型初始值设定项引发异常。”的解决方法【备忘】

    今天搞到NHibernate时,突然报了一个“NHibernate.Cfg.Configuration 的类型初始值设定项引发异常.”的异常. 详细异常信息“System.IO.FileLoadExc ...

  5. C# static 字段初始值设定项无法引用非静态字段、方法或属性

    问题:字段或属性的问题字段初始值设定项无法引用非静态字段.方法 下面代码出错的原因,在类中定义的字段为什么不能用? public string text = test(); //提示 字段或属性的问题 ...

  6. C#中将dateTimePicker初始值设置为空

    最近在做一个小项目,有一个功能是根据用户选择条件查询数据,要求时间控件的默认值为空,只有当用户修改了时间,才根据时间查询.简单的说,就是默认或者点击清空按钮的情况下,时间控件dateTimePicke ...

  7. Vue 恢复初始值的快速方法

    vue 中经常定义很多data ,在用户进行一些操作后,需要讲data中的某个对象定义为初始值 例如 form: { title: '', describe: '', inspectionCatego ...

  8. [转]Vue中用props给data赋初始值遇到的问题解决

    原文地址:https://segmentfault.com/a/1190000017149162 2018-11-28更:文章发布后因为存在理解错误,经@Kim09AI同学提醒后做了调整,在此深表感谢 ...

  9. Vue中用props给data赋初始值遇到的问题解决

    Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14   作者:yuyongyu    我要评论   这篇文章主要介绍了Vue中用props给dat ...

  10. 简单模拟IOC容器:为添加了@Autowired的属性赋值(初始值)

    创建@Autowired注解 package com.zzj.test; import java.lang.annotation.ElementType; import java.lang.annot ...

随机推荐

  1. os删除文件或者文件夹

    最近在使用os.remove删除目录时报错了,这里记录下os操作文件或者目录的常用方法 递归删除文件或文件夹 import shutil shutil.rmtree() 文件的重命名 os.renam ...

  2. Python 解LeetCode:394 Decode String

    题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...

  3. Java中常用的设计模式代码与理解

    Java中常用的设计模式代码与理解 一.单例模式 1.饿汉式 (太饿了,类加载的时候就创建实例) /** * 饿汉式单例模式 */ public class HungrySingleInstance ...

  4. [C++] 习题 2.18 倒序查找字串

    目录 前置技能 字符串 KMP 算法 需求描述 概要设计 具体实现 string.cpp strmatching.cpp main.cpp 倒序查找字串: 设计一个算法,在串 str 中查找字串 su ...

  5. Python-02-基础知识

    一.第一个Python程序 [第一步]新建一个hello.txt [第二步]将后缀名txt改为py [第三步]使用记事本编辑该文件 [第四步]在cmd中运行该文件 print("Hello ...

  6. 【LEETCODE】36、121题,Best Time to Buy and Sell Stock

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. Scratch(三)剪刀石头布

    经过上一讲的突击训练,我们从门外汉开始走向编程的深坑,我们今天还要对上一讲的游戏进行加强. 上一个游戏还能演变成什么游戏呢? 我其实知道你们想到的是老hu机什么的,确实,上一个游戏改改可以变成老hu机 ...

  8. Vue使用指南(三)

    组件 '''1.根组件:new Vue()创建的组件,一般不明确自身的模板,模板就采用挂载点2.局部组件: local_component = {}2.全局组件: Vue.component({})' ...

  9. 对称加密,非对称加密,数字签名,https

    对称加密和非对称加密 对称加密 概念:加密秘钥和解密秘钥使用相同的秘钥(即加密和解密都必须使用同一个秘钥) 特点:一对一的双向保密通信(每一方既可用该秘钥加密,也可用该秘钥解密,非对称加密是多对一的单 ...

  10. 在论坛中出现的比较难的sql问题:13(循环替换问题 过滤各种标点符号)

    原文:在论坛中出现的比较难的sql问题:13(循环替换问题 过滤各种标点符号) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 去掉一个字段中的标点符号的SQL语句怎么写 ...