简体   繁体   English

编年史队列:将循环整数转换为时间戳,反之亦然

[英]Chronicle Queue: Convert cycle integer to timestamp and vice-versa

Is there a way to convert between a certain cycle in Chronicle Queue to a timestamp?有没有办法将 Chronicle Queue 中的某个周期转换为时间戳? I checked the 4.5 apidocs and didn't find anything.我检查了 4.5 apidocs 并没有找到任何东西。

The cycle number is the number of days (or hours) since epoch.周期数是自纪元以来的天数(或小时数)。 If your epoch is 0 then the time stamp is如果您的纪元为 0,则时间戳为

Date date = new Date(TimeUnit.DAYS.toMillis(cycle)); 

You can do the reverse with你可以做相反的事情

long cycle = TimeUnit.MILLIS(System.currentTimeMillis()).toDays()

If you have an hourly cycle, you can replace days with hours above.如果你有一个小时周期,你可以用上面的小时替换几天。

Using built in functions you can do this for any roll cycle.使用内置函数,您可以为任何滚动循环执行此操作。

int cycle = rollCycle.current(() -> time, epoch);

To answer your question in 1 word, "no" its not possible, However its worth being aware that.用 1 个词回答您的问题,“不”是不可能的,但是值得意识到这一点。 If you are using the default, which is daily rolling, The chronicle queue will create a new queue file for its data each day.如果您使用默认值,即每日滚动,则编年史队列将每天为其数据创建一个新的队列文件。 The cycle number is directly correlated to the day another words which file ( but not the time ).周期数与另一个词哪个文件(但不是时间)的日期直接相关。 Note: The calculation for working out the day from the cycle number has to take into account the EPOCH time thats set up in chronicle queue.注意:从周期数计算出当天的计算必须考虑在编年史队列中设置的 EPOCH 时间。 If this level of granularity is sufficient ( in other words you want which day but not the time during that day), then Peter's post above tells you how to get the day from the cycle number.如果这种粒度级别足够(换句话说,您想要哪一天而不是当天的时间),那么上面彼得的帖子会告诉您如何从周期数中获取日期。 There are other ways to find out when the entry was written that does not use the cycle number.还有其他方法可以找出不使用循环编号的条目何时写入。 Let me know if you would like me to cover these other ways.如果您希望我介绍这些其他方式,请告诉我。

Given a cycle number you can do something like this (Kotlin):给定一个循环数,您可以执行以下操作(Kotlin):

fun rollCycleToTimestamp(cycle: Long) = Instant.ofEpochMilli(rollingCycle.lengthInMillis() * cycle)

Each rolling cycle type has it's own 'lengthInMillis', so if you'll multiply this by the cycle number you'll get the epoch time in millis format.每个滚动周期类型都有自己的“lengthInMillis”,因此如果您将其乘以周期数,您将获得毫秒格式的纪元时间。

This is also a generic solution for any rolling cycle type.这也是适用于任何滚动循环类型的通用解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM