I need to access the date and time in ubuntu from a program.This program may not use any commands to do this. So making a call to date is not an option.
Is there a file or files which hold this information?
Where can it be found ?
No, read time(7). There are some system calls (listed in syscalls(2)...) to query the time (since Unix Epoch); in particular time(2) and clock_gettime(2).
You then need to convert that time into a string, probably using localtime(3) then strftime(3). That conversion use some files notably /etc/timezone (and some under /usr/share/zoneinfo/ ...) according to TZ variable (see environ(7) and locale(7)).
BTW, date is free software (so you could study its source code). And you could strace(1) it.
See also vdso(7) and this.
Related
I'm writing a script that processes files in directories, but I want the directories' timestamps to remain the same. So I want to get the timestamp before the operation, then set in back after.
Getting the timestamp is easy enough (os.path.getmtime()), but I can't seem to find an equivalent set method.
Any suggestion?
use os.utime().
Should work fine.
Define variables with date, time and stuff before so the actual setting function doesn't get that cluttered.
I'm doing tests for a system on which the output depends on the current date, the system compares the output with an expected output I prepared previously.
For the test to pass, I shall invoke the system with the same date so I can obtain the same results and the test can succeed. otherwise, the test will fail.
I want to set the current date within the cmd scope ONLY without affecting the current date of the system.
This date is accessed either using the latex (\the\year) variable and from python using the datetime library (datetime.now()).
I tried using the date command but this changes the date in the system which triggers many operations and errors (Antivirus updates, etc ...) because its a corporate server so I should not change the date of the server while running the tests.
I can write code to neglect the comparison of the dates, but this operation is too complicated as the date is written in generated PDFs and many other files are in different formats.
So, is it possible to change the current date within the windows 10 command line ONLY (Not system scope)?
you might find your answer here Stackoverflow Discussion about Datetime from CMD and here Getting Datetime independently from Windows locale
Furthermore, you can set a locale in python this way:
import locale
locale.setlocale(locale.LC_TIME, "your/locale/here")
making something that works across both Python and LaTeX is awkward and very OS specific. google suggests that tools like RunAsDate exists for Windows but they hook some pretty nasty internal calls to do things
as #ndclt suggested, in Python the standard approach would be to "mock" the relevant functions out such that they return a known value. under LaTeX you could write a "test harness" that does \year=2000 \input{orig.tex}
I found this answer, and it somewhat provides what I needed, but I wanted to ask about any problems that can occur when storing and pulling files from Dropbox based on date.
I have an employee list with the filename empList.txt sitting in a DB folder named empList-20171106_183150. The folder name has the year,month,day and time right down to the second, appended to it(YYYYMMDD_HHMMSS).
Locally, I have a python script that has a log(txt), which just contains the date of the last time the script went and downloaded the updated list. The log looks like this if the last time the script ran was on Nov 01 2017 at 9am
20171101_090020
If I used Dropbox and a script written in Python to download the lastest version based on the date/time, are there any disadvantages to doing this?
I just compare the date stored in the log, to the date appended on the folder. If the date of folder in DB is greater, a download is needed. My only concern is during the date comparison and download, one of the managers might upload a new list, meaning I would have to run the script again.
How do complete programs such as MalwareBytes or internet security software manage a user downloading an update when they make a new update available at the same time? For me, I just run the update again to make sure while I was checkking/updating a new update wasn't made available.
I wouldn't recommend using date comparisons, because of potential issues with race conditions, like you mentioned, etc.
The Dropbox API exposes ways to tell if things have changed instead. Specifically, when downloading the file, you should store the metadata for the version of the file you downloaded. In particular, FileMetadata.rev or FileMetadata.content_hash would be useful.
If you then check again later and either of those values are different than the last one you downloaded, you know something has changed, so you should re-download.
Can I change creation date of some file using Python in Linux?
Linux and Unix file system stores :
File access, change and modification time (remember UNIX or Linux never stores file creation time, this is favorite question asked in UNIX/Linux sys admin job interview)
Understanding UNIX / Linux file systems
You can use os.utime to change access and modify time but not the creation date.
I am not a UNIX expert, so maybe I'm wrong, but I think that UNIX (or Linux) don't store file creation time.
Check out os.utime
os.utime(file_path,(new_atime,new_mtime))
It's no longer true that Linux doesn't support creation time. See:
https://askubuntu.com/questions/470134/how-do-i-find-the-creation-time-of-a-file
Note this specific answer to view C-code that displays the field:
https://askubuntu.com/a/980750/116108
If the author of that post can help I might be able to create a wrapper with ctypes to modify it from Python.
I would like to write a small script that does the following (and that I can then run using my crontab):
Look into a directory that contains directories whose names are in some date format, e.g. 30-10-09.
Convert the directory name to the date it represents (of course, I could put this information as a string into a file in these directories, that doesn't matter to me).
Compare each date with the current system time and find the one that has a specific time difference to the current system date, e.g. less than two days.
Then, do something with the files in that directory (e.g., paste them together and send an email).
I know a little bash scripting, but I don't know whether bash can itself handle this. I think I could do this in R, but the server where this needs to run doesn't have R.
I'm curious anyway to learn a little bit of either Python or Ruby (both of which are on the server).
Can someone point me in the right direction what might be the best way to do this?
I would suggest using Python. You'll need the following functions:
os.listdir gives you the directory contents, as a list of strings
time.strptime(name, "%d-%m-%y") will try to parse such a string, and return a time tuple. You get a ValueError exception if parsing fails.
time.mktime will convert a time tuple into seconds since the epoch.
time.time returns seconds since the epoch
the smtplib module can send emails, assuming you know what SMTP server to use. Alternatively, you can run /usr/lib/sendmail, through the subprocess module (assuming /usr/lib/sendmail is correctly configured)