Go语言练习---判断闰年以及根据现在的秒数求现在的年月日
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语言练习---判断闰年以及根据现在的秒数求现在的年月日的更多相关文章
- C 语言实例 - 判断闰年
用户输入年份,判断该年份是否为闰年. #include <stdio.h> int main() { int year; printf("输入年份: "); scanf ...
- C语言初学 判断闰年的问题
#include<stdio.h> main( ) { int year , leap; scanf("%d",&year); i ...
- 【C语言】判断某一正整数是否为完数
什么是完数? 如果一个数等于它的因子之和,则称该数为“完数”(或“完全数”). 例如,6的因子为1.2.3,而 6=1+2+3,因此6是“完数”. 程序框图:m 问题分析 根据完数的定义,解决本题的 ...
- 判断闰年C语言版
#include<stdio.h> int isLeap(int year) { // 必须先判断是平年的情况 后判断闰年的情况 == && year%!=) || yea ...
- c#判断闰年
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- php判断闰年
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- java练习题:解一元二次方程、判断闰年、判断标准身材、三个数取最大值
1.解一元二次方程 注:求根公式为(-b+根号德尔塔)/2a,(-b-根号德尔塔)/2a Scanner sc=new Scanner(System.in); System.out.println(& ...
- 判断闰年的方法以及如何获得单链表的倒数第K个元素
今天很悲催,心中向往的公司,打电话过来面试,问到我两个问题,结果竟然都没有回答上,伤心了,记录下今天失败,希望以后不要被同样的问题给PASS. 问题1.如何判断是否为闰年 所谓闰年那就是:四年一闰,百 ...
- c语言 如何判断是不是 可显字符
c语言 如何判断是不是 可显字符int isprint(int c)若可显示返回1,否则0:要包含头文件ctype.h
随机推荐
- k8s 运行单实例 mysql
配置文件mysql.yaml --- apiVersion: v1 kind: Service metadata: name: mysql-01 spec: ports: - port: 3306 s ...
- 0702-计算机视觉工具包torchvision
0702-计算机视觉工具包torchvision 目录 一.torchvision 概述 二.通过 torchvision 加载模型 三.通过 torchvision 加载并处理数据集 四.通过 to ...
- mooc人大单元测试3
@font-face { font-family: Wingdings } @font-face { font-family: 宋体 } @font-face { font-family: " ...
- Vue.js小案例、生命周期函数及axios的使用
一.调色框小案例: 随着三个滑动框的变化,颜色框的颜色随之改变 1.1.实例代码 <!DOCTYPE html> <html lang="en" xmlns:v- ...
- 用Qt(C++)实现如苹果般的亮屏效果
用Qt(C++)实现如苹果般的亮屏效果 苹果的亮屏效果可能有很多人没注意到,和其他大部分手机或电脑不同的是,苹果的亮屏特效不是简单的亮度变化,而是一个渐亮的过程.详细来说就是,图片中较亮的部分先显示出 ...
- 1090 Highest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- <JVM下篇:性能监控与调优篇>补充:浅堆深堆与内存泄露
笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...
- IntelliJ IDEA中项目的包和目录结构显示设置
在新建的目录下再创建一个新目录时,出现了目录没有分层显示,这样使用起来十分不方便. 解决方案:点击右上方的设置按钮,将"compact middle packages"取消勾选,实 ...
- Web端直传数据至OSS
官方文档 最佳实践 小程序直传实践 支付宝小程序直传实践 微信小程序直传实践 Web端PostObject直传实践 Web端PostObject直传实践简介 JavaScript客户端签名直传 服务端 ...
- git中一些常见问题的解决
1. 解决: 先pull,执行git pull origin 分支名称:然后再执行 git push origin 分支名称 2.git报remote HTTP Basic Access denied ...