Python Forum
Timestamp of file changes if a share mapped to a server….
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timestamp of file changes if a share mapped to a server….
#21
(Jun-30-2023, 08:14 PM)tester_V Wrote: It sounds very good but then there are tons of Python datetime libraries for a reason...
Pendulum is the best bye fair bit and it's built on top of datetime,
and special care has been taken to ensure Timezones are handled correctly.

Can show some seamless convert between time zones,using in_timezone and convert.
Start for now time in Kuala_Lumpur,if it's timestamp it will work the same as all comparisons in Pendulum are done in UTC or in the Timezone of the datetime being used.
>>> import pendulum
>>>
>>> kuala = pendulum.now("Asia/Kuala_Lumpur")
>>> kuala
DateTime(2023, 7, 1, 8, 14, 24, 85924, tzinfo=Timezone('Asia/Kuala_Lumpur'))
>>> print(kuala)
2023-07-01T08:14:24.085924+08:00
>>> print(kuala.to_datetime_string())
2023-07-01 08:14:24
There are many to way display time in Pendulum can use to_datetime_string then it easier to see.
>>> seattle = kuala.in_timezone('US/Pacific')
>>> print(seattle.to_datetime_string())
2023-06-30 17:14:24

>>> # Check with Timezone i an in
>>> norway = kuala.in_timezone('Europe/Paris')
>>> print(norway.to_datetime_string())
2023-07-01 02:14:24

# Convert Kuala_Lumpur time to Seattle time
>>> seattle = pendulum.timezone('US/Pacific')
>>> seattle = seattle.convert(kuala)
>>> print(seattle.to_datetime_string())
2023-06-30 17:14:24
tester_V likes this post
Reply
#22
This is a great conversation, I read tons of docs and started understanding (kind of) some of the "date times" I'm working with. I think for now I should just stick with the snippet shared by the All Great and all Powerful deanhystad Wink
from datetime import datetime, date, time, timedelta
from pytz import timezone
from pathlib import Path
 
# Get start and end of day in Seattle
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
end_time = start_time + timedelta(hours=24)
print(start_time, end_time)
 
# Convert to timestamps.
start_time = start_time.timestamp()
end_time = end_time.timestamp()
print(start_time, end_time)
 
# Print all files that were modified today.
folder = Path(__file__).parent  # Scan folder that contains this file.
for file in folder.iterdir():
    mtime = file.stat().st_mtime
    if start_time <= mtime < end_time:
        print(file.name, mtime, datetime.fromtimestamp(mtime)) 
I understand that the range of "datetimes" for the files to download from Asia is set by the "start_time" and "end_time"
both "times" are in UTC and localized for the Pacific NW.
if it is true that all timestamps are UTC then the following should filter the files I'm looking for:
for file in folder.iterdir():
    mtime = file.stat().st_mtime
    if start_time <= mtime < end_time:
but it seems I'm getting the "wrong timestamps". Then I thought OK, let's localize my "start_time" and "end_time" for the Kuala_Lumpur because it is local time for the hosts I'm trying to get the files from.
#start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("Asia/Kuala_Lumpur"))
end_time = start_time + timedelta(hours=24)
print(start_time, end_time)
 
# Convert to timestamps.
start_time = start_time.timestamp()
end_time = end_time.timestamp()
print(start_time, end_time)
It seems I'm still getting the files with "wrong" timestamps. I know that the files have the wrong timestamps because when I open them data inside is from tomorrow...

What did I do wrong?
By the way, I was thinking maybe the line below is not localized it is a UTC timestamp but not tied up to any time zone.
mtime = file.stat().st_mtime
I tried :
mtime3 = ef.stat().st_mtime(pytz.utc) 
But it produces an error:
TypeError: 'float' object is not callable

Again, I really appreciate you guys looking into it!
Thank you!
Reply
#23
All your work should use timestamps, and all timestamps are UTC. Specify the start and stop times using DateTime objects for whatever timezone you prefer, but convert to a timestamp for making comparisons.
Reply
#24
I think I did that, actually, you did, I just converted the timestamps to "Asia/Kuala_Lumpur" Time Zone.
It did not work.

#start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("Asia/Kuala_Lumpur"))
end_time = start_time + timedelta(hours=24)
print(start_time, end_time)
  
# Convert to timestamps.
start_time = start_time.timestamp()
end_time = end_time.timestamp()
print(start_time, end_time)
I think it does not work because I did not convert the timestamp var - "mtime" to "Asia/Kuala_Lumpur" Time Zone:
for file in folder.iterdir():
    mtime = file.stat().st_mtime
    if start_time <= mtime < end_time:
        print(file.name, mtime, datetime.fromtimestamp(mtime))
Do they all "start_time", "mtime" and the "end_time" should be in the same time zone?
I just could not find how to do that.
thank you.
Reply
#25
You can either think that all timestamps are from the same time zone (GMT), or you can think that time zones do not apply to timestamps. Either way, you cannot have timestamps that are in different time zones. It is impossible. Time zones only apply to DateTime objects. If you ask a Seattle DateTime for a timestamp, the timestamp is UTC. If you ask a Kuala Lumpur DateTime for a timestamp, the timestamp is UTC. When you ask the operating system for a files mtime or ctime, it is a timestamp, and since it is a timestamp it will also be UTC.

Maybe this will help clear things up. You are talking on the phone to someone in Kuala Lumpur. You do a countdown "3...2...1...GO!", and on go you both run this program.
from datetime import datetime

print(datetime.now(), datetime.now().timestamp())
Because of the time zone difference, the datetime objects will print different times (maybe even different dates), but the timestamps will be the same.
Reply
#26
ok, the timestamp is always displayed by numbers something like this '1679796823.977553'
And I think I do compare the "mtime" var which is the timestamp of each file against the "timestamp filter" that was created with the:
#start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("Asia/Kuala_Lumpur"))
end_time = start_time + timedelta(hours=24)
print(start_time, end_time)
   
# Convert to timestamps.
start_time = start_time.timestamp()
end_time = end_time.timestamp()
print(start_time, end_time)
it seems the logic is sound...
I'm comparing the "timestamp filter" localized for the target timezone.
Right?
Why I'm failing, I'm comparing the same time format that is UTC....

Sorry about this, I did not mean to give you a hard time!
Thank you.
Reply
#27
(Jul-02-2023, 05:00 AM)tester_V Wrote: Why I'm failing, I'm comparing the same time format that is UTC....

Sorry about this, I did not mean to give you a hard time!
Thank you.
I think deanhystad has explain this in servals ways.

To take a little more about this,so pytz as used in this Thread is deprecated and should not be used anymore.
In standard library there in now zoneinfo that takes over for pytz.

So one more demo,as you mention this timestamp 1679796823.977553 is from Kuala_Lumpur.
The timestamp is always in UTC format,
If you and the guy in Kuala_Lumpur run timestamp script at same time both will be 1679796823.977553 .
But time and Timezone will of course be different

So can just work with UTC time format,or can eg convert as shown under
Using zoneinfo to convert Kuala_Lumpur timestamp to Seattle time.
from datetime import datetime
from zoneinfo import ZoneInfo

from_timezone = ZoneInfo('Asia/Kuala_Lumpur')
to_timezone = ZoneInfo('US/Pacific')

timestamp = 1679796823.977553
dt = datetime.fromtimestamp(timestamp, to_timezone)
result_timestamp = dt.replace(tzinfo=from_timezone).timestamp()
print(dt)
print(result_timestamp)
Output:
2023-03-25 19:13:43.977553-07:00 1679742823.977553
The same with Pendulum.
import pendulum

timestamp = 1679796823.977553
seattle = pendulum.timezone('US/Pacific')
seattle_time = seattle.convert(pendulum.from_timestamp(timestamp))
seattle_timestamp = seattle_time.timestamp()
print(seattle_time.to_datetime_string())
print(seattle_timestamp)
Output:
2023-03-25 19:13:43 1679796823.977553

So if this work can test in reverse,i can take a timestamp at my place now at then run code an it will print the time Kuala Lumpur now.
from datetime import datetime
from zoneinfo import ZoneInfo

from_timezone = ZoneInfo('Europe/Paris')
to_timezone = ZoneInfo('Asia/Kuala_Lumpur')

# I make this timestamp now Norway
timestamp = datetime.now().timestamp()
dt = datetime.fromtimestamp(timestamp, to_timezone)
result_timestamp = dt.replace(tzinfo=from_timezone).timestamp()
print(dt)
print(result_timestamp)
Output:
2023-07-02 20:36:53.733209+08:00 1688323013.733209
Larz60+ likes this post
Reply
#28
I wish I would try the "pendulum" module, I'm ready to try something different,
but it is failing installation on my server and on my working laptop with the same error:

Using cached pendulum-2.1.2.tar.gz (81 kB)
Installing build dependencies ... error
error: subprocess-exited-with-error

× pip subprocess to install build dependencies did not run successfully.
│ exit code: 1
╰─> [10 lines of output]
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x000001702E4C30A0>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/poetry-core/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by

So I have to stick with datetime....
Thank you for the snippets and the coaching.
Reply
#29
My server has Python 3.9 installed. when I tried to pip 'zoneinfo' it produced an error:

ERROR: Could not find a version that satisfies the requirement zoneinfo (from versions: none)
ERROR: No matching distribution found for zoneinfo

I have to stay with 'pytz'.
Reply
#30
ok. I'm staying with the UTC timestamp. UTC timestamp has no time zone.

Is the snippet below creates the correct UTC time-based filter of 24 hours period?

#start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("Asia/Kuala_Lumpur"))
end_time = start_time + timedelta(hours=24)
print(start_time, end_time)
    
# Convert to timestamps.
start_time = start_time.timestamp()
end_time = end_time.timestamp()
print(start_time, end_time)


Could you confirm whether it is correct or not?
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 429 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 926 Feb-16-2023, 08:11 PM
Last Post: cubangt
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,317 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  access share attributed among several class methods drSlump 0 1,134 Nov-18-2021, 03:02 PM
Last Post: drSlump
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,673 Oct-21-2021, 03:29 AM
Last Post: buran
  How to take the tar backup files form remote server to local server sivareddy 0 2,027 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,590 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to share a numpy array between 2 processes on Windows? qstdy 0 2,232 Jan-29-2021, 04:24 AM
Last Post: qstdy
  Access Network SMB Share on a NAS deltapy 2 9,429 Oct-31-2020, 09:14 AM
Last Post: deltapy
  Multiprocessing share variable catosp 0 2,214 Jul-15-2020, 10:45 AM
Last Post: catosp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020