SG_SAT_DATETIME(8)
β¨ SG_SAT_DATETIME(8) β¨
okay bestie, so this is a page from the sg3_utils manual set β sg_sat_datetime β and it's genuinely one of the cutest little utilities in the whole toolbox. It's all about asking your ATA drive what time it thinks it is, or telling it what time it actually is. Let's get into it π«Ά
πΈ NAME πΈ
sg_sat_datetime β report or set the date and time on an ATA device. That's the whole vibe, no cap.
π¦ SYNOPSIS π¦
sg_sat_datetime [--dma] [--elapsed] [--format] [--help] [--hex]
[--milliseconds=MS] [--readonly] [--seconds=SECS] [--srep] [--verbose]
[--version] DEVICE
π DESCRIPTION π
So what actually happens under the hood? This sends a SET DATE & TIME EXT command, or a READ LOG [DMA] EXT command, to the ATA DEVICE β either to set the date and time, or to read it out of the Device Statistics log at address 04h, General Statistics page (01h). Both of these commands live in the ATA Command Set (ACS-5) standard, and the ATA commands get sent using ATA PASS-THROUGH SCSI commands. Nested, but it works π
Here's the logic, and it's lowkey simple:
- If you give either
--milliseconds=MSor--seconds=SECSβ and bestie, you literally cannot give both β then the SET DATE & TIME command is sent. - Otherwise, the
READ LOG EXTcommand goes out. Or, if you passed--dma, it's theREAD LOG DMA EXTcommand instead.
Now for the fun part π The date and time get sent to and received from the DEVICE generally as the number of milliseconds since the epoch of 1970-01-01 00:00:00 UTC, stored in a 48 bit unsigned integer. That's the exact same epoch Unix machines use, except Unix usually counts seconds since then. So the Unix date command β and especially its +%s format β is your bestie for converting between timestamps and humanly readable forms. There's a whole EXAMPLES section below for this, iykyk.
Real talk though: if the date and time is not set after a power-on reset, the value is the same as the device power-on hours in milliseconds. Not a clock, just a counter. Periodt.
β¨ OPTIONS β¨
Heads up boo: arguments to long options are mandatory for short options as well.
-d,--dmaβ useREAD LOG DMA EXTto read the date and time value from the device. Ignored if you're setting the date and time (so don't stress about it then).-e,--elapsedβ assume the date and time fromREAD LOG [DMA] EXTis an elapsed time from some event like a power cycle or hard reset, and format the output as'<n> days hh:mm:ss.xxx'wherehhis hours (00to23inclusive),mmis minutes (00to59inclusive),ssis seconds (00to59inclusive) andxxxis milliseconds (000to999inclusive). If the number of days is0then'0 days'is not output β unless you give this option two or more times π. This one's especially useful if the date and time has not been set, because per ACS-5, when it's not set the date and time is initialized to the power-on hours of the device in milliseconds.-f,--formatβ output the date and time formatted using the default locale. Cute and readable π-h,--helpβ output the usage message then exit. The classic.-H,--hexβ output the date and time in ASCII hexadecimal. Raw numbers, no filter π-m,--milliseconds=MSβ where MS is the number of milliseconds since1970-01-01 00:00:00 UTCto set in theDEVICEwith the ATASET DATE & TIME EXTcommand.-R,--readonlyβ open theDEVICEread-only. The default action is to open theDEVICEread-write, jsyk.-s,--seconds=SECSβ where SECS is the number of seconds since1970-01-01 00:00:00 UTCto set in theDEVICEwith the ATASET DATE & TIMEcommand. SECS gets multiplied by 1000 before being used in that command (math! we love her).-S,--srepβ report the number of seconds since1970-01-01 00:00:00 UTC. This is done by dividing the value returned by the ATAREAD LOG [DMA] EXTcommand by1000.-v,--verboseβ increase the level of verbosity (i.e. debug output). For the nosy girlies π΅οΈββοΈ-V,--versionβ print the version string and then exit.
π EXIT STATUS π
The exit status of sg_sat_datetime is 0 when it is successful. Otherwise, see the sg3_utils(8) man page β it's got the full breakdown.
π NOTES π
The DEVICE should immediately start accumulating time after the date and time is set. Like, ie. if you set it and then retrieve it, the returned values will likely have increased. She's ticking, bestie β°
The ACS-5 standard makes no provision for a power-safe date and time storage. Unless the device has vendor-specific power-safe capability, after a power-on reset, per ACS-5, the date and time should be the power-on hours of the device returned in milliseconds (or seconds if the --srep option is used). So don't be shocked when it comes back weird. That's just how it is π
π EXAMPLES π
On Unix machines (e.g. Linux, FreeBSD, and Solaris) the date command is useful when working with timestamps. Here's the whole flow β¨
To fetch the timestamp from a DEVICE and display it in a humanly readable form, the following could be used:
# sg_sat_datetime -S /dev/sg2
1681156506
# date --date=@1681156506
Mon Apr 10 02:55:06 PM CDT 2023
# date -R --date="@1681156506"
Mon, 10 Apr 2023 14:55:06 -0500
The latter two date commands show different forms of the same date (i.e. 1681156506 seconds since 1970-01-01 00:00:00 UTC). And the sg_sat_datetime and date commands can be combined using backquotes:
# date -R --date=@`sg_sat_datetime -S /dev/sg2`
Mon, 10 Apr 2023 14:55:06 -0500
Alternatively, the --format option can be used to format the date and time using the default locale.
# sg_sat_datetime --format /dev/sg2
Mon Apr 10 15:02:54 2023
To set the date and time on the DEVICE to now (approximately) the following could be used:
# date +%s
1681157099
# sg_sat_datetime --seconds=1681157099 /dev/sg0
And those two command lines could be combined into one by using backquotes:
# sg_sat_datetime --seconds=`date +%s` /dev/sg0
Iconic. Ate. No notes π
π AUTHORS π
Written by Jeremy Bauer and Daniel Woeste with extensive use of sg_timestamp and sg_sat_read_gplog by Douglas Gilbert. Shoutout to the whole team π«Ά
π REPORTING BUGS π
Report bugs to <dgilbert at interlog dot com>.
πΈ COPYRIGHT πΈ
Copyright Β© 2023 Jeremy Bauer and Daniel Woeste of Western Digital Corporation
This software is distributed under a FreeBSD license. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
β SEE ALSO β
sdparm(sdparm)sg_logs(sg3_utils)sg_timestamp(sg3_utils)sg_sat_read_gplog(sg3_utils)
okay that's the whole page bestie, go set some clocks π°οΈπ β this was sg3_utils-1.48, dated May 2023, and that's the SG_SAT_DATETIME(8) sign-off from me to you ππ
π¦ where this page comes from
- arch rolling: sg3_utils 1.49-1 Β· GPL-2.0-or-later AND BSD-2-Clause
- nixpkgs unstable: sg3_utils 1.49
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms