File: 03-date.py

package info (click to toggle)
python-pattern 2.6%2Bgit20180818-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 93,888 kB
  • sloc: python: 28,119; xml: 15,085; makefile: 194
file content (36 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from __future__ import print_function
from __future__ import unicode_literals

from builtins import str, bytes, dict, int

import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from pattern.db import date, time, NOW
from pattern.web import Bing, NEWS

# It is often useful to keep a date stamp for each row in the table.
# The pattern.db module's date() function can be used for this.
# It is a simple wrapper around Python's datetime.datetime class,
# with extra functionality to make it easy to parse or print it as a string.

print(date(NOW))
print(date())
print(date("2010-11-01 16:30", "%Y-%m-%d %H:%M"))
print(date("Nov 1, 2010", "%b %d, %Y"))
print(date("Nov 1, 2010", "%b %d, %Y", format="%d/%m/%Y"))
print("")

# All possible formatting options:
# http://docs.python.org/library/time.html#time.strftime

for r in Bing(license=None, language="en").search("today", type=NEWS):
    print(r.title)
    print(repr(r.date))  # Result.date is a string (e.g. we can't > <= += with the date).
    print(date(r.date))  # date() can parse any Result.date in the web module.
    print("")

d = date("4 november 2011")
d += time(days=2, hours=5)
print(d)