2

I am using embedded Cassandra 3.11.7 with springboot.

I live in South Korea which uses KST time which is UTC + 9.

So I'm looking for a way to set Cassandra timezone to kst or UTC+9.

When inserting the actual Cassandra, it is stored as utc that is -9 hours longer than the current time, so it is not possible to get an accurate value when looking up the date.

So I'm looking for a way to set the Cassandra timezone.

I have a cassandra configuration and webFlux.

@Configuration
@EnableReactiveCassandraRepositories
@PropertySource(value = "file:${sf1.manager.path}/conf/cassandra.properties", ignoreResourceNotFound = true)
public class CassandraConfig extends AbstractReactiveCassandraConfiguration  {}

thank you.

Aaron
  • 55,518
  • 11
  • 116
  • 132
orreo
  • 31
  • 3

2 Answers2

2

Caveat: I’m not a Cassandra user.


According to the documentation, Cassandra stores time stamps in UTC.

There should be no need to set the default time zone of the Cassandra server or its JVM. Simply exchange values in UTC, having an offset of zero hours-minutes-seconds. Use the java.time classes only, never the terrible legacy classes.

Capture the current moment in UTC.

Instant instant = Instant.now() ;

Generate text in standard ISO 8601 format. The Z on the end means an offset of zero hours-minutes-seconds from UTC, and is pronounced “Zulu”.

String s = instant.toString() ;

Parse such a string.

Instant instant = Instant.parse( s ) ;

If using JDBC, convert to the OffsetDateTime class.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
myPreparedStatement.setObject( … , odt ) ;

Retrieval.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

Adjust into a time zone to view that moment in the wall-clock time of your region.

The 2-4 letter codes such as KST, CST, IST are not actual time zones. They hint at the time zone, and indicate if Daylight Savings Time (DST) is in effect or not. These pseudo-zones are not standardized, and are not unique! Use these only for localized presentation to the user, never for data storage nor data exchange.

Real time zones are named in format of Continent/Region such as Asia/Seoul.

ZoneId z = ZoneId.of( "Asia/Seoul" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

Generate strings, automatically localized using DateTimeFormatter.ofLocalizedDateTime.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

The user asked the same question on https://community.datastax.com/questions/13762/ so I'm re-posting my answer here.


Cassandra does not store timezone information. Date and time data types are encoded independent of the timezone of the server.

For example:

  • The CQL date type is a 32-bit integer representing the number of days since Unix epoch (January 1, 1970).
  • The CQL time type is a 64-bit signed integer representing the number of nanoseconds since midnight GMT.
  • The CQL timestamp type is a 64-bit signed integer representing the number of milliseconds Unix epoch at 00:00 GMT.

Clients are responsible for displaying the data in the format required. For example, when you retrieve data using cqlsh, the data is displayed in UTC by default. To display date/time in a different timezone, cqlsh needs to be configured with a different timezone.

In your case, you will need to do the formatting/conversion within your application when displaying the data. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23