Giter Site home page Giter Site logo

hustcc / timeago Goto Github PK

View Code? Open in Web Editor NEW
223.0 8.0 37.0 120 KB

:hourglass: Simple library used to format datetime with `*** time ago` statement. eg: "3 hours ago".

Home Page: http://timeago.org

License: MIT License

Python 99.65% Shell 0.09% JavaScript 0.26%
timeago datetime format-datetime python multi-language

timeago's Introduction

timeago

A very simple python lib, used to format datetime with *** time ago statement. Javascript version here. timeago.js.

Build Status PyPi Status Python Versions

Such as:

just now
12 seconds ago
3 minutes ago
2 hours ago
24 days ago
6 months ago
2 years ago

in 12 seconds
in 3 minutes
in 2 hours
in 24 days
in 6 months
in 2 years

For other languages see below.

Install

pip install timeago

Usage & Example

# -*- coding: utf-8 -*-
import timeago, datetime

now = datetime.datetime.now() + datetime.timedelta(seconds = 60 * 3.4)

date = datetime.datetime.now()

# locale
print (timeago.format(date, now, 'zh_CN')) # will print `3分钟前`

# input datetime
print (timeago.format(date, now)) # will print 3 minutes ago

# input timedelta
print (timeago.format(datetime.timedelta(seconds = 60 * 3.4))) # will print 3 minutes ago

# input date, auto add time(0, 0, 0)
print (timeago.format(datetime.date(2016, 5, 27), now))

# input datetime formatted string
print (timeago.format('2016-05-27 12:12:03', '2016-05-27 12:12:12')) # will print just now

# inverse two parameters
print (timeago.format('2016-05-27 12:12:12', '2016-05-27 12:12:03')) # will print a while

Method & Parameter

only one API format.

Three parameters of method format:

  • date: the parameter which will be formatted, must be instance of datetime / timedelta or datetime formatted string.
  • now: reference time, must be instance of datetime or datetime formatted string.
  • locale: the locale code, default en.

Locale

At the time we're speaking, following locale are available:

  • ar
  • bg
  • ca
  • da
  • de
  • el
  • en
  • en_short
  • es
  • eu
  • fa_IR
  • fi
  • fr
  • gl
  • guj_IN
  • he
  • hu
  • in_BG
  • in_HI
  • in_ID
  • is
  • it
  • ja
  • ko
  • lt
  • ml
  • my
  • nb_NO
  • nl
  • nn_NO
  • pl
  • pt_BR
  • pt_PT
  • ro
  • ru
  • sk
  • sv_SE
  • ta
  • th
  • tr
  • uk
  • vi
  • zh_CN
  • zh_TW

Localization

  1. Fork the project
  2. Create a locale python script called [name_of_your_locale].py following the existing other locales.
  3. Add the name of your locale in the Readme (both in MD and in RST) to keep it updated (alphabetically).
  4. Add test case following the english model
  5. Create the Pull Request.

Notes

For complicated plurals, you can take example on the PL :flag-pl: locale here

timeago's People

Contributors

0xflotus avatar aashrut avatar ajphukan avatar avrong avatar hakashun avatar hustcc avatar iteanjd123 avatar jenca-adam avatar justinwhite avatar lolobosse avatar meyt avatar naglis avatar nmb10 avatar seluj78 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

timeago's Issues

Tag the source

It would be very helpful if you could tag releases as well again. This would enable distributions who want to get the source from GitHub instead of PyPI.

Thanks

Example code does not work!

○ python time.py
Traceback (most recent call last):
  File "time.py", line 6, in <module>
    print (timeago.format(date, now, locale='zh_CN')) # will print <removed because encoding>
NameError: name 'date' is not defined

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 9, in <module>
    print (timeago.format(date, now)) # will print 3 minutes ago
NameError: name 'date' is not defined

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 12, in <module>
    print (timeago.format(datetime.timedelta(seconds = 60 * 3.4))) # will print 3 minutes ago
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 25, in format
    diff_seconds = date.total_seconds()
AttributeError: 'datetime.timedelta' object has no attribute 'total_seconds'

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 15, in <module>
    print (timeago.format(datetime.date(2016, 5, 27), now))
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 33, in format
    raise ParameterUnvalid('the parameter `date` should be datetime / timedelta, or datetime formated string.')
timeago.excepts.ParameterUnvalid: 'the parameter `date` should be datetime / timedelta, or datetime formated string.'

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 18, in <module>
    print (timeago.format(datetime.date(2016, 5, 27), now))
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 33, in format
    raise ParameterUnvalid('the parameter `date` should be datetime / timedelta, or datetime formated string.')
timeago.excepts.ParameterUnvalid: 'the parameter `date` should be datetime / timedelta, or datetime formated string.'

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 21, in <module>
    print (timeago.format('2016-05-27 12:12:12', '2016-05-27 12:12:03')) # will print just now
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 37, in format
    diff_seconds = (now - date).total_seconds()
AttributeError: 'datetime.timedelta' object has no attribute 'total_seconds'

.

○ python --version
Python 2.6.6

timeago 1.0.12 refuses to install on Windows 10

C:\WINDOWS\system32>pip install timeago --upgrade
Collecting timeago
  Using cached https://files.pythonhosted.org/packages/70/c7/3a1ccd78d542e592534f4c74e63ead33d5f11977b0e26d061e022ed285ed/timeago-1.0.12.tar.gz
    ERROR: Command errored out with exit status 1:
     command: 'c:\python37\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\AlexFlipnote\\AppData\\Local\\Temp\\pip-install-6k63qzau\\timeago\\setup.py'"'"'; __file__='"'"'C:\\Users\\AlexFlipnote\\AppData\\Local\\Temp\\pip-install-6k63qzau\\timeago\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\AlexFlipnote\AppData\Local\Temp\pip-install-6k63qzau\timeago\pip-egg-info'
         cwd: C:\Users\AlexFlipnote\AppData\Local\Temp\pip-install-6k63qzau\timeago\
    Complete output (9 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\AlexFlipnote\AppData\Local\Temp\pip-install-6k63qzau\timeago\setup.py", line 12, in <module>
        long_description = readme(),
      File "C:\Users\AlexFlipnote\AppData\Local\Temp\pip-install-6k63qzau\timeago\setup.py", line 7, in readme
        return f.read()
      File "c:\python37\lib\encodings\cp1252.py", line 23, in decode
        return codecs.charmap_decode(input,self.errors,decoding_table)[0]
    UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 805: character maps to <undefined>
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Current Python version: Python 3.7.4
Tried versions: 3.7.6, 3.8.1
OS: Windows 10 Pro

Note: It does work to install it on my Debian 8.10 server, as a side-note.

typo in README?

README says:

print (timeago.format('2016-05-27 12:12:12', '2016-05-27 12:12:03')) # will print just now

but I got "a while" as the output

I installed timeago by pip install timeago, and timeago.__version__ is '1.0.7'

Remove "in X hours ago" ??

Hi, How do I remove "IN X HOURS AGO", I only want it to show "X hours/days/minutes ago" and not "in X hours ago"
It does work, but on some instances it shows "3 hours ago" and in some other instances it shows "in 3 hours"...
Using Python..

`

    now = datetime.datetime.now() + datetime.timedelta(seconds=60 * 3.4)

    date = datetime.datetime.now()

    time_ago = (timeago.format(streamer_channel_info['last_seen']))`

Please include the future

Please include the future so that the answer is “in X” if date > now, corresponding to “X ago” if date < now. And perhaps a “just now” if date == now.

Locales not imported with PyInstaller

Example:

import time
import timeago

timestamp = time.time() - 30
print(timeago.format(timestamp))

PyInstaller command:

$ pyinstaller -F -c --noupx -i NONE main.py

Output:

Traceback (most recent call last):
  File "timeago\locales\__init__.py", line 25, in timeago_template
ModuleNotFoundError: No module named 'timeago.locales.en'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print(timeago.format(timestamp))
  File "timeago\__init__.py", line 80, in format
  File "timeago\locales\__init__.py", line 29, in timeago_template
[28604] Failed to execute script 'main' due to unhandled exception!

Python 3.10.1
PyInstaller 4.8
timeago 1.0.15

The PyInstaller documentation provides examples of how to add hooks for hidden imports. There is also an example package repo.

A temporary workaround is to add each locale module manually using --hidden-import 'timeago.locales.<locale>'

I haven't worked with PyInstaller hooks, but it looks easy to implement. I can try to make a PR if needed when I look at it further.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.