Room SQLITE 查询减去长时间戳

Room SQLITE query subtracting long timestamps

我有一个 table,它有两列:'TimeStart' 和 'TimeFinish' 这些列存储一个长值,该值是使用类型类转换器从日期格式转换而来的。

我需要对 return table 中的所有行编写查询,但我需要计算总时间。因此,我需要减去 Timestart 和 TimeFinish 中的 long 值以接收每一行花费的个人时间,然后将它们加起来。我想 return 一个以小时为单位的值,以便用户查看他们在活动中花费了多少小时。我不确定执行此操作的最佳方法是什么。有人可以帮我写一个具有上述功能的查询吗?

TABLE

@Entity(tableName = "tableCheckIn")
public class TableCheckIn {

@PrimaryKey(autoGenerate = true)
private long pkCheckInId;

@ColumnInfo(name = "userId")
private long fkUserId;

@ColumnInfo(name = "moodStart")
private long moodStart;

@ColumnInfo(name = "checkInChallenge")
private String checkInChallenge;

@ColumnInfo(name = "checkInGratitude")
private String checkInGratitude;

@ColumnInfo(name = "workoutStart")
@TypeConverters({TimeTypeConverter.class})
private Date workoutStart;

@ColumnInfo(name = "workoutEnd")
@TypeConverters({TimeTypeConverter.class})
private Date workoutEnd;

日期转换器

public class TimeTypeConverter {

@TypeConverter
public static Long toDate(Date date){
    return date == null ? null : date.getTime();
}

@TypeConverter
public static Date toLong(Long timestamp){
    return timestamp == null ? null : new Date(timestamp);
}

DAO

 @Query("SELECT (SUM(workoutEnd - workoutStart) / 3600) as totalWorkouts FROM tableCheckIn WHERE userId = :pkUserId")
LiveData<List<ModelStatsTotalWorkoutTime>> totalWorkoutTime(long pkUserId);

回购

public LiveData<List<ModelStatsTotalWorkoutTime>> totalWorkoutTime(long userId) {
    return feelingFitDatabase.getUsersDao().totalWorkoutTime(userId);
}

视图模型

  public LiveData<List<ModelStatsTotalWorkoutTime>> totalWorkoutTime(long userId) {
    return repoUsers.totalWorkoutTime(userId);
}

片段

  mViewModelUsers.totalWorkoutTime(pkUserId).observe(getViewLifecycleOwner(), new Observer<List<ModelStatsTotalWorkoutTime>>() {
        @Override
        public void onChanged(List<ModelStatsTotalWorkoutTime> modelStatsTotalWorkouts) {

            for(ModelStatsTotalWorkoutTime list: modelStatsTotalWorkouts){
                totalWorkoutTimeSeconds += list.getTotalWorkouts();
            }

            totalWorkoutTimeMinutes = totalWorkoutTimeSeconds / 60;
            totalWorkoutTimeHours = totalWorkoutTimeMinutes / 60;

            if (totalWorkoutTimeMinutes <= 59){
                tvTotalTimeWorkingOut.setText(""+totalWorkoutTimeMinutes);
                tvTotalTimeWorkingOutDescription.setText(getString(R.string.workout_minutes));
            } else {
                tvTotalTimeWorkingOut.setText(""+totalWorkoutTimeHours);
                tvTotalTimeWorkingOutDescription.setText(getString(R.string.workout_hours));
            }
        }
    });
}

TimeStartTimeFinish 包含包含毫秒的 unix 纪元时间戳。
在 SQLite 中,您可以使用以下格式获取可读格式的日期:

select 
  datetime(1596107885085 / 1000, 'unixepoch'),
  datetime(1596108413648 / 1000, 'unixepoch')

哪个returns:

2020-07-30 11:18:05     2020-07-30 11:26:53

要得到它们的分钟差,您必须除以 1000 以去除毫秒,然后除以 60:

(TimeFinish - TimeStart) / 1000 / 60

因此您可以通过以下方式获得以分钟为单位的总差异:

select sum(TimeFinish - TimeStart) / 1000 / 60 from tablename