获取一天中的时间段(小时/半小时/刻钟)

发布于 2025-01-14 06:24:46 字数 818 浏览 0 评论 0原文

有什么办法可以每小时和一天中的其他时段下载吗? 我指的是列表形式的结果。

[
"00:00", // start of the day e.q 14.03
"01:00",
"02:00",
....
"23:00",
"00:00  // end of the day e.q 14.03
]

[
"00:00", // start of the day e.q 14.03
"00:30",
"01:00"
"01:30"
....
"00:00  // end of the day e.q 14.03
]

14.03

[
"00:00", // start of the day e.q 14.03
"00:15",
"00:30"
"00:45"
"01:00"
....
"00:00  // end of the day e.q 14.03
]

出于示例目的

表示 3 月 14 日。当然,可以手动添加它,但这不是一个特别优雅的解决方案。是否可以在不明确声明常量作为每小时值的情况下做到这一点? 最好的解决方案是不使用循环和 if else 结构的函数。

我还没有找到这种解决方案的实现。我自己也花了一个小时在这上面但没有成功。我需要它来实现一个功能,从这样的 eq 小时列表中创建一个 Map 或一个对列表:

[
"00:00 - 01:00",
"01:00 - 02:00",
"02:00 - 03:00 "
//.......
"23:00 - 00:00"
]

有人有机会实现这样的问题并且能够提供帮助吗?

Is there any way to download every hour and another period of the day?
I mean exactly the result in the form of a list.

[
"00:00", // start of the day e.q 14.03
"01:00",
"02:00",
....
"23:00",
"00:00  // end of the day e.q 14.03
]

and

[
"00:00", // start of the day e.q 14.03
"00:30",
"01:00"
"01:30"
....
"00:00  // end of the day e.q 14.03
]

and

[
"00:00", // start of the day e.q 14.03
"00:15",
"00:30"
"00:45"
"01:00"
....
"00:00  // end of the day e.q 14.03
]

14.03 means March 14 for the sake of the example.

Of course it is possible to add this manually, but it would not be a particularly elegant solution. Is it possible to do it without explicitly declaring constants as values of each hour ?
The best solution would be a function without using loops and if else constructions.

I have not yet been able to find an implementation of such a solution. I myself also spend another hour on this without success. I need it to implement a functionality that creates a Map or a list of pairs from a list of such e.q hours:

[
"00:00 - 01:00",
"01:00 - 02:00",
"02:00 - 03:00 "
//.......
"23:00 - 00:00"
]

Has anyone had occasion to implement such a problem and would be able to help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2025-01-21 06:24:46

您可以编写一个函数来生成 Sequence,如下所示:

fun generateTimesOfDay(interval: java.time.Duration) = sequence {
    if (interval > java.time.Duration.ofDays(1)) {
        repeat(2) { yield(LocalTime.MIDNIGHT) }
        return@sequence
    }
    var time = LocalTime.MIDNIGHT
    while (true) {
        yield(time)
        val newTime = time + interval
        if (newTime > time) {
            time = newTime
        } else {
            break // passed or reached midnight
        }
    }
    yield(LocalTime.MIDNIGHT) // Midnight the next day to end last period
}

然后您就拥有了可以与 toString().format( ) 和一些 DateTimeFormatter 来按照您喜欢的方式格式化它们。您可以使用 zipWithNext() 来创建时间范围。

val x = generateTimesOfDay(java.time.Duration.ofMinutes(15))
println(x.toList())
println(
    x.zipWithNext { a, b -> "$a - $b" }.toList()
)

请注意,我使用完全限定的 java.time.Duration 来避免与 Kotlin 标准库 Duration 类发生冲突。

You could write a function to generate a Sequence<LocalTime> like this:

fun generateTimesOfDay(interval: java.time.Duration) = sequence {
    if (interval > java.time.Duration.ofDays(1)) {
        repeat(2) { yield(LocalTime.MIDNIGHT) }
        return@sequence
    }
    var time = LocalTime.MIDNIGHT
    while (true) {
        yield(time)
        val newTime = time + interval
        if (newTime > time) {
            time = newTime
        } else {
            break // passed or reached midnight
        }
    }
    yield(LocalTime.MIDNIGHT) // Midnight the next day to end last period
}

Then you have all the LocalTimes you can use with toString() or with .format() and some DateTimeFormatter to format them how you like. You can use zipWithNext() to create the time ranges.

val x = generateTimesOfDay(java.time.Duration.ofMinutes(15))
println(x.toList())
println(
    x.zipWithNext { a, b -> "$a - $b" }.toList()
)

Note, I'm using fully qualified java.time.Duration to avoid conflicts with the Kotlin standard library Duration class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文