Skip to content

Commit

Permalink
provide time utils to get now instant with micro seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Dec 15, 2024
1 parent 8b40422 commit c1b3315
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/flowci/common/TimeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flowci.common;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public abstract class TimeUtils {

public static Instant now() {
return Instant.now().truncatedTo(ChronoUnit.MICROS);
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/flowci/common/model/EntityListener.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.flowci.common.model;

import com.flowci.common.TimeUtils;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class EntityListener {

@PrePersist
public void prePersist(Object entity) {
if (entity instanceof EntityBase e) {
e.createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
e.updatedAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
e.createdAt = TimeUtils.now();
e.updatedAt = TimeUtils.now();
}
}

@PreUpdate
public void preUpdate(Object entity) {
if (entity instanceof EntityBase e) {
e.updatedAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
e.updatedAt = TimeUtils.now();
}
}
}

0 comments on commit c1b3315

Please sign in to comment.