I've tried to convert datetime string into datetime of an SArray (uses C++ booster library), but it does not seem to understand the %p format specifier. http://www.boost.org/doc/libs/1_43_0/doc/html/date_time/date_time_io.html
This documentation says specifiers marked with ! do not currently work for input.
Does that mean that you cannot parse anything with pm or PM?
I was able to get the string-to-datetime conversion to work by making two small changes:
Use %I for the hour on a 12-hour clock (%H is for a 24 hour clock).
Use %P (upper case) for the AM/PM flag.
Here's what works for me:
sf = gl.SFrame({'date': ['2015-11-06 02:12:42 pm',
'2015-11-05 03:43:11 pm']})
sf['date2'] = sf['date'].str_to_datetime('%Y-%m-%d %I:%M:%S %p')
Related
I have a datetime in format = '2020-05-01'. I want the output to be 2020-05-01 00:00:00.
I use this simple code to achieve this but I am missing the time part.
datetime.strptime(month, "%Y-%m-%d")
I am using python2. Does anyone have any idea on how to achieve this. This seems like a really simple problem but for some reason I am not able to achieve it.
Your one line of code already generates a datetime value which should be set to midnight (the default value for no explicit time component). If you want to view this data with its time component, then use strftime with an appropriate format mask:
month = '2020-05-01'
dt = datetime.strptime(month, "%Y-%m-%d")
dt_out = dt.strftime("%Y-%m-%d %H:%M:%S")
print(dt_out)
This prints:
2020-05-01 00:00:00
x = datetime.strptime(month, "%Y-%m-%d")
x.strftime(month, "%Y-%m-%d %H:%M:%S")
Source:
https://www.programiz.com/python-programming/datetime/strftime
I'm trying to investigate the Python time striptime method to decompose a time represented as 11:49:57.74. The standard %H, %M, %S are able to decompose the hour , minute , second. However, since the data is a string ( which is taken in python pandas column as datatype object, the Milliseconds after the decimal second is left uninterpreted. Hence, I get an error. Could someone please advise how to parse the example so that the seconds and microseconds are correctly interpreted from the time string ?
I would then use them to find the time delta between two time stamps.
I don't know if I had correctly understood your question.
So, to convert that string time to datetime and calculate the timedelta between two times you need to do as follow:
timedelta = str() #declare an empty string where save the timedelta
my_string = '11:49:57.74' # first example time
another_example_time = '13:49:57.74' #second example time, invented by me for the example
first_time = datetime.strptime(my_string, "%H:%M:%S.%f") # extract the first time
second_time = datetime.strptime(another_example_time , "%H:%M:%S.%f") # extract the second time
#calculate the time delta
if(first_time > second_time):
timedelta = first_time - second_time
else:
timedelta = second_time - first_time
print "The timedelta between %s and %s is: %s" % (first_time, second_time, timedelta)
Here obviusly you don't have any date, so the datetime library as default use 1900-01-01 as you can see in the result of the print:
The timedelta between 1900-01-01 11:49:57.740000 and 1900-01-01 13:49:57.740000 is: 2:00:00
I hope this solution is what you need. Next time provide a little bit more information please, or share an example with the code that you have tried to write.
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.
I have converted some sensor data into CSV format. The data contains a timestamp attribute that is divided into two parts as follows:
measurement_timestamp_begin:
seconds: 3733032665
fractions: 3056174174
I need to convert this into standard UNIX timestamp format. I saw several posts for doing the conversion but each method receives a single argument. I don't understand the fraction part. Does it mean seconds.fraction? e.g. 3733032665.3056174174.
Following code converts above mentioned timestamp format to standard UNIX timestamp format.
#!/usr/bin/env python
import datetime, time
unix_epoch = datetime.date(*time.gmtime(0)[0:3])
ntp_epoch = datetime.date(1900, 1, 1)
ntp_delta = (unix_epoch - ntp_epoch).days * 24 * 3600
def ntp_to_unix_time(date):
return (date - ntp_delta)
print datetime.datetime.fromtimestamp(int(ntp_to_unix_time(3733032665.3056174174))).strftime('%Y-%m-%d %H:%M:%S')
I am trying to convert UTC timestamp to simple binary encoded message.
Would like to achieve what is mentioned in example here.
Binary Encoding Example
The following timestamp:
UTC timestamp 14:17:22 Friday, October 4, 2024
is expressed in binary code (nanoseconds since Unix epoch) this way:
007420bf8838fb17 (8 bytes in nanoseconds since Unix epoch synced to a master clock to microsecond accuracy.
What I have done so far is,
import struct
from datetime import datetime
dt = datetime(2024, 10, 4, 14, 17, 22, 0)
timestamp = (dt - datetime(1970, 1, 1)).total_seconds() * 1000000000
utc_timestamp = struct.pack('d', timestamp)
Output I see on CLI is '\xf4\x02\x82\xa1E\xfb\xb7C' but, as per example in shared link, expected is 007420bf8838fb17
TL;DR: I think there's either an error in linked example, or the description of the representation is incorrect or incomplete.
import struct
from datetime import datetime, timedelta, timezone
utc_tz = timezone(timedelta(0))
t = datetime(2024, 10, 4, 14, 17, 22, 0, utc_tz)
nanos = int(t.timestamp()) * 1_000_000_000
print(hex(timestamp))
This gives the result of 0x17fb45a18202f400, not the quoted 0x17fb3888bf297400 (as presented, I think it's a little-endian byte string, so this value has the order of bytes reversed).
The quoted answer, converted to decimal seconds (dividing by 1_000_000_000) is 1728037042.0005898. That value obviously has a sub-second quantity, which the source datetime does NOT have.
Decoding the seconds component of the quoted answer:
import time
answer = 0x17fb3888bf297400
secs = int(answer / 1_000_000_000)
print(time.gmtime(secs))
gives:
time.struct_time(tm_year=2024, tm_mon=10, tm_mday=4, tm_hour=10, tm_min=17, tm_sec=22, tm_wday=4, tm_yday=278, tm_isdst=0)
which looks almost correct, save that the hour component is 4 hours earlier. So ... my guess is that this is actually a US/Eastern timestamp (not UTC), and contains a sub-second component of 5898ns, and the example is misleading.
If the schema you're using is encoding timestamps as a 64-bit number of nanoseconds since the Unix epoch (midnight 1 January 1970), then I think your example code has only one error: use format <q (or <Q) rather than d, and you'll get a little-endian 64-bit integer as required. You will also need to convert the timestamp to an integer to have struct.pack() accept it.