Kind of puzzled about timestamps

Joshua Judson Rosen rozzin at hackerposse.com
Thu Mar 4 21:56:45 EST 2021


On 3/4/21 7:13 PM, Bruce Labitt wrote:
> Good point.  I'll check that.  Logging machine was set to local time EST.  But it does have a wireless link, maybe it set itself internally to UT.  Thanks for the hint.

You have your code explicitly calling a function named `UTC from timestamp'.

If you want localtime and not UTC, call the function that doesn't start with "utc".

And if you want to assume some particular timezone other than your system's default,
you can pass that as an optional argument.

BTW, FYI "UT" is *not* the same thing as "UTC". Timezones are confusing enough,
it's worth spending the extra character to avoid creating even more confusion
(or just call it "Z" and save yourself even more characters).

And as a general word of advice from someone whose been burnt way too many times:
if you're going to put timestamps in your filenames, either just use UTC
or explicitly indicate which timezone the timestamps are assuming.

"the local non-UTC timezone" *changes*. Frequently. Like, twice every year if you're lucky--
and more frequently than that if you're unlucky. And if you are, for example, generating those
files/filenames between 1:00 AM and 2:00 AM when you go from EDT to EST in November
(and that "1:00-2:00 localtime" interval *repeats*)..., you'll be sorry.

-- 
Connect with me on the GNU social network! <https://status.hackerposse.com/rozzin>
Not on the network? Ask me for more info!


> On Thu, Mar 4, 2021, 7:05 PM Dana Nowell <dananowell at cornerstonesoftware.com <mailto:dananowell at cornerstonesoftware.com>> wrote:
> 
>     If I'm reading it correctly, it's a 5 hr difference?  Local vs gmt?
> 
> 
>     On Thu, Mar 4, 2021, 6:43 PM Bruce Labitt <bruce.labitt at myfairpoint.net <mailto:bruce.labitt at myfairpoint.net>> wrote:
> 
>         This is an odd question.  It involves both python and linux.
> 
>         Have a bunch of files in a directory that I'd like like to sort by similar names and in time order.  This isn't particularly difficult in python.  What is puzzling me is the modified timestamp returned by python doesn't match whats reported by the file manager nautilus or even ls.  (ls and nautilus are consistent)
> 
>         $ lsb_release -d Ubuntu 20.04.2 LTS
>         $ nautilus --version  GNOME nautilus 3.36.3
> 
>         $ python3 --version  Python 3.8.5
> 
>         $ ls -lght
> 
>         total 4.7M
>         -rw-r--r-- 1 bruce 209K Feb 26 01:49 20210226_022134_PLD.edf
>         -rw-r--r-- 1 bruce  65K Feb 26 01:49 20210226_022134_SAD.edf
>         -rw-r--r-- 1 bruce 2.4M Feb 26 01:49 20210226_022133_BRP.edf
>         -rw-r--r-- 1 bruce 1.1K Feb 26 00:58 20210225_224134_EVE.edf
>         -rw-r--r-- 1 bruce 1.9M Feb 25 21:18 20210225_224141_BRP.edf
>         -rw-r--r-- 1 bruce 169K Feb 25 21:17 20210225_224142_PLD.edf
>         -rw-r--r-- 1 bruce  53K Feb 25 21:17 20210225_224142_SAD.edf
> 
>         Python3 script
> 
>         #!/usr/bin/env python3
>         import os
>         from datetime import datetime
> 
>         def convert_date(timestamp):
>           d = datetime.utcfromtimestamp(timestamp)
>           formatted_date = d.strftime('%d %b %Y  %H:%M:%S')
>           return formatted_date
> 
>         with os.scandir('feb262021') as entries:
>           for entry in entries:
>             if entry.is_file():
>               info = entry.stat()
>               print(f'{entry.name <http://entry.name>}\t Last Modified: {convert_date(info.st_mtime) }' )  # last modification
> 
>         info /(after exit) contains/: os.stat_result(st_mode=33188, st_ino=34477637, st_dev=66306, st_nlink=1, st_uid=1000, st_gid=1000, st_size=213416, st_atime=1614379184, st_mtime=1614322176, st_ctime=1614379184)
> 
>         Running the script results in:
> 
>         20210226_022133_BRP.edf     Last Modified: 26 Feb 2021  06:49:34
>         20210225_224141_BRP.edf     Last Modified: 26 Feb 2021  02:18:42
>         20210225_224142_PLD.edf     Last Modified: 26 Feb 2021  02:17:44
>         20210225_224142_SAD.edf     Last Modified: 26 Feb 2021  02:17:44
>         20210225_224134_EVE.edf     Last Modified: 26 Feb 2021  05:58:26
>         20210226_022134_SAD.edf     Last Modified: 26 Feb 2021  06:49:36
>         20210226_022134_PLD.edf     Last Modified: 26 Feb 2021  06:49:36
> 
>         Actually, what is returned by my script is at least sensible, given that 20210225_224141_BRP.edf started on Feb 25th and ended recording at 2:17am on Feb 26th.  I know this because I can see the data on a separate program.  20210226_022133_BRP.edf started on Feb 26th at around 2:21am and terminated at 6:49am.  BRP files are written to continuously at a 25 Hz rate all evening.  What makes no sense whatsoever is what *ls* is reporting.
> 
>         Do *ls* and python3 use different definitions of "last modified"?
> 
>         Guess I can keep going, but I really was surprised at the difference between methods.  Default for ls is "last modified", at least as reported by man.  ls's last modified just isn't correct, at least on Ubuntu 20.04.2
> 
>         Is this a quirk?  Am I doing something wrong?  Some kind of voodoo definition of "last modified"?  What does Linux say "last modified" really means?
> 
>         FWIW, I am coming up to speed on processing these edf files to help out on an open source project.  Been working on some data analysis tools.  As an aside, biological data is very messy.  It's been a treat to work on this as it's forced me to dust off the mental cobwebs and work on a problem that can help a lot of people.


More information about the gnhlug-discuss mailing list