I am new to hbase and currently I am using hbase-1.2.6. I did the connection to hbase using python script by using happybase package. my question is :
can someone please let me know how to decode timestamp value which is automatically inserted whenever we put any records in table?
1.what is the exact interpretation of timestamp value in hbase?
2.can we convert this timestamp value to yy-mm-dd-hh:mm:ss format?
The timestamp value is the number of milliseconds since the epoch (January 1, 1970 UTC). You can use the python datetime module to manipulate it. Example:
from datetime import datetime as dt
print (dt.fromtimestamp(1511356398000 / 1000))
Output:
2017-11-22 07:13:18
The result is a datetime object in my local time zone. (Central USA) The datetime.fromtimestamp method wants a floating point value that is the time in seconds since the epoch, so divide the time in milliseconds by 1000.
Here is the datetime module reference.
Related
Suddenly ran into issue with this timestamp (which was hidden in JSON object):
-180835200000
Tried to extract standard timestamp with:
dt.strftime(dt.fromtimestamp(json.loads(res.text)['suggestions'][0]['data']['state']['actuality_date']/1000),'%Y-%m-%d')
But failed since the value is negative.
How do I convert this negative TIMESTAMP to human date ?
The given time is a POSIX time (aka Epoch time or Unix time). It represents the number of seconds that have elapsed since the 00:00:00 UTC on 1 January 1970, excluding leap seconds. Unix time
For your application, if the timestamp has a negative sign in front then it represents a time point before January 1, 1970. Make sure the time is in integer format and the following result is tested in Python 3.7.12;
from datetime import datetime as dt
dt.strftime(dt.fromtimestamp(-180835200000/1000),'%Y-%m-%d')
Outputs (human readable)
1964-04-09
If you believe that the negative sign is a mistake in data, you can simply use abs() function which will output 1975-09-25.
I am downloading a json file containing timestamp using python . But the timestamp i am getting is below format
`2021-04-01T21:43:52.757Z`
Want to convert into normal timestamp (CST Hrs). I also see that the time is increased by 4 hours when i compare the report manually.
`4/1/2021 5:43:53 PM`
The above hours is 4 hrs less when i compare with json file entry. Please advise me.
You need to use python's datetime module to handle this. The Z in the string actually means time zone 0 or UTC time which is 6 hours ahead of CST not 4:
import datetime
date_object = datetime.datetime.strptime(
"2021-04-01T21:43:52.757Z", "%Y-%m-%dT%H:%M:%S.%fZ"
)
date_object = date_object - datetime.timedelta(hours=6)
print(
f"{date_object.month}/{date_object.day}/{date_object.year} {date_object.strftime('%I:%M:%S %p')}"
)
Which will give this output:
4/1/2021 03:43:52 PM
have to use an f string if you want non zero padded dates because its not available in datetime according to the docs
You can use pytz module to deal with time zones directly and not hardcode them if you want. Or if you are on python 3.9 you can use timezone objects to create timezones yourself
As part of my new work I need to convert one existing java class to a python one.
person.setDob(String.valueOf(person.getDateOfBirth().getTime()));
Please see the above snippet here how to fetch time in milliseconds from date object in python,
Hope I can use datetime.datetime for this purpose. Please help.
To get a date string with milliseconds (3 decimal places behind seconds), use this:
from datetime import datetime
print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
OUTPUT 2018-10-04 10:18:32.926
%f is displaying milliseconds
To get milliseconds in python, use the below code.
import datetime
print('Datetime in milliscond using now()',datetime.datetime.now())
print('Datetime in milliscond using utcfromtimestamp()', datetime.datetime.utcfromtimestamp(0))
output looks like below
Datetime in milliscond using now() 2019-03-11 17:34:28.290409
Datetime in milliscond using now() 1970-01-01 00:00:00
I have a DB with time entries formatted as follow:
2018-11-05T08:58:00Z
I'm trying to generate SQL queries to compare "now()" with the time in the DB to determine which row(s) to return.
I'm battling to "convert" my local time (now()) to an equivalent time format so that I can use < or > operations against the DB values.
Additionally, I am not sure if the problem has two parts. The example fo the time above is not in a "Datetime" field in MySQL but stored simply as TEXT, leaving me to suspect that I would need to "convert" the DB entries into another format first?
The following code, using the datetime module, works for me (tested in Python 3.6):
import datetime
value = "2018-11-05T08:58:00Z"
dt = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
# Result is: datetime.datetime(2018, 11, 5, 8, 58)
This will convert your string values to datetime instances, which you can then compare to now(). The values that get created should be naive (meaning they have no associated timezone information).
However, if you are sure that now() for you is not UTC (aka Zulu time), you may need to do a conversion. This could be possible if, for example, you are using Django's timezone.now() and your configured timezone is something other than UTC. In this case, I might convert the result of now() to UTC, so you only have to convert one value. The pytz module can easily handle this kind of thing.
Check this :
import time
time = time.strftime('%Y-%m-%dT%H:%M:%SZ')
print(time)
I wanted to convert the UNIX time into local date and time. I am getting the UNIX timestamp value from my server but when I convert the UNIX using these set of code I get a time which is 1 hour 30 min less than the actual time. But when I take the raw timestamp data and check in the online UNIX to local date and time converter I get the correct time.
import datetime
time_local = datetime.datetime.fromtimestamp(1502705627085/1e3)
print time_local
is your timezone correct on your system ?
From the python datetime documentation :
classmethod datetime.fromtimestamp(timestamp[, tz])
Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.
If you cannot change your system's timezone, you can specify a tz as explained in the datetime module documentation.
import datetime
import tzlocal
time_local = datetime.datetime.fromtimestamp(1502705627085/1e3, tzlocal.get_localzone())
print time_local