Giter VIP home page Giter VIP logo

strftime.org's Introduction

strftime.org's People

Contributors

chuckwoodraska avatar cxong avatar fraziern avatar gingerchew avatar mccutchen avatar moreati 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

strftime.org's Issues

Website is down

Not sure if you're aware that strftime.org is currently down. I'm willing to host it on S3 myself, if you'd grant me license to do so.

Add recipes

There are many gotchas with formatting time, getting right time/date/datetime object, and choosing right syntax for popular formats. It would help to have a preformatted copy-pasteable snippets. I'd personally need 2014-08-27 01:00:00.00000 in UTC, but other formats, such as full ISO 8601 format are useful too.

Python3?

Thank you for this great reference with easy to remember URL.

There are some useful new codes in Python3, like %G and %V for iso-year and iso-week.
Would you consider an overhaul?

Note about %z gotcha

This is a good opportunity to improve on the python.org docs. An issue I've seen is that %z does unexpected things on Windows (http://bugs.python.org/issue20010). Instead of returning +HHHH it returns a string name of the time zone (often with Unicode characters). Code that uses it is liable to break when run in Windows.

Include note [4] about %f, i.e. that it can be used to parse milliseconds

It is not immediately clear from the description of "%f", "Microsecond as a decimal number, zero-padded on the left", that it can be used to parse millisecond fields.

It is safe to use for parsing millisecond fields, as explained in note [4] in the docs: "When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right."

As you can see from e.g. https://stackoverflow.com/questions/698223/how-can-i-parse-a-time-string-containing-milliseconds-in-it-with-python and this comment: https://stackoverflow.com/questions/698223/how-can-i-parse-a-time-string-containing-milliseconds-in-it-with-python#comment46200797_698279 this is confusing.

It might help people in the future to get note [4] included in this page?

I can't see an easy way to do that in your build.py, so I haven't created a PR for you, sorry.

Dealing with leading 0's on Windows

I have Python 3.X installed on Windows 10. Just started learning and made it to the site looking at date representation. I got an error using the format placeholders. Attempting to use the format string '%B %-d, %-I:%M' netted me:

ValueError: Invalid format string

I figured I did something wrong but after looking at StackOverflow turns out I could have used # instead of -.

I do not know the specifics of why this is not working but I thought it would be worth mentioning somewhere that perhaps:

Note: If you using windows and encounter ValueError: Invalid format string when trying to remove leading 0's try using the ampersand in place of the hypen e.g '%B %#d, %#I:%M'

I see that string (platform specific) on the page and a link to the documentation but it was not obvious on that page what I had to do either :/

show how to call it

It would be wonderful if the website shows how to call the function in the first place:

>>> datetime.datetime(2013, 12, 25, 17, 15, 30).strftime('%Y-%m-%d %H:%M:%S')
'2013-12-25 17:15:30'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())  # right now in UTC
'2014-04-21 00:15:59'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())  # right now in local time
'2014-04-21 00:16:21'

throws new error when referring to latest Python 3 Library page

When I use the following Python 3 URL (https://docs.python.org/3/library/datetime.html) instead of the Python 2 URL (http://docs.python.org/2/library/datetime.html), I get the error message below. It seems unrelated to me to the version of the web page

Here's the error message for the source code below :

`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
28 for row in table.select('tbody > tr'):
29 tds = row.find_all('td')
---> 30 directive = tds[0].find('span').string
31 # we use getText() here because some of the meanings have extra markup
32 meaning = tds[1].getText().replace('\n', ' ')

AttributeError: 'NoneType' object has no attribute 'string'`

For the following source code :

`#!/usr/bin/env python3

""" Here's the same code but not executed inside a main() function."""

""" Found interesting enumeration of string formatting at https://strftime.org/

From the reference page's github module "build.py" located at
https://github.com/mccutchen/strftime.org"""

#I had to make a few modifications to get it to render the templated ouptut HTML on Windows 10 correctly.

import datetime
import sys
import urllib.request, urllib.error, urllib.parse

from bs4 import BeautifulSoup
import pystache

url = 'https://docs.python.org/3/library/datetime.html'
body = urllib.request.urlopen(url).read()
soup = BeautifulSoup(body, features="html.parser")

table = soup.find(id='strftime-and-strptime-behavior').find('table')
example_date = datetime.datetime(2013, 9, 30, 7, 6, 5)

directives = []
for row in table.select('tbody > tr'):
tds = row.find_all('td')
directive = tds[0].find('span').string
# we use getText() here because some of the meanings have extra markup
meaning = tds[1].getText().replace('\n', ' ')
example = example_date.strftime(directive)
directives.append({
'directive': directive,
'meaning': meaning,
'example': example
})

# Also add the non zero padded version for some of them
# http://stackoverflow.com/questions/28894172/why-does-d-or-e-remove-the-leading-space-or-zero

exclude = ['%f', '%y']
if 'zero-padded' in meaning and directive not in exclude:
    non_zero_padding_decimal = directive.replace("%", "%#")
    non_zero_padding_example = example_date.strftime(
        non_zero_padding_decimal
    )

    non_zero_padding_meaning = (
        meaning.replace("zero-padded", "") + " (Platform specific)")

    directives.append({
        'directive': non_zero_padding_decimal,
        'meaning': non_zero_padding_meaning,
        'example': non_zero_padding_example
    })

template_file = '.\strftime.org-master\templates\index.html.mustache'
template = open(template_file).read()
context = {
'example_date': str(example_date),
'example_date_repr': repr(example_date),
'directives': directives,
'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%d'),
}

Render the final context into the HTML template file

rendered_html = pystache.render(template, context)

create / open the output file for writing.

output_html = open("rendered_html.html", "wt")

Now write out the output file.

output_html.write(rendered_html)
output_html.close()

print(pystache.render(template, context))

import webbrowser

webbrowser.open('rendered_html.html')
webbrowser.open('https://docs.python.org/3/library/datetime.html')`

Adding Suffix to Day e.g 5th

#29
Hello @mccutchen . Thank you for sharing the link, but it does not contain what I am looking for. I meant to ask if strftime allows formatting a date with a suffix. Formatting examples like: May 5th, Jan 1st . I know it allows formatting like May 5, and Jan 1

Simplify by using Python f-string syntax

Now that Python 3.5 is EOL, using f-strings can help you avoid striftime() on all current versions of CPython.

>>> from datetime import datetime
>>> f"{datetime.now():%Y-%m-%d %H:%M}"
'2020-09-22 12:19'

Use xmas day as an date

The problem is that it can be a bit confusing re the order of the dat/month.

Classic example is 1/11 would be lst of November or 11th January

To make it easier for everyone at work, I always use christmas day as the date..
as there is not a 25th month, and it a familiar date...

Just a suggestion..
Pete

Add tags for easy searching

It would be great if there would be tags above the table to easily select all codes related to hours/days/etc.

Adding the position of a Day e.g (st, nd, rd, th)

Hello.
I would like to ask if the strftime module has a function that returns a date with its position . For example ;
02/12/2021 - 2nd
strftime.function_name('02/12/2021') # returns 2nd
and
04/11/2021 - 4th
strftime.function_name('04/12/2021') # returns 4th

add %F

The shorthand specifier for %Y-%m-%d

Include examples

Would you be interested in a pull request that adds examples of commonly used formats for strftime?

here's a version that runs on Windows 10.

I had to fix a few things to get this to run without error on Jupyter Notebook on Windows 10. Not sure exactly why the final output does not look exactly like the the website version. The CSS file is probably not being picked up correctly.

Don't have privileges to do pull request on this repo yet, so I cannot merge into main branch.

`#!/usr/bin/env python3

""" Here's the same code but not executed inside a main() function."""

Taken from the reference page's github module "build.py" located at https://github.com/mccutchen/strftime.org"""

#I had to make a few modifications to get it to render the templated ouptut HTML on Windows 10 correctly.

import datetime
import sys
import urllib.request, urllib.error, urllib.parse

from bs4 import BeautifulSoup
import pystache


url = 'http://docs.python.org/2/library/datetime.html'
body = urllib.request.urlopen(url).read()
soup = BeautifulSoup(body, features="html.parser")

table = soup.find(id='strftime-and-strptime-behavior').find('table')
example_date = datetime.datetime(2013, 9, 30, 7, 6, 5)

directives = []
for row in table.select('tbody > tr'):
    tds = row.find_all('td')
    directive = tds[0].find('span').string
    # we use getText() here because some of the meanings have extra markup
    meaning = tds[1].getText().replace('\n', ' ')
    example = example_date.strftime(directive)
    directives.append({
        'directive': directive,
        'meaning': meaning,
        'example': example
    })

    # Also add the non zero padded version for some of them
    # http://stackoverflow.com/questions/28894172/why-does-d-or-e-remove-the-leading-space-or-zero

    exclude = ['%f', '%y']
    if 'zero-padded' in meaning and directive not in exclude:
        non_zero_padding_decimal = directive.replace("%", "%#")
        non_zero_padding_example = example_date.strftime(
            non_zero_padding_decimal
        )

        non_zero_padding_meaning = (
            meaning.replace("zero-padded", "") + " (Platform specific)")

        directives.append({
            'directive': non_zero_padding_decimal,
            'meaning': non_zero_padding_meaning,
            'example': non_zero_padding_example
        })
    
template_file = '.\\strftime.org-master\\templates\\index.html.mustache'
template = open(template_file).read()
context = {
    'example_date': str(example_date),
    'example_date_repr': repr(example_date),
    'directives': directives,
    'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%d'),
}

# Render the final context into the HTML template file
rendered_html = pystache.render(template, context)

# create / open the output file for writing.
output_html = open("rendered_html.html", "wt")

# Now write out the output file.
output_html.write(rendered_html)
output_html.close()

print(pystache.render(template, context))

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.