Score:1

Calculate seconds minutes and days ago in shell

mr flag

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.

fo flag
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.
Luana avatar
mr flag
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 ...
fo flag
I don't understand your requirements. You will need to give more examples. Show all the input parameters and the desired output.
Luana avatar
mr flag
@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 avatar
mr flag
sorry I understand English but I wrote in another language haha
Marcel avatar
gb flag
Duplicate question. This was already answered [here](https://stackoverflow.com/a/27442175/2172543)
Score:1
fo flag

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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.