1

I have a timestamp that saves a user's last access information like

Time => 14:15:05 PM

Day => 08/23/2023

Based on this saved information, I would like to write a command that returns how much time has passed compared to the time at now 15:15:14 PM on 8/23/2023 as the format below

=> 30 seconds ago

=> 12 minutes ago

=> 4 hours ago

=> 11 days ago

=> 1 year ago

what I've tried so far

date -d '7:00:00 AM - 15 minutes' '+%H:%M'

but this only returns 15 minutes ago based on the prescribed time of 7:00:00 AM an already defined time and decreasing its value to 15 minutes ago.

What I want is to calculate based on the actual time of now (23/08/2023 15:15:14 PM) and say how much time has passed since 14:15:05 on 23/08/2023 or the day before 22/08/2023.

Luana
  • 19
  • 5
  • 1
    You get the epoch seconds of a timestamp: `past=$(date -d "..." "+%s")` and the current epoch seconds `now=$(date "+%s")`, and do some arithmetic to get the difference. – glenn jackman Jul 23 '23 at 22:10
  • thank you for your response but it is still not what I am wanting, I need him to say with baae at the time save and with the local tempo do carimbo hora/dia of now at the moment how much time has passed in minutes hours or days or even years as I mentioned in the examples like 12 minutes ago etc ... – Luana Jul 23 '23 at 22:19
  • I don't understand your requirements. You will need to give more examples. Show all the input parameters and the desired output. – glenn jackman Jul 23 '23 at 22:28
  • @gleen would be the 14:15:05 time stamp that is saved in an hour folder.txt and then calculate how much time has already passed compared to the time now of the result of the hour folder.txt and reporting 15 minutes ago or 2 hours ago based on the actual time now. – Luana Jul 24 '23 at 04:55
  • sorry I understand English but I wrote in another language haha – Luana Jul 24 '23 at 04:55
  • Duplicate question. This was already answered [here](https://stackoverflow.com/a/27442175/2172543) – Marcel Jul 24 '23 at 14:45

1 Answers1

1

The date command cannot calculate durations.

It's essentially what I commented before:

  1. you have timestamp one: ts1=$( date -d "08/23/2023 14:15:05" "+%s" )

  2. you get a second timestamp, however you want ts2=$( date -d "now - 15 minutes" "+%s" )

  3. calculate the differences in different ways:

    $ printf '%(%F %T)T minus %(%F %T)T is %d %s\n' "$ts1" "$ts2" "$((ts1-ts2))" seconds
    2023-08-23 14:15:05 minus 2023-07-24 19:07:25 is 2574460 seconds
    
    $ printf '%(%F %T)T minus %(%F %T)T is %d %s\n' "$ts1" "$ts2" "$(( (ts1-ts2) / 60 ))" minutes
    2023-08-23 14:15:05 minus 2023-07-24 19:07:25 is 42907 minutes
    
    $ printf '%(%F %T)T minus %(%F %T)T is %d %s\n' "$ts1" "$ts2" "$(( (ts1-ts2) / (60*60) ))" hours
    2023-08-23 14:15:05 minus 2023-07-24 19:07:25 is 715 hours
    
    $ printf '%(%F %T)T minus %(%F %T)T is %d %s\n' "$ts1" "$ts2" "$(( (ts1-ts2) / (60*60*24) ))" days
    2023-08-23 14:15:05 minus 2023-07-24 19:07:25 is 29 days
    

I think our misunderstanding is how to accomplish step 2.

If you don't want to do it manually, lots of languages have date modules that can do durations: perl, python

glenn jackman
  • 4,630
  • 1
  • 17
  • 20