Discussion:
Date of yesterday
(too old to reply)
Max Power
2018-04-09 05:45:05 UTC
Permalink
Raw Message
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
On Linux:
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday

Thanks for reply.
Robert Klein
2018-04-09 05:54:09 UTC
Permalink
Raw Message
Hi Max,

this works for me:

date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d


Best regards
Robert

On Mon, 09 Apr 2018 07:45:05 +0200
Post by Max Power
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday
Thanks for reply.
Philip Guenther
2018-04-09 06:12:43 UTC
Permalink
Raw Message
Post by Robert Klein
date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
Did you test that after 11pm on the day when daylight-saving time ends and
the clock is turned back, resulting in a 25 hour long day?

I would use this:
perl -MPOSIX=strftime,mktime -le '@d=localtime(); $d[3]--; mktime(@d);
print strftime("%Y_%m_%d",@d)'

Philip Guenther
Paul de Weerd
2018-04-09 08:26:30 UTC
Permalink
Raw Message
On Sun, Apr 08, 2018 at 11:12:43PM -0700, Philip Guenther wrote:
| On Sun, Apr 8, 2018 at 10:54 PM, Robert Klein <***@roklein.de> wrote:
|
| > this works for me:
| >
| > date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
| >
|
| Did you test that after 11pm on the day when daylight-saving time ends and
| the clock is turned back, resulting in a 25 hour long day?

For those special occassions there's:

date -j `date +%Y%m%d1200` +%s

Turning this into:

date -r $(($(date -j `date +%Y%m%d1200` +%s) - 86400)) +%Y_%m_%d

Less perl (and less typing) at the expense of a total of 3 invocations
of date. Although I loathe the natural language parsing options built
into Linux date(1), this sort of thing is rather convenient.

| I would use this:
| perl -MPOSIX=strftime,mktime -le '@d=localtime(); $d[3]--; mktime(@d);
| print strftime("%Y_%m_%d",@d)'
|
| Philip Guenther

Paul 'abolish DST now' de Weerd
--
++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+
+++++++++++>-]<.>++[<------------>-]<+.--------------.[-]
http://www.weirdnet.nl/
Stephane HUC "CIOTBSD"
2018-04-09 09:04:50 UTC
Permalink
Raw Message
as: date -r $(( $(date +%s) - 86400)) +%F
;)
Post by Paul de Weerd
|
| >
| > date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
| >
|
| Did you test that after 11pm on the day when daylight-saving time ends and
| the clock is turned back, resulting in a 25 hour long day?
date -j `date +%Y%m%d1200` +%s
date -r $(($(date -j `date +%Y%m%d1200` +%s) - 86400)) +%Y_%m_%d
Less perl (and less typing) at the expense of a total of 3 invocations
of date. Although I loathe the natural language parsing options built
into Linux date(1), this sort of thing is rather convenient.
|
| Philip Guenther
Paul 'abolish DST now' de Weerd
--
~ " Fully Basic System Distinguish Life! " ~ " Libre as a BSD " +=<<<
----
<me>Stephane HUC as PengouinBSD or CIOTBSD</me>
<mail>***@stephane-huc.net</mail>
Philip Guenther
2018-04-09 18:48:13 UTC
Permalink
Raw Message
Post by Stephane HUC "CIOTBSD"
as: date -r $(( $(date +%s) - 86400)) +%F
;)
...
Post by Stephane HUC "CIOTBSD"
Post by Paul de Weerd
| Did you test that after 11pm on the day when daylight-saving time ends
and
Post by Paul de Weerd
| the clock is turned back, resulting in a 25 hour long day?
<sigh>
The best part about top-posting is that you don't have to read the earlier
comments which already explained why the solution you're proposing isn't
reliable...
Stephane HUC "PengouinBSD"
2018-04-09 18:58:42 UTC
Permalink
Raw Message
get the current timestamp, subtracting 86400 seconds is not reliable to
get yesterday's date to the nearest second?
terrible!
Post by Philip Guenther
Post by Stephane HUC "CIOTBSD"
as: date -r $(( $(date +%s) - 86400)) +%F
;)
...
Post by Stephane HUC "CIOTBSD"
Post by Paul de Weerd
| Did you test that after 11pm on the day when daylight-saving time ends
and
Post by Paul de Weerd
| the clock is turned back, resulting in a 25 hour long day?
<sigh>
The best part about top-posting is that you don't have to read the earlier
comments which already explained why the solution you're proposing isn't
reliable...
--
~ " Fully Basic System Distinguish Life! " ~ " Libre as a BSD " +=<<<
----
<me>Stephane HUC as PengouinBSD or CIOTBSD</me>
<mail>***@stephane-huc.net</mail>
Philip Guenther
2018-04-09 20:34:41 UTC
Permalink
Raw Message
On Mon, Apr 9, 2018 at 11:58 AM, Stephane HUC "PengouinBSD" <
Post by Stephane HUC "PengouinBSD"
get the current timestamp, subtracting 86400 seconds is not reliable to
get yesterday's date to the nearest second?
terrible!
Yes, some days are 90000 seconds long.
Stephane HUC "PengouinBSD"
2018-04-09 20:36:52 UTC
Permalink
Raw Message
what?

please, explain-me!
Post by Philip Guenther
On Mon, Apr 9, 2018 at 11:58 AM, Stephane HUC "PengouinBSD"
get the current timestamp, subtracting 86400 seconds is not reliable to
get yesterday's date to the nearest second?
terrible!
Yes, some days are 90000 seconds long.
--
~ " Fully Basic System Distinguish Life! " ~ " Libre as a BSD " +=<<<
----
<me>Stephane HUC as PengouinBSD or CIOTBSD</me>
<mail>***@stephane-huc.net</mail>
Philip Guenther
2018-04-09 20:45:35 UTC
Permalink
Raw Message
On Mon, Apr 9, 2018 at 1:36 PM, Stephane HUC "PengouinBSD" <
Post by Stephane HUC "PengouinBSD"
what?
please, explain-me!
| Did you test that after 11pm on the day when daylight-saving time ends and
| the clock is turned back, resulting in a 25 hour long day?
On the day when daylight-saving time ends, the clock is turned back an hour
resulting in a day which is 25 hours long!

: morgaine; date -r $((1541403000))
Sun Nov 4 23:30:00 PST 2018
: morgaine; date -r $((1541403000 - 86400))
Sun Nov 4 00:30:00 PDT 2018
: morgaine;
Tom Smyth
2018-04-09 21:05:03 UTC
Permalink
Raw Message
Howdy...
Daylight savings time sucks... :/...
Is there a way to Reference UTC and then do the calculating
n and then convert to local time zone if you are worried about
calculating yesterday on the edge case of the 2 hrs a year
that this would make an impact...

as a side issue would avoiding running this particular job
between 11PM and 1am prevent the edge case from having
an impact
Peace out
Tom Smyth
Post by Philip Guenther
On Mon, Apr 9, 2018 at 1:36 PM, Stephane HUC "PengouinBSD" <
Post by Stephane HUC "PengouinBSD"
what?
please, explain-me!
| Did you test that after 11pm on the day when daylight-saving time ends
and
Post by Stephane HUC "PengouinBSD"
| the clock is turned back, resulting in a 25 hour long day?
On the day when daylight-saving time ends, the clock is turned back an hour
resulting in a day which is 25 hours long!
: morgaine; date -r $((1541403000))
Sun Nov 4 23:30:00 PST 2018
: morgaine; date -r $((1541403000 - 86400))
Sun Nov 4 00:30:00 PDT 2018
: morgaine;
--
Kindest regards,
Tom Smyth

Mobile: +353 87 6193172
The information contained in this E-mail is intended only for the
confidential use of the named recipient. If the reader of this message
is not the intended recipient or the person responsible for
delivering it to the recipient, you are hereby notified that you have
received this communication in error and that any review,
dissemination or copying of this communication is strictly prohibited.
If you have received this in error, please notify the sender
immediately by telephone at the number above and erase the message
You are requested to carry out your own virus check before
opening any attachment.
Philip Guenther
2018-04-09 21:18:56 UTC
Permalink
Raw Message
Post by Tom Smyth
Howdy...
Daylight savings time sucks... :/...
Is there a way to Reference UTC and then do the calculating
n and then convert to local time zone if you are worried about
calculating yesterday on the edge case of the 2 hrs a year
that this would make an impact...
What...why...why are you making this harder than it has to be?

mktime/localtime let you perform calculations based on time broken down
into years/months/days/etc. "Yesterday" is a calculation based on days, so
do it in that form. Those APIs exist to let you do that! That's what the
solution I provided in my original reply does!
Post by Tom Smyth
as a side issue would avoiding running this particular job
between 11PM and 1am prevent the edge case from having
an impact
So you know how to do the Right Thing, but you're going to instead leave a
bug for you or your successor to hit months or years away? Please do not
do that on any system that others might depend on.


Philip Guenther
Allan Streib
2018-04-09 21:20:15 UTC
Permalink
Raw Message
Post by Tom Smyth
Howdy...
Daylight savings time sucks... :/...
Is there a way to Reference UTC and then do the calculating
n and then convert to local time zone if you are worried about
calculating yesterday on the edge case of the 2 hrs a year
that this would make an impact...
Yes, use the perl example someone suggested hours ago.

You don't want to mess with this stuff yourself. Use a proper date
libarary to do date calculations.

Allan
Stephane HUC "PengouinBSD"
2018-04-09 20:58:41 UTC
Permalink
Raw Message
Excuse-me, but i dont really understand this!
(perhaps, because it's in english).
Post by Philip Guenther
On Mon, Apr 9, 2018 at 1:36 PM, Stephane HUC "PengouinBSD" <
Post by Stephane HUC "PengouinBSD"
what?
please, explain-me!
| Did you test that after 11pm on the day when daylight-saving time ends
and
Post by Stephane HUC "PengouinBSD"
| the clock is turned back, resulting in a 25 hour long day?
On the day when daylight-saving time ends, the clock is turned back an hour
resulting in a day which is 25 hours long!
: morgaine; date -r $((1541403000))
Sun Nov 4 23:30:00 PST 2018
: morgaine; date -r $((1541403000 - 86400))
Sun Nov 4 00:30:00 PDT 2018
: morgaine;
--
~ " Fully Basic System Distinguish Life! " ~ " Libre as a BSD " +=<<<
----
<me>Stephane HUC as PengouinBSD or CIOTBSD</me>
<mail>***@stephane-huc.net</mail>
Jordan Geoghegan
2018-04-09 23:13:58 UTC
Permalink
Raw Message
Post by Stephane HUC "PengouinBSD"
Excuse-me, but i dont really understand this!
(perhaps, because it's in english).
If you don't understand how Daylight Savings time works, or how time
works on computers in general, then you should just trust the expert
advice offered to you by Philip- he even went to the trouble of
explaining it to you several different ways; and Daniel linked to all
the information you could ever need regarding Daylight Savings.
Daniel Ouellet
2018-04-09 20:52:53 UTC
Permalink
Raw Message
Here to confuse you even more, there is time zone that have 30 minutes
and even 45 minutes differences.

https://www.timeanddate.com/time/time-zones-interesting.html

Have fun.
Post by Stephane HUC "PengouinBSD"
what?
please, explain-me!
EDT EST for example.
Some days are even 82800 long.
Some time zone even have 1/2 hour if these still exists, so the would be
84600 or 88200.
Michael Lambert
2018-04-09 20:43:55 UTC
Permalink
Raw Message
Post by Philip Guenther
On Mon, Apr 9, 2018 at 11:58 AM, Stephane HUC "PengouinBSD" <
Post by Stephane HUC "PengouinBSD"
get the current timestamp, subtracting 86400 seconds is not reliable to
get yesterday's date to the nearest second?
terrible!
Yes, some days are 90000 seconds long.
But those make up for the days that are only 82800 seconds...
Martin Schröder
2018-04-09 21:31:12 UTC
Permalink
Raw Message
Post by Stephane HUC "PengouinBSD"
get the current timestamp, subtracting 86400 seconds is not reliable to
get yesterday's date to the nearest second?
Did they teach leap seconds in your school yet?

Best
Martin
Stephane HUC "PengouinBSD"
2018-04-10 12:12:49 UTC
Permalink
Raw Message
this will be my final answer - and which certainly does not want to be a
polemic:

No, at school I wasn't told about an'leap second', an intercalar second!
But, I only went in third - some here will be able to tell you what this
level is, personally I don't know the correspondence, with other
cultures, but it's pretty low... then I became "computer self-taught",
until I could do a developer training (Java, SQL, Web) where even there,
we weren't spoken to.
My wife, who has a master's degree in history, just learned today by me
what it is!

I had no idea it was so complex!

I thank those who responded decently, such as :

- Daniel Ouellet for his link on "timezones" where I learned that some
had a duration of 45, even 30".
- Tom Smyth, with his reference to summer time - which I did not
understand in Philip Guenther's initial message, nor that he had THE
right answer!
- Philip Guenther, himself, who tried to be explicit.
- and, also Theo Buethler...

I don't have to be ashamed to ask questions,
I don't have to be ashamed not to know things...
I appreciate learning, and I'm sorry I don't know so much, and not "in
control" - that is, the degree of knowledge and practice of few persons.
On the other hand, I am ashamed of some messages that came to insult me
personally... directly!
Knowledge and mastery are not necessary and useful reasons for despising
others, or those who know less!

Now, if I am such a disgrace to you, in my interventions, in my
participations, all you have to do is banish me, remove me from the
list... which would be in no way constructive, and even less positive.

i.e: I have learned some things, such as the use of CVS, thanks to the
intelligent cooperation of some here on this list... and certainly,
others...

Personally, I stay, because learning from others doesn't scare me...
That, to participate, even if I know much less than others does not
frighten me, and will never proccur me of shame!
Even if I'm wrong, in my participations!

Thanks all.
"Have a nice day"
Post by Martin Schröder
Post by Stephane HUC "PengouinBSD"
get the current timestamp, subtracting 86400 seconds is not reliable to
get yesterday's date to the nearest second?
Did they teach leap seconds in your school yet?
Best
Martin
--
~ " Fully Basic System Distinguish Life! " ~ " Libre as a BSD " +=<<<
----
<me>Stephane HUC as PengouinBSD or CIOTBSD</me>
<mail>***@stephane-huc.net</mail>
Stuart Henderson
2018-04-10 23:03:51 UTC
Permalink
Raw Message
Post by Paul de Weerd
|
| >
| > date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
| >
|
| Did you test that after 11pm on the day when daylight-saving time ends and
| the clock is turned back, resulting in a 25 hour long day?
date -j `date +%Y%m%d1200` +%s
date -r $(($(date -j `date +%Y%m%d1200` +%s) - 86400)) +%Y_%m_%d
Less perl (and less typing) at the expense of a total of 3 invocations
of date. Although I loathe the natural language parsing options built
into Linux date(1), this sort of thing is rather convenient.
|
| Philip Guenther
Paul 'abolish DST now' de Weerd
The time I usually need yesterday's date is when constructing a filename
for things like log files or mailboxes that have been rotated in a job
run overnight, often in monthly.local.

For this, I just cheat and set a timezone that is a bit behind...
`date -z US/Pacific +%Y-%m-%d`

Obviously no good if your timezone is within a couple of hours east of
the date line...

Christophe Simon
2018-04-09 08:38:11 UTC
Permalink
Raw Message
Hi,

You can use this form, which is a little simpler:

date -d 'now -1 day' '+%Y_%m_%d'

Regards

Christophe
Post by Robert Klein
Hi Max,
date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
Best regards
Robert
On Mon, 09 Apr 2018 07:45:05 +0200
Post by Max Power
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday
Thanks for reply.
Otto Moerbeek
2018-04-09 08:54:54 UTC
Permalink
Raw Message
Post by Christophe Simon
Hi,
date -d 'now -1 day' '+%Y_%m_%d'
Nope, this is a GNU extension.

-Otto
Post by Christophe Simon
Regards
Christophe
Post by Robert Klein
Hi Max,
date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
Best regards
Robert
On Mon, 09 Apr 2018 07:45:05 +0200
Post by Max Power
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday
Thanks for reply.
Christophe Simon
2018-04-09 10:50:33 UTC
Permalink
Raw Message
The command I executed was run on OpenBSD:

$ uname -a
OpenBSD XXX 6.3 GENERIC.MP#107 amd64

$ /bin/date -d 'now -1 day' '+%Y_%m_%d'
2018_04_09

$ /bin/date -d 'yesterday' '+%Y_%m_%d'
2018_04_09

I don't know when it was imported into BSD's date, but this extension is
available on OpenBSD, at least in 6.3...

Regards.

Christophe
Post by Otto Moerbeek
Post by Christophe Simon
Hi,
date -d 'now -1 day' '+%Y_%m_%d'
Nope, this is a GNU extension.
-Otto
Post by Christophe Simon
Regards
Christophe
Post by Robert Klein
Hi Max,
date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
Best regards
Robert
On Mon, 09 Apr 2018 07:45:05 +0200
Post by Max Power
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday
Thanks for reply.
Otto Moerbeek
2018-04-09 10:56:47 UTC
Permalink
Raw Message
Post by Christophe Simon
$ uname -a
OpenBSD XXX 6.3 GENERIC.MP#107 amd64
$ /bin/date -d 'now -1 day' '+%Y_%m_%d'
2018_04_09
$ /bin/date -d 'yesterday' '+%Y_%m_%d'
2018_04_09
I don't know when it was imported into BSD's date, but this extension is
available on OpenBSD, at least in 6.3...
There is a -d option, but it does not do what you expect. What
date is it now? Try other values than -1

-Otto
Post by Christophe Simon
Regards.
Christophe
Post by Otto Moerbeek
Post by Christophe Simon
Hi,
date -d 'now -1 day' '+%Y_%m_%d'
Nope, this is a GNU extension.
-Otto
Post by Christophe Simon
Regards
Christophe
Post by Robert Klein
Hi Max,
date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
Best regards
Robert
On Mon, 09 Apr 2018 07:45:05 +0200
Post by Max Power
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday
Thanks for reply.
Christophe Simon
2018-04-09 11:25:13 UTC
Permalink
Raw Message
Oh, sorry,

Thank your for having corrected me !

Regards.

Christophe
Post by Otto Moerbeek
Post by Christophe Simon
$ uname -a
OpenBSD XXX 6.3 GENERIC.MP#107 amd64
$ /bin/date -d 'now -1 day' '+%Y_%m_%d'
2018_04_09
$ /bin/date -d 'yesterday' '+%Y_%m_%d'
2018_04_09
I don't know when it was imported into BSD's date, but this extension is
available on OpenBSD, at least in 6.3...
There is a -d option, but it does not do what you expect. What
date is it now? Try other values than -1
-Otto
Post by Christophe Simon
Regards.
Christophe
Post by Otto Moerbeek
Post by Christophe Simon
Hi,
date -d 'now -1 day' '+%Y_%m_%d'
Nope, this is a GNU extension.
-Otto
Post by Christophe Simon
Regards
Christophe
Post by Robert Klein
Hi Max,
date -r $(( $(date +%s) - 1 * 24 * 60 * 60 )) +%Y_%m_%d
Best regards
Robert
On Mon, 09 Apr 2018 07:45:05 +0200
Post by Max Power
Hi guys,
How can I do to get yesterday's date?
I need for create a backup directory.
yesterday=backup_$(date -d "yesterday" '+%Y_%m_%d')
mkdir -p /raid1/backup/$yesterday
Thanks for reply.
Loading...