I'm trying to set the time in linux with python. I got the date and time , what i need to do the set the time that i got in my system?
import os
import ntplib
from datetime import datetime,timezone
c = ntplib.NTPClient()
response = c.request('ch.pool.ntp.org',version = 3)
response.offset
data = datetime.fromtimestamp(response.tx_time, timezone.utc)
time = data.time()
date = data.date()
time1 =time.strftime("%H:%M:%S")
os.system('date --set %s' % date)
This is the code i wrote to get the time from a server .
in bold it's what i tried and it not working.
Something like this:
import os
# TOOD use real date
rv = os.system('date -s "2 OCT 2006 18:00:00"')
if rv == 0:
print('date was set')
else:
print('date was not set')
Or you can configure your linux to use NTP server.
Related
I'm having issues with timezones and time offsets when working with Python, the Google Calendar API, and a client device operating in local time.
I have a Raspberry Pi which acts as a booking display outside a shared room. A Python script is called periodically to pull in the next upcoming calendar event from the Google Calendar API, split the result into variables, then pass those variables back to the bash script which called it.
I built it in the winter—when the UK's timezone has equivalence to UTC—and it worked perfectly. Now, in British Summer Time, it doesn't. The comparison of time objects to check whether or not the meeting is currently in progress doesn't work, and the room is showing as available when it isn't.
I've read and read (then re-read and re-read) articles and explainers about working with timezones and time offsets in Python, and I just cannot get my head around it. It doesn't help that I don't really understand object-based programming, so the example scripts provided don't mean much to me!
Ideally, I would simply like everything to work using local time. The Raspberry Pi is connected to the internet and its time is updated automatically, and the Google Calendar is correctly set to the UK timezone. How could I go about making all references to time 'local', so the time objects everywhere in the Python script are always current UK time?
Please help. Stack Overflow has been such a rich source of knowledge in the past that I've never needed to ask a question myself, but this is making my brain hurt! Thank you!
This is what I've got, which is a modified version of Google's own example script:
service = build('calendar', 'v3', credentials=creds)
# Call the Calendar API
timeStamp_now = datetime.datetime.utcnow()
timeStamp_12 = timeStamp_now + timedelta(hours = 12)
now = timeStamp_now.isoformat() + 'Z' # 'Z' indicates UTC time
now12 = timeStamp_12.isoformat() + 'Z' # 'Z' indicates UTC time
events_result = service.events().list(calendarId='################resource.calendar.google.com', timeZone="Europe/London", timeMin=now,
maxResults=1, singleEvents=True, showDeleted=False, timeMax=now12,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('meetingStart="NoEvents"')
for event in events:
startTime=event['start'].get('dateTime')
endTime=event['end'].get('dateTime')
eventTitle=event['summary']
eventOrganizer=event['creator'].get('email')
try:
eventConferencing=event['conferenceData'].get('conferenceSolution').get('name')
except:
eventConferencing=(' ')
if startTime <= now <= endTime:
inProgress=('true')
else:
inProgress=('false')
safeEventTitle= ""
for i in eventTitle:
num = ord(i)
if (num >=0) :
if (num <=127) :
safeEventTitle= safeEventTitle + i
print('meetingStart="' + startTime + '"')
print('meetingEnd="' + endTime + '"')
print('meetingTitle="' + safeEventTitle + '"')
print('meetingOrganizer="' + eventOrganizer + '"')
print('meetingConferencing="' + eventConferencing + '"')
print('meetingInProgress="' + inProgress + '"')
The Raspberry Pi's time is set correctly:
pi#raspberrypi:~ $ date
Fri 16 Jul 20:31:38 BST 2021
The Python script returns this when run:
pi#raspberrypi:~ python /myPythonScript.sh
meetingStart="2021-07-16T20:00:00+01:00"
meetingEnd="2021-07-16T21:00:00+01:00"
meetingTitle="Test"
meetingOrganizer="abc#def.com"
meetingConferencing=" "
meetingInProgress="false"
See a sample code below that will adjust your datetime to UTC regardless of Daylight Saving Time.
Code:
import datetime
import pytz
timeZone = pytz.timezone("Europe/London")
dt = datetime.datetime.utcnow()
print("datetime now is:\t", dt)
local_dt = timeZone.localize(dt, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
print("Non-DST time is:\t", utc_dt) # prints utc regardless of DST
Output using bst datetime:
Output using utc datetime:
In your case:
timeZone = pytz.timezone("Europe/London")
dt = datetime.datetime.utcnow()
local_dt = timeZone.localize(dt, is_dst=None)
# timeStamp_now below should be in UTC already
timeStamp_now = local_dt.astimezone(pytz.utc)
Reference:
How to convert local time string to UTC?
Python daylight savings time
I need to get the time for the UK from an NTP server. Found stuff online however any time I try out the code, I always get a return date time, the same as my computer. I changed the time on my computer to confirm this, and I always get that, so it's not coming from the NTP server.
import ntplib
from time import ctime
c = ntplib.NTPClient()
response = c.request('uk.pool.ntp.org', version=3)
response.offset
print (ctime(response.tx_time))
print (ntplib.ref_id_to_text(response.ref_id))
x = ntplib.NTPClient()
print ((x.request('ch.pool.ntp.org').tx_time))
This will work (Python 3):
import socket
import struct
import sys
import time
def RequestTimefromNtp(addr='0.de.pool.ntp.org'):
REF_TIME_1970 = 2208988800 # Reference time
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = b'\x1b' + 47 * b'\0'
client.sendto(data, (addr, 123))
data, address = client.recvfrom(1024)
if data:
t = struct.unpack('!12I', data)[10]
t -= REF_TIME_1970
return time.ctime(t), t
if __name__ == "__main__":
print(RequestTimefromNtp())
The timestamps returned as call to the NTP server returns time in seconds.
ctime() provides datetime format based on local machine's timezone settings by default. Thus, for uk timezone you need to convert tx_time using that timezone. Python's in-built datetime module contains function for this purpose
import ntplib
from datetime import datetime, timezone
c = ntplib.NTPClient()
# Provide the respective ntp server ip in below function
response = c.request('uk.pool.ntp.org', version=3)
response.offset
print (datetime.fromtimestamp(response.tx_time, timezone.utc))
UTC timezone used here. For working with different timezones you can use pytz library
This is basically Ahmads answer but working for me on Python 3. I am currently keen on Arrow as simplifying times and then you get:
import arrow
import socket
import struct
import sys
def RequestTimefromNtp(addr='0.de.pool.ntp.org'):
REF_TIME_1970 = 2208988800 # Reference time
client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
data = b'\x1b' + 47 * b'\0'
client.sendto( data, (addr, 123))
data, address = client.recvfrom( 1024 )
if data:
t = struct.unpack( '!12I', data )[10]
t -= REF_TIME_1970
return arrow.get(t)
print(RequestTimefromNtp())
The following function is working well using python 3:
def GetNTPDateTime(server):
try:
ntpDate = None
client = ntplib.NTPClient()
response = client.request(server, version=3)
ntpDate = ctime(response.tx_time)
print (ntpDate)
except Exception as e:
print (e)
return datetime.datetime.strptime(ntpDate, "%a %b %d %H:%M:%S %Y")
I used ntplib server and get date and change format in dd-mm-yyyy
Hi Everyone i have googles to my hearts content but have not found the answer.
Basically I want to add user inputted time to the current time.
This is just a small project I'm working on while learning Python.
So if the current time is 17:16 and the user wants to add 1hr 30 to that. how would i do it.
This is what i have:
import datetime
flex = input("Enter your flex amount in HHMM:")
flex = flex[0]+flex[1]+"-"+flex[2]+flex[3]
time = datetime.datetime.now().strftime("%H-%M")
balance = time+flex
print(time)
print(flex)
print(balance)
I have now tried
import datetime
flex = input("Enter your flex amount in HHMM:")
time = datetime.datetime.now().strftime("%H-%M")
flex = flex[0]+flex[1]+"-"+flex[2]+flex[3]
time = time[0]+time[1]+"-"+time[2]+time[3]
balance = datetime.timedelta(hours=int(time[0]+time[1]),
minutes=int(time[2]+time[3]) +
datetime.timedelta(hours=int(flex[0]+flex[1]),
minutes=int(flex[2]+flex[3]))
But now its complaining about its expecting an integer. but if i change it ot an integer will that not defeat the purpose of me wanting to add is as time.
Thanks
I got it to work using the answer. This is what it looks like now thanks pal.
from datetime import timedelta as td
import datetime as da
#flex = input("Enter your flex amount in HHMM:")
flex = "0134"
now = da.datetime.now()
user_hours = int(flex[:2])
user_minute = int(flex[2:5])
delay = td(hours=user_hours, minutes=user_minute)
balance = da.datetime.now()+delay
print("Lunch: " +str(lunch))
print("Time when balance at 00:00 : " +str(balance))
print("Now: " +str(now))
Simple using timedelta create an offset indicated by timedelta object and ad it to your time object (working the same with date and datetime too).
from datetime import timedelta, datetime
actual_time = datetime.now()
user_hours = int(flex[:3])
user_minute = int(flex[2:5])
delay = timedelta(hours=user_hours, minutes=user_minute)
print(datetime.now()+delay)
So if the current time is 17:16 and the user wants to add 1hr 30 to
that. how would i do it.
You can use timedelta, i.e.:
new_time = datetime.datetime.now() + datetime.timedelta(hours=1, minutes=30) # or simply minutes=90, etc...
Cool so when tring
balance = datetime.timedelta(hours=int(time[0]+time[1]), minutes=int(time[2]+time[3]) + datetime.timedelta(hours=int(flex[0]+flex[1]), minutes=int(flex[2]+flex[3]))
its complaining that its expecting an interger not a time delta
I have the following:
import psycopg2
from openpyxl import Workbook
wb = Workbook()
wb.active =0
ws = wb.active
ws.title = "Repair"
ws.sheet_properties.tabColor = "CCFFCC"
print(wb.sheetnames)
import datetime
import smtplib
import mimetypes
import logging
LOG_FILENAME = 'log-production.out'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
logging.debug('This message should go to the log file')
from datetime import date, timedelta
import os, sys
try:
conn = psycopg2.connect("connection string")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
cur.execute("""SELECT ams.unit.line,ams.unit.work_order,ams.unit.model_num, ams.unit.revision ,ams.unit.serial_num,ams.unit.lpn, ams.unit_repair_detail.level_1_name as level_1,
ams.unit_repair_detail.level_2_name as level_2, ams.unit_repair_detail.level_3_name as level_3,ams.unit_repair_detail.level_4_name as level_4,ams.unit_repair.date_started AT TIME ZONE 'UTC' as date_started,ams.unit_repair.date_completed AT TIME ZONE 'UTC' as date_completed
FROM ams.unit_repair
left join
ams.unit
on ams.unit_repair.unit_id=ams.unit.id and
LOWER(ams.unit_repair.line) = LOWER(ams.unit.line)
right join
ams.unit_repair_detail
on ams.unit_repair.sid = ams.unit_repair_detail.unit_repair_sid
WHERE
LOWER(ams.unit.line) like ('%') and
ams.unit_repair_detail.date_created >= (CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - interval '24 hours')
AND ams.unit_repair_detail.date_created <= (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
and LOWER(ams.unit.model_num) like LOWER('%')
order by model_num asc""")
rows = cur.fetchall()
print "\nShow me the databases:\n"
col_names = ["Line","Work order","Model number","Revision","Serial number","Lpn","Level 1","Level 2","Level 3","Level 4","Date started","Date completed"]
ws.append(col_names)
for row in rows:
ws.append(row)
This was working but after the daylight savings time change everything broke... The query returns the correct data on the db but when I run it from the python script and the file is created it is still in UTC time. I don't know what I am doing that is converting my dates back to UTC... Can anybody help me? I have tried setting the timezones at the top to be central so it converts the UTC to central with no luck
cur.execute("SET TIME ZONE 'America/Chicago';")
I have also tried
>>> import time
>>> offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
>>> offset / 60 / 60 * -1
I also tried changing my AT TIME ZONE UTC TO CST and no luck... I have tried multiple solutions on the web but nothing appears to be working. Any help will be greatly appreciated!!
just in case anybody runs into something like this in the future... I found the problem if you add the following to the query
`at time zone 'America/Chicago'
it will resolve the problem. Somehow the at time zone UTC is not enough you still need to specify the output timezone
I need my script to download a new file, if the old one is old enough. I set the maximum age of file in seconds. So that I would get back on track with my script writing I need example code, where file age is printed out in seconds.
This shows how to find a file's (or directory's) last modification time:
Here are the number of seconds since the Epoch, using os.stat
import os
st=os.stat('/tmp')
mtime=st.st_mtime
print(mtime)
# 1325704746.52
Or, equivalently, using os.path.getmtime:
print(os.path.getmtime('/tmp'))
# 1325704746.52
If you want a datetime.datetime object:
import datetime
print("mdatetime = {}".format(datetime.datetime.fromtimestamp(mtime)))
# mdatetime = 2012-01-04 14:19:06.523398
Or a formated string using time.ctime
import stat
print("last accessed => {}".format(time.ctime(st[stat.ST_ATIME])))
# last accessed => Wed Jan 4 14:09:55 2012
print("last modified => {}".format(time.ctime(st[stat.ST_MTIME])))
# last modified => Wed Jan 4 14:19:06 2012
print("last changed => {}".format(time.ctime(st[stat.ST_CTIME])))
# last changed => Wed Jan 4 14:19:06 2012
Although I didn't show it, there are equivalents for finding the access time and change time for all these methods. Just follow the links and search for "atime" or "ctime".
Another approach (I know I wasn't the first answer but here goes anyway):
import time, os, stat
def file_age_in_seconds(pathname):
return time.time() - os.stat(pathname)[stat.ST_MTIME]
The accepted answer does not actually answer the question, it just gives the answer for last modification time. For getting the file age in seconds, minutes or hour you can do this.
import os, time
def file_age(filepath):
return time.time() - os.path.getmtime(filepath)
seconds = file_age('myFile.txt') # 7200 seconds
minutes = int(seconds) / 60 # 120 minutes
hours = minutes / 60 # 2 hours
Use stat.M_TIME to get the last modified time and subtract it from the current time.
http://docs.python.org/library/stat.html
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, time
def file_age_in_seconds(filename):
try:
return int(time.time() - os.path.getmtime(filename))
except:
#on any failure condition
return -1
filename = "/tmp/foobar.txt"
print(file_age_in_seconds(filename)) #prints -1
f = open(filename, 'w')
f.write("this is a line")
f.close()
print(file_age_in_seconds(filename)) #prints 0
time.sleep(4.2)
print(file_age_in_seconds(filename)) #prints 4
This will do in days, can be modified for seconds also:
#!/usr/bin/python
import os
import datetime
from datetime import date
t1 = os.path.getctime("<filename>")
now = datetime.datetime.now()
Y1 = int(datetime.datetime.fromtimestamp(int(t1)).strftime('%Y'))
M1 = int(datetime.datetime.fromtimestamp(int(t1)).strftime('%m'))
D1 = int(datetime.datetime.fromtimestamp(int(t1)).strftime('%d'))
date1 = date(Y1, M1, D1)
Y2 = int(now.strftime('%Y'))
M2 = int(now.strftime('%m'))
D2 = int(now.strftime('%d'))
date2 = date(Y2, M2, D2)
diff = date2 - date1
days = diff.days
You can get it by using OS and datetime lib in python:
import os
from datetime import datetime
def fileAgeInSeconds(directory, filename):
file = os.path.join(directory, filename)
if os.path.isfile(file):
stat = os.stat(file)
try:
creation_time = datetime.fromtimestamp(stat.st_birthtime)
except AttributeError:
creation_time = datetime.fromtimestamp(stat.st_mtime)
curret_time = datetime.now()
duration = curret_time - creation_time
duration_in_s = duration.total_seconds()
return duration_in_s
else:
print('%s File not found' % file)
return 100000
#Calling the function
dir=/tmp/
fileAgeInSeconds(dir,'test.txt')