vasquez
vasquez

Reputation: 581

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? I checked the 4.5 apidocs and didn't find anything.

Upvotes: 2

Views: 530

Answers (3)

Anton K
Anton K

Reputation: 21

Given a cycle number you can do something like this (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.

This is also a generic solution for any rolling cycle type.

Upvotes: 2

Rob Austin
Rob Austin

Reputation: 620

To answer your question in 1 word, "no" its not possible, However its worth being aware that. 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. 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.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533530

The cycle number is the number of days (or hours) since epoch. If your epoch is 0 then the time stamp is

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);

Upvotes: 4

Related Questions