Operations on os.path.getctime - python-3.x

I am reading the following JSON:
{
"Age": 15,
"startTime": {
"date": "06/15/2021",
"time": "4:04 pm",
"utcTimestamp": 1623765862
},...
with
data=json.load(self.fName)
Dict['StartDate'] = data['startTime']['date']
Dict['StartTime'] = data['startTime']['time']
I also created a variable to track the file creation time:
fileCreationTime= datetime.fromtimestamp(os.path.getctime(fname)).strftime('%Y-%m-%d %H:%M:%S')
I am trying to find the amount of time between the time the json file was created and the "StartTime" in the json file.
I tried a few things including:
daysToUpload = datetime.fromtimestamp(os.path.getctime(fname)).strftime('%Y-%m-%d') - Dict['StartDate']
But that did not work. (unsupported operand type(s) for -: 'str' and 'str').
Maybe I can use the UPC time but
>>os.path.getctime(fname)
>>1635377313.0170193
I'm not sure how to relate that the UTC.
I'd like:
timeToUpload = fileCreationDate - TimeSpecifiedInJsonFile
This is Python 3.x running on Windows.

To get the time difference, you can simply use utc_timestamp itself without converting it to datetime format as follows:
fileCreationDate = int(os.path.getctime(fname)) # utc timestamp
TimeSpecifiedInJsonFile = int(data['startTime']['utcTimestamp']) # utc timestamp
timeToUpload = fileCreationDate - TimeSpecifiedInJsonFile
print(timeToUpload) # will be print a time difference (seconds)

Related

What is the correct way to parse time in a timezone using momentjs

I am trying to parse time using following code line
const fromAnchorDate = moment.tz('09:00 AM', "hh:mm A", "America/New_York").tz("America/New_York");
However, when I look at this object why does it show time as 1:00 AM in PST. Shouldn't it be 6 AM PST.

Adding attributes to hdf5 file

I had this script to convert some .txt files into .hdf5. These were later used as input for another function.
I had implemented this before and everythin was running smoothly (about 2weeks ago).
name = "ecg.hdf5"
sampling_rate = 250;
ecg = np.genfromtxt('ecg.txt')
hf = h5py.File(name, 'w')
# Create subgroups and add dataset
signals = hf.create_group('signals/ECG/raw')
ecg = signals.create_dataset('ecg',data = ecg)
# max and min for plot limits
ecg_max = max(ecg)
ecg_min = min(ecg)
# Add attributes
ecg.attrs.create('json','{"name": "signal0", "resolution": 16, "labels": ["I"], "units": {"signal": {"max": %f, "min": %f}, "time": {"label": "second"}}, "sampleRate": %d, "type": "/ECG/raw/ecg"}' %(ecg_max,ecg_min,sampling_rate))
hf.close()
As I was running it, I keep having this error and can't atribute the 'attribute'
rro adding atribu
Any idea, please?
Thanks in advance
Solvet it: updating the h5py version from 2.9.0 to 2.10.0

Time data with gmt does not match format input for pandas conversion

How to convert date time string with GMT into a pandas date time format ?
Here is an example :
#date_time is like 12/Dec/2015:18:25:11 +0100
df['date_time'] = pd.to_datetime(df['date_time'], format="%d/%b/%Y:%I:%M:%S %Z")
Here is the error :
ValueError: time data '12/Dec/2015:18:25:11 +0100' does not match
format '%d/%b/%Y:%I:%M:%S %Z' (match)
You'd better check the formatted string:
https://docs.python.org/3/library/datetime.html
Use '%d/%b/%Y:%H:%M:%S %z' instead of '%d/%b/%Y:%I:%M:%S %Z'.

NodeJs How to use moment-timezone to convert Epoch to Specific TImezone and format

I have a use-case in which I want to convert an Epoch integer value into a specific Timezone time and a specific format,
Also, I want to convert a human-readable date time into epoch.
I am trying to use moment-tz for the timezone conversion.
I using a specific Epoch timestamp 1555296000 which is -
Monday, April 15, 2019 10:40:00 AM in Kuala Lampur Malaysia,
I am able to convert 2019-04-15 10:40:00 of Asia/Kuala_Lumpur timezone into correct Unix.
But I am unable to convert 1555296000 into another timezone's unix,
i.e I wish to convert 1555296000 into equivalent YYYY-MM-DD hh:mm:ss of Asia/Calcutta timezone.
Following is the code I'm trying to work with -
var moment = require('moment-timezone');
console.log("Convert from Asia/Kuala_Lumpur to Unix -> ", moment.tz("2019-04-15 10:40:00","Asia/Kuala_Lumpur").unix());
// Outputs - 1555296000
console.log("Epoch to Specific TimeZone and Format -> ",moment(1555296000).tz("Asia/Calcutta").format('YYYY-MM-DD hh:mm:ss'));
// Outputs - 1970-01-19 05:31:36
// I want - 2019-04-15 08:10:00
Try this out
const moment = require("moment-timezone");
console.log(
moment
.unix(1555296000)
.tz("Asia/Calcutta")
.format("YYYY-MM-DD HH:mm:ss")
);
2019-04-15 08:10:00 - "Asia/Calcutta" is 2019-04-15 10:40:00 - "Asia/Kuala_Lumpur"

Momentjs get time in current location

I'm trying to generate a momentjs object of a certain timestamp in the current day of a specified location. For example:
const timeNow = moment().tz('Africa/Cairo')
const startTime = moment('10:00 am', 'HH:mm a')
const endTime = moment('2:30 pm', 'HH:mm a')
Printing the above 3 variables outputs this:
Fri, 12:31 am
Thu, 10:00 am
Thu, 02:30 pm
Where the first result is in fact the current time in Cairo, However the other two results are the day before. How can I change it so that they return the current day?
You can simply do:
moment.tz('Africa/Cairo') // <= Moment Object
One small info: whenever you'll get to see some javascript date in a browser that will be shown in your system's time-zone. As javascript Date is UTC, browsers will show accordingly. Use moment.format() to get string values.

Resources