Created
March 20, 2019 06:47
-
-
Save anhtran/36def4ce5405eebeffe1480a6ad855f2 to your computer and use it in GitHub Desktop.
create datetime from time ago string (Vietnamese)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arrow | |
def create_datetime_from_timeago(text, timezone='Asia/Saigon'): | |
""" | |
Create datetime object from timeago string. | |
Author: anhtran <anhtran.net> | |
:param str text: | |
:param str timezone: | |
Example: | |
5 phút trước | |
1 giờ trước | |
2 ngày trước | |
4 tháng trước | |
""" | |
s = re.search(r'^(\d+)\s(phút|giờ|ngày|tháng)\strước$', text) | |
if s: | |
n = int(s.group(1)) | |
if 'phút' in text: | |
return arrow.now().shift(minutes=-n).to(timezone).datetime | |
elif 'giờ' in text: | |
return arrow.now().shift(hours=-n).to(timezone).datetime | |
elif 'ngày' in text: | |
return arrow.now().shift(days=-n).to(timezone).datetime | |
elif 'tháng' in text: | |
return arrow.now().shift(months=-n).to(timezone).datetime | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment