package main

import (
"fmt"
"math"
"time"
) /*
@闰年判断
·封装一个函数判断某一个年份是不是闰年(整4不整百,或者整四百)(例如:公元4年是如年,公元100、200、300不是闰年,公元400年是闰年)
·计算一年的精确天数
@根据当前时间纳秒求年月日
·time.Now().Unix()会返回距离1970年零时所逝去的时间秒数:1234567890
·据此求出今天的年月日(一年有60*24*365秒,整除得年;对一年的秒数求余,看看余数中有多少完整的60*24*30就有多少个月;)
·不许借助外部包,尤其是time包!
*/ func IsLeapYear(year int) bool {
if (year%4 == 0 && year%100 != 0) || (year%400 == 0) ||(year%1000==0){
return true
}
return false
} var OrdinaryYearMonthDays = [12]int{31,28,31,30,31,30,31,31,30,31,30,32}
var LeapYearMonthDays = [12]int{31,29,31,30,31,30,31,31,30,31,30,32} func GetDate(elapsedSeconds int)(year,month,day int) { //var year,month,day int for i := 1900; i <= 2000; i++ {
if IsLeapYear(i) {
fmt.Println(i, "是闰年")
}
} //一年的精确天数
daysPerYear := (365*400 + (24*3 + 25)) / 400.0
secondsPerDay := float64(24 * 3600)
secondsPerYear := secondsPerDay * daysPerYear //计算今年是哪一年
//elapsedSeconds := int(time.Now().Unix())
elapsedYears := int(float64(elapsedSeconds) / secondsPerYear)
year = 1970 + elapsedYears
fmt.Println(year) //计算当前是几月
//今年逝去的秒数:1970年后逝去的总秒数-今年以前的48个整年所包含的秒数
thisYearSeconds := float64(elapsedSeconds) - float64(elapsedYears)*secondsPerYear
//今年过去了多少天
thisYearDays := int(math.Ceil(thisYearSeconds / secondsPerDay))
fmt.Println(thisYearDays) var thisYearMonthDays [12]int
if IsLeapYear(year){
thisYearMonthDays = LeapYearMonthDays
}else {
thisYearMonthDays = OrdinaryYearMonthDays
} var tempDays = 0
for i, monthDays:=range thisYearMonthDays {
pastMonthDays := tempDays
tempDays += monthDays
fmt.Println(i,tempDays)
if tempDays >= thisYearDays{
month =i+1
//今年过去的天数-过去11个整月的天数
day = thisYearDays - pastMonthDays
break
}
}
fmt.Println("month=", month)
fmt.Println("day=", day)
return
} func main() {
//year, month, day := GetDate(int(time.Now().Unix()))
//fmt.Printf("当前是%d年%d月%d日", year,month,day)
pastSeconds := int(time.Date(2019, 11, 18, 22, 57, 1, 0, time.Local).Unix())
year, month, day := GetDate(pastSeconds)
fmt.Printf("当前是%d年%d月%d日\n", year,month,day)
fmt.Println(pastSeconds,int(time.Now().Unix()))
}

  

Go语言练习---判断闰年以及根据现在的秒数求现在的年月日的更多相关文章

  1. C 语言实例 - 判断闰年

    用户输入年份,判断该年份是否为闰年. #include <stdio.h> int main() { int year; printf("输入年份: "); scanf ...

  2. C语言初学 判断闰年的问题

    #include<stdio.h> main( ) {  int   year , leap; scanf("%d",&year);             i ...

  3. 【C语言】判断某一正整数是否为完数

    什么是完数? 如果一个数等于它的因子之和,则称该数为“完数”(或“完全数”). 例如,6的因子为1.2.3,而 6=1+2+3,因此6是“完数”. 程序框图:m  问题分析 根据完数的定义,解决本题的 ...

  4. 判断闰年C语言版

    #include<stdio.h> int isLeap(int year) { // 必须先判断是平年的情况 后判断闰年的情况 == && year%!=) || yea ...

  5. c#判断闰年

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. php判断闰年

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. java练习题:解一元二次方程、判断闰年、判断标准身材、三个数取最大值

    1.解一元二次方程 注:求根公式为(-b+根号德尔塔)/2a,(-b-根号德尔塔)/2a Scanner sc=new Scanner(System.in); System.out.println(& ...

  8. 判断闰年的方法以及如何获得单链表的倒数第K个元素

    今天很悲催,心中向往的公司,打电话过来面试,问到我两个问题,结果竟然都没有回答上,伤心了,记录下今天失败,希望以后不要被同样的问题给PASS. 问题1.如何判断是否为闰年 所谓闰年那就是:四年一闰,百 ...

  9. c语言 如何判断是不是 可显字符

    c语言 如何判断是不是 可显字符int isprint(int c)若可显示返回1,否则0:要包含头文件ctype.h

随机推荐

  1. k8s service NodePort 方式向外发布

    k8s service NodePort 方式向外发布 k8s 无头service 方式向内发布 k8s service 服务发现 {ServiceName}.{Namespace}.svc.{Clu ...

  2. Weekly Contest 139

    1071. Greatest Common Divisor of Strings For strings S and T, we say "T divides S" if and ...

  3. Eureka原理剖析

    Eureka作为微服务中的注册中心,为微服务集群间各个服务进行调用提供寻址的功能,有了它集群间的服务只需要指定服务名称就可以了,无需再去关心服务具体部署的服务器IP,即可正常调用.下面来对其中我们开发 ...

  4. git 配置ssh

    git 配置ssh 生成一个个人账号/邮箱的sshkey ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/XX ...

  5. CVE-2013-1347:Microsoft IE CGenericElement UAF 漏洞利用样本分析

    CVE-2013-1347 漏洞是典型的 IE 浏览器 UAF 漏洞,所以其利用方法和一般的 IE 浏览器漏洞的利用方法非常相似,所以流程大体上可以分为这些步骤:(1) 对象被释放 (2) 精确覆盖被 ...

  6. Github搜索语法

    目录 使用 Github 进行邮件配置信息收集 使用Github进行数据库信息收集

  7. 简述MySQL优化

    数据库的优化可以从四个方面来优化: 1.结构层: web服务器采用负载均衡服务器,mysql服务器采用主从复制,读写分离 2.储存层: 采用合适的存储引擎,采用三范式 3.设计层: 采用分区分表,索引 ...

  8. 分布式ID

    需求 全局唯一 高性能 高可用 简单易用 UUID 优点: 唯一 不依赖于任何第三方服务 缺点: 是字符串类型而非数字,不满足数字ID的需求 字符串太长了,DB查询效率受影响 数据库自增ID 如果使用 ...

  9. docker学习之使用 DockerFile 构建镜像并搭建 swarm+compose 集群

    题目要求 (1)将springboot应用程序打成jar包:Hot.jar (2)利用dockerfile将Hot.jar构建成镜像 (3)构建 Swarm 集群 (4)在 Swarm 集群中使用 c ...

  10. C++ primer plus读书笔记——第15章 友元、异常和其他

    第15章 友元.异常和其他 1. 友元类的所有方法都可以访问原有类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数.成员函数.或类为友元是由类定义的, ...