python - UTC minus 15 minutes in 13 digit timestamp format - python-2.7

I have scratched my brains enough to find my last resort here . I am calling an API which takes values like below for the lastUpdatedTime paramter :
lastUpdatedTime : 1499591547769
lastUpdatedTime : 1499591547770
So i assume the above values are converted to miliseconds since there are 13 digits (1499591547770 & 1499591547769).
Below is what i want to do in simple english .
Get the UTC time now and subtract 15 minutes from it .
Send that value to the API and fetch the data.
My problem is time conversion .
Below is what i am doing in python.
update_time = (datetime.utcnow() - timedelta(minutes=15)).strftime('%m/%d/%Y %H:%M:%S')
update_time_check = (calendar.timegm(time.strptime(update_time, '%m/%d/%Y %H:%M:%S')))*1000
The value for the above is something like 1502247759000
When i send that value (lastUpdatedTime = 1502247759000) to the API , i get a blank result set .
Please let me know if my steps are correct in generating the timestamp which the API is expecting .
Thanks in advance.

Related

Get single digit time in python

I am using following code to get current time in python.
import datetime
now = datetime.datetime.now()
message_sent_time = now.strftime("%I:%M %p")
It returns time as
06:58 PM
However, I would like to get output as
6:58 PM
i.e. single digit time when time is before 10. How can I achieve it?
You can get a single-digit hour using %-I:
>>> message_sent_time = now.strftime("%-I:%M %p")
>>> print(message_sent_time)
7:07 PM
Full strftime formatting reference: http://strftime.org/

How to get Current Date in Ballerina

Is there a way to get the current date in ballerina?
As I was browsing through some code examples I came across the syntax to get the current time. Shown below is how to get the current date in Ballerina:
Note: first you have to import the time package given below for this to work.
import ballerina/time;
Then put the following lines of code:
time: Time currentTime = time:[currentTime][2]();
string customTimeString = currentTime.format("dd-MM-yyyy");
This will give the following output:
08-07-2018
This is work for ballerina 0.991 and 1.0 first you have to import the time package
Then it will give the current date if you want to get in a format it will included the code
import ballerina/time;
To get current time
time:Time time = time:currentTime();
string standardTimeString = time:toString(time);
io:println("Current system time in ISO format: ", standardTimeString);
To format the time
string|error customTimeString = time:format(time, "yyyy-MM-dd-E");
if (customTimeString is string) {
io:println("Current system time in custom format: ", customTimeString);
}
y -Years
M -months
d -date
E -day
h -hour
m -Minuit
s -seconds
For Swan Lake Update 3 they seem to have removed the time:currentTime() function.
It seems they have replaced it with time:utcNow().
According to the ballerina documentation,
"The time:Utc is the tuple representation of the UTC. The UTC represents the number of seconds from a specified epoch. Here, the epoch is the UNIX epoch of 1970-01-01T00:00:00Z."
So you can convert this above tuple representation to RFC 3339 timestamp by using,
time:Utc currTime = time:utcNow();
string date = time:utcToString(currTime);
io:println(date);
Then you will get a result like below,
2023-01-14T17:04:15.639510400Z
Using ballerina time library you can convert to other different representations as well.

python - password expiry

I am trying to add a password expiry on to my web application. The goal is for the user to change their password every 30 days with a notification of how many days are left before they have to change their password. I am trying to calculate the amount of days left using :
enter code here
import datetime
today = datetime.date.today()
expire = 30
day = today.strftime(' %d ')
daysLeft = (expire - day)
print today.strftime('you have '+ daysLeft +' days to change your password')
but am having issues because I am trying to use an integer and a string in the one sum. Any help would be greatly appreciated.
if you want to convert a string to an integer, use this syntax :
daysLeft = (expire - int(day))
if you want to convert an integer to a string, you can use this syntax :
today.strftime('you have '+ str(daysLeft) +' days to change your password')
But as André Schild said in the comments, you probably don't want to do it this way. You could just print your message without calling strftime, if the information you want to be displayed is on the days remaining only.

Xively read data in Python

I have written a python 2.7 script to retrieve all my historical data from Xively.
Originally I wrote it in C#, and it works perfectly.
I am limiting the request to 6 hour blocks, to retrieve all stored data.
My version in Python is as follows:
requestString = 'http://api.xively.com/v2/feeds/41189/datastreams/0001.csv?key=YcfzZVxtXxxxxxxxxxxORnVu_dMQ&start=' + requestDate + '&duration=6hours&interval=0&per_page=1000' response = urllib2.urlopen(requestString).read()
The request date is in the correct format, I compared the full c# requestString version and the python one.
Using the above request, I only get 101 lines of data, which equates to a few minutes of results.
My suspicion is that it is the .read() function, it returns about 34k of characters which is far less than the c# version. I tried adding 100000 as an argument to the ad function, but no change in result.
Left another solution wrote in Python 2.7 too.
In my case, got data each 30 minutes because many sensors sent values every minute and Xively API has limited half hour of data to this sent frequency.
It's general module:
for day in datespan(start_datetime, end_datetime, deltatime): # loop increasing deltatime to star_datetime until finish
while(True): # assurance correct retrieval data
try:
response = urllib2.urlopen('https://api.xively.com/v2/feeds/'+str(feed)+'.csv?key='+apikey_xively+'&start='+ day.strftime("%Y-%m-%dT%H:%M:%SZ")+'&interval='+str(interval)+'&duration='+duration) # get data
break
except:
time.sleep(0.3)
raise # try again
cr = csv.reader(response) # return data in columns
print '.'
for row in cr:
if row[0] in id: # choose desired data
f.write(row[0]+","+row[1]+","+row[2]+"\n") # write "id,timestamp,value"
The full script you can find it here: https://github.com/CarlosRufo/scripts/blob/master/python/retrievalDataXively.py
Hope you might help, delighted to answer any questions :)

How do you remove seconds and milliseconds from a date time string in python

How I can convert a date in format "2013-03-15 05:14:51.327" to "2013-03-15 05:14", i.e. removing the seconds and milliseconds. I don't think there is way in Robot frame work. Please let me know if any one have a solution for this in python.
Try this (Thanks Blender!)
>>> date = "2013-03-15 05:14:51.327"
>>> newdate = date.rpartition(':')[0]
>>> print newdate
2013-03-15 05:14
In Robotframework the most straightforward way would be to user Split String From Right from the String library library:
${datestring}= Set Variable 2019-03-15 05:14:51.327
${parts}= Split String From Right ${datestring} : max_split=1
# parts is a list of two elements - everything before the last ":", and everything after it
# take the 1st element, it is what we're after
${no seconds}= Get From List ${parts} 0
Log ${no senods} # 2019-03-15 05:14