1

To be specific I do not want date +%z or date -R for the current local time, but I need something very similar that should work for any date in the past. For example, something like:

command 2018-04-01 12:33:45

should return the UTC offset (assume Chicago as my local time zone) at 12:33:45 on 1st April 2018 local time, Chicago time for example.

I searched extensively and there, probably, is no question close to this one, everyone wants current offset not date-time specific one, therefore it is not a duplicate.

Thanks very much

Update:

I have found something here, that asnwers how to get past dates using date command, then I have combined it with -R to get something close to what I want:

date -d "35 days ago" -R

I can go 35 days back and get the UTC offset.

quanta
  • 215
  • 3
  • 14

2 Answers2

1

To convert a particular date time to UTC you can use below command

for example date you provided in your question

2018-04-01 12:33:45

It would be something like below

date -u --date=@$(date "+%s" --date="2018-04-01 12:33:45")

which would have output similar to Sun Apr 1 10:33:45 UTC 2018

If you would like to achieve command date here then you can either create a command alias for above command or use above command in your script providing the date value to convert as an argument

Usman Malik
  • 103
  • 7
0

I am answering my own question based on

  1. My own update just after submitting the question.
  2. Nahuel Fouilleul's comment.

Quoting from my own "edit":

"I have found something here, that answers how to get past dates using date command, then I have combined it with -R to get something close to what I want:

date -d "35 days ago" -R

I can go 35 days back and get the UTC offset."

here comes Nahuel's comment, "what about date -d 2018-04-01T12:33:45 -R"

That is in line with what I wrote in my "edit". Nahuel's solution works perfectly for me.

also Usman Malik's answer perfectly provides the solution.

So to summarize, my (not entirely though) answer will be:

date -d specific_past_date -R

where the specific_past_date is the date on which I need the UTC offset, using the date and time that I mentioned in my question, if I do:

date -d 2018-04-01T12:33:45 -R

I get Sun, 01 Apr 2018 12:33:45 -0500 from Chicago, that means Chicago time was 5 hours behind UTC on 1st April 2018 at 12:33:45.

quanta
  • 215
  • 3
  • 14