- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据英国夏令时 (BST)/夏令时 (DST) ( https://www.gov.uk/when-do-the-clocks-change ) 时钟规则:
2019 年,民用本地时间变更发生在 3 月 31 日和 10 月 27 日,但每年的日子都会略有变化。
类似的 DST 规则适用于欧洲中部时间 CET(冬季)> CEST(夏季),检查三月/十月的最后一个星期日 ( https://www.timeanddate.com/time/change/denmark/copenhagen )。这些 BST/GMT 和 CET/CEST 规则的组合会影响例如北海周边的所有国家。无论 BST/GMT,还是 CET/CEST,下面的 UTC 时间戳应该是相同的。
我已经根据提供 BST/GMT 日期的 time.UTC
编写了以下代码,但我想知道是否有更简单/更通用的方法来使用任意 time.Location
可能适用于 CET/CEST 和(理想情况下)任何 DST 规则。
time.FixedZone
应该处理不变的时区,但是其中的 offset
是多少?如何使以下函数不受时区影响?for
循环? map
,以找出下一个时钟更改?代码:
func beginOfMonth(year, month int, loc *time.Location) time.Time {
return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
}
// https://www.gov.uk/when-do-the-clocks-change
func lastUTCSunday(year, month int) time.Time {
beginOfMonth := beginOfMonth(year, month, time.UTC)
// we can find max 5 sundays in a month
sundays := make([]time.Time, 5)
for d := 1; d <= 31; d++ {
currDay := beginOfMonth.Add(time.Duration(24*d) * time.Hour)
if currDay.Weekday().String() == "Sunday" {
sundays = append(sundays, currDay)
}
if currDay.Month() != beginOfMonth.Month() {
break
}
}
// check if last date is same month
if sundays[len(sundays)-1].Month() == beginOfMonth.Month() {
// the 5th sunday
return sundays[len(sundays)-1]
}
// if not like before, then we only have 4 Sundays
return sundays[len(sundays)-2]
}
// https://www.gov.uk/when-do-the-clocks-change
func MarchClockSwitchTime(year int) time.Time {
lastSunday := lastUTCSunday(year, int(time.March)) // month: 3
return time.Date(
year, lastSunday.Month(), lastSunday.Day(),
1, 0, 0, 0, // 1:00 AM
lastSunday.Location(),
)
}
// https://www.gov.uk/when-do-the-clocks-change
func OctoberClockSwitchTime(year int) time.Time {
lastSunday := lastUTCSunday(year, int(time.October)) // month: 10
return time.Date(
year, lastSunday.Month(), lastSunday.Day(),
2, 0, 0, 0, // 2:00 AM
lastSunday.Location(),
)
}
我还使用 GoConvey 编写了一些测试,这些测试应该验证这些基于星期日的奇怪的夏令时 (DST) 规则,但它们仅适用于 2019 年、2020 年。最好找到一种方法使此代码更通用。
func TestLastSunday(t *testing.T) {
Convey("Should find the last UTC Sunday of each month\n\n", t, func() {
for year := 2019; year <= 2020; year++ {
for month := 1; month <= 12; month++ {
lastUtcSunday := lastUTCSunday(year, month)
So(lastUtcSunday.Month(), ShouldEqual, time.Month(month))
So(lastUtcSunday.Weekday().String(), ShouldEqual, "Sunday")
So(lastUtcSunday.Year(), ShouldEqual, year)
So(lastUtcSunday.Day(), ShouldBeGreaterThanOrEqualTo, 28-7)
}
}
})
}
// https://www.gov.uk/when-do-the-clocks-change
func TestClockChange(t *testing.T) {
Convey("Should find the last UTC Sunday for the March switch\n\n", t, func() {
switch2019 := MarchClockSwitchTime(2019)
So(switch2019.Month(), ShouldEqual, time.March)
So(switch2019.Weekday().String(), ShouldEqual, "Sunday")
So(switch2019.Day(), ShouldEqual, 31)
So(switch2019.Location().String(), ShouldEqual, "UTC")
So(switch2019.Location().String(), ShouldEqual, time.UTC.String())
switch2020 := MarchClockSwitchTime(2020)
So(switch2020.Month(), ShouldEqual, time.March)
So(switch2020.Weekday().String(), ShouldEqual, "Sunday")
So(switch2020.Day(), ShouldEqual, 29)
So(switch2020.Location().String(), ShouldEqual, "UTC")
So(switch2020.Location().String(), ShouldEqual, time.UTC.String())
})
Convey("Should find the last UTC Sunday for the October switch\n\n", t, func() {
switch2019 := OctoberClockSwitchTime(2019)
So(switch2019.Month(), ShouldEqual, time.October)
So(switch2019.Weekday().String(), ShouldEqual, "Sunday")
So(switch2019.Day(), ShouldEqual, 27)
So(switch2019.Location().String(), ShouldEqual, "UTC")
So(switch2019.Location().String(), ShouldEqual, time.UTC.String())
switch2020 := OctoberClockSwitchTime(2020)
So(switch2020.Month(), ShouldEqual, time.October)
So(switch2020.Weekday().String(), ShouldEqual, "Sunday")
So(switch2020.Day(), ShouldEqual, 25)
So(switch2020.Location().String(), ShouldEqual, "UTC")
So(switch2020.Location().String(), ShouldEqual, time.UTC.String())
})
}
最佳答案
Go 已经满了 IANA TZDB支持通过time.LoadLocation
。英国使用 Europe/London
,丹麦的 CET/CEST 使用 Europe/Copenhagen
,等等。请参阅 the list here .
您不应该自己重新实现时区逻辑。正如汤姆·斯科特在The Problem with Time & Timezones中恰本地调侃的那样- “那是疯狂。”
关于go - 按时区查找下一个夏令时日期(BST/GMT、CET/CEST 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58489477/
我已经编写了一个 BSTLink 类来将 BST 转换为双向链表,但是在我尝试通过引用传递 BST 的节点指针的类中的构造调用抛出一个错误“没有匹配的函数来调用 BSTLink::construct(
当我使用 xgboost 为 2-cates classification problem 训练我的数据时,我想使用提前停止来获得最佳模型,但我对在我的预测中使用哪一个感到困惑,因为提前停止将返回 3
因为我找不到有用的东西所以我在这里问我的问题: 我们如何在不使用任何额外空间的情况下将 BST 转换为中序链接列表,然后再转换回“相同”的 BST。 到目前为止我已经尝试过的(虽然仍在做):我尝试了
我从我们的教授讲座幻灯片中获得了 BST(二叉搜索树)和随机 BST 的源代码,现在我想通过插入新元素来测试它们是否正常工作,然后我如何才能看到我的结果,如 http://cs.lmu.edu/~ra
给定两棵二叉树 T1 和 T2,您必须找到要在 T1 中完成的最少插入次数,以使其在结构上与 T2 相同。如果不可能则返回 -1。 注意事项 假设插入在 BST 中以正常方式完成。 假设在插入时,如果
我有一个始终将日期存储为 UTC 的网络应用程序,但它们需要分别以 GMT/BST 的形式显示给用户。 我有一个 UTC/GMT 日期(2013 年 3 月 30 日 22:00),我每小时移动一次以
题目地址:https://leetcode-cn.com/problems/largest-bst-subtree/ 题目描述 Given a binary tree, find the larg
注意:BST - 二叉搜索树(缩写) 正如标题所说,这是一项家庭作业,所以我不是在寻找答案。相反,我只需要一个正确方向的点。 对于作业,我应该创建一个 BST 类,它直接定义为最多包含 2 个 BST
我有一个插入方法和一个搜索方法,我正在考虑一种方法来循环二叉搜索树并使用像获取节点这样的方法,然后在另一个二叉搜索树上搜索它,如果它成立,那么我将其插入该元素,但问题是我无法想出一种基于索引获取节点的
function validateBst(root, min=-Infinity, max=Infinity) { if (!root) return true; if (root.value
我有一个问题,我知道如何像这样计算树中的所有节点 return 1 + rightchild(tree->right) + rightchild(tree->left); 现在我的问题是如果一个节点在
给定一个二叉树根,任务是返回 任何子树的所有键的最大总和 这也是 二叉搜索树 (BST) . 假设 BST 定义如下: - 节点的左子树仅包含键小于节点键的节点。 - 节点的右子树仅包含键大于节点键的
我正在尝试使用 ScalaCheck 为 BST 创建一个 Gen,但是当我调用 .sample 方法时,它给了我 java.lang.NullPointerException。我哪里错了? seal
这些是我的领域: public class BSTSet extends AbstractSet { // Data fields private BSTNode root;
我需要在二叉搜索树中执行范围搜索功能,这将给出给定范围内的项目数量。我不明白如何在找到项目时增加计数值。因为,我必须使用递归函数&如果我在递归过程中将计数变量初始化为0,它将始终从0开始计数值,而不是
所以我正在尝试编写一个代码来返回二叉搜索树中的最小值。我知道这是树的最左边的值,并且我知道我需要它递归地向左运行,直到什么都没有为止。但是我的代码不起作用,我不知道为什么。任何帮助将不胜感激。 (de
我想返回一个包含树中所有键的字符串,按照它们的存储顺序。每个子树中的键应包含在括号中。 _7_ / \ _3_ 8 / \ 1
尝试运行递归算法来找出某棵树是否是 BST(二叉搜索树)。 boolean checkBST(Node root) { boolean isBST = true; if(root ==
使用下面的代码,每个时区都正确打印值,除了 BST import java.text.*; def format = "yyyy-MM-dd HH:mm:ssXXX" def dt = new Da
您能帮忙搜索功能吗,它总是返回nil,我不明白为什么 func BTreeSearchItem(root *TreeNode, elem string) *TreeNode { if root
我是一名优秀的程序员,十分优秀!