AnswerBun.com

Stumped by this date behavior

Super User Asked by Thomas Anderson on December 22, 2020

$ date --version
date (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.

Current date in US Eastern EDT.

$ date
Tue Oct 27 21:16:35 EDT 2020

Now try to convert to UTC, which is +4.

$ env TZ=UTC date -d "21:07:28 EDT"
Thu Oct 29 01:07:28 UTC 2020

$ env TZ=Etc/UTC date --date='TZ="America/New_York" 21:07:28'
Thu Oct 29 01:07:28 UTC 2020

Why is it Oct 29? Should be Oct 28.

I am totally stumped by this.

Thanks for any help.

One Answer

If you provide only a time to the -d option of date, it interprets that as the provided time on today's date. With that in mind, let's look at your example:

$ env TZ=UTC date -d "21:07:28 EDT"
Thu Oct 29 01:07:28 UTC 2020

You're telling date to print out the string representation of 21:07:28 EDT on the current date. You're also setting TZ=UTC. TZ is used not only to format the time, but also to figure out the current date.

When you ran that command, it was approximately 1am on October 28th in UTC. So your date command translates to "print the UTC time when it's Oct 28 21:07:28 EDT". And because UTC is four hours ahead of EDT, it prints Oct 29 01:07:28.

If you want it to use a different date than "today" in the timezone specified by $TZ, then you have to be explicit. E.g.,

TZ=UTC date -d "Oct 27 21:07:28 EDT"

Or you could use a second invocation of the date command to generate today's date in your local timezone:

TZ=UTC date -d "$(date +%F) 21:07:28 EDT"

Note that your shell does not set TZ when calling date for command substitution (the $(date +%F) part). Which is why that inner date gives you the date in your local timezone.

Correct answer by satwell on December 22, 2020

Add your own answers!

Related Questions

Clonezilla not enough free memory

2  Asked on December 16, 2021 by furjoza

 

An operating system wasn’t found

4  Asked on December 16, 2021

   

Sorting XML having xsd format using xslt

1  Asked on December 16, 2021 by harshi

   

Lower FPS than expected on laptop

1  Asked on December 16, 2021 by bobleeswagger

       

VLC Player Mac View Playlist Keyboard Shortcut

1  Asked on December 16, 2021 by operation420-net

     

Weird Windows 10 Update bug/glitch

0  Asked on December 16, 2021 by rakib-hasan

   

Virtual box shared folder is not mounted in guest

1  Asked on December 13, 2021 by whitecolor

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP