| 1359 | ## Generic DateField ########################################################## |
| 1360 | |
| 1361 | >>> from django.contrib.localflavor.generic.forms import * |
| 1362 | |
| 1363 | A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where |
| 1364 | appropriate. |
| 1365 | |
| 1366 | >>> import datetime |
| 1367 | >>> f = DateField() |
| 1368 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 1369 | datetime.date(2006, 10, 25) |
| 1370 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 1371 | datetime.date(2006, 10, 25) |
| 1372 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) |
| 1373 | datetime.date(2006, 10, 25) |
| 1374 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) |
| 1375 | datetime.date(2006, 10, 25) |
| 1376 | >>> f.clean('2006-10-25') |
| 1377 | datetime.date(2006, 10, 25) |
| 1378 | >>> f.clean('25/10/2006') |
| 1379 | datetime.date(2006, 10, 25) |
| 1380 | >>> f.clean('25/10/06') |
| 1381 | datetime.date(2006, 10, 25) |
| 1382 | >>> f.clean('Oct 25 2006') |
| 1383 | datetime.date(2006, 10, 25) |
| 1384 | >>> f.clean('October 25 2006') |
| 1385 | datetime.date(2006, 10, 25) |
| 1386 | >>> f.clean('October 25, 2006') |
| 1387 | datetime.date(2006, 10, 25) |
| 1388 | >>> f.clean('25 October 2006') |
| 1389 | datetime.date(2006, 10, 25) |
| 1390 | >>> f.clean('25 October, 2006') |
| 1391 | datetime.date(2006, 10, 25) |
| 1392 | >>> f.clean('2006-4-31') |
| 1393 | Traceback (most recent call last): |
| 1394 | ... |
| 1395 | ValidationError: [u'Enter a valid date.'] |
| 1396 | >>> f.clean('200a-10-25') |
| 1397 | Traceback (most recent call last): |
| 1398 | ... |
| 1399 | ValidationError: [u'Enter a valid date.'] |
| 1400 | >>> f.clean('10/25/06') |
| 1401 | Traceback (most recent call last): |
| 1402 | ... |
| 1403 | ValidationError: [u'Enter a valid date.'] |
| 1404 | >>> f.clean(None) |
| 1405 | Traceback (most recent call last): |
| 1406 | ... |
| 1407 | ValidationError: [u'This field is required.'] |
| 1408 | |
| 1409 | >>> f = DateField(required=False) |
| 1410 | >>> f.clean(None) |
| 1411 | >>> repr(f.clean(None)) |
| 1412 | 'None' |
| 1413 | >>> f.clean('') |
| 1414 | >>> repr(f.clean('')) |
| 1415 | 'None' |
| 1416 | |
| 1417 | DateField accepts an optional input_formats parameter: |
| 1418 | >>> f = DateField(input_formats=['%Y %m %d']) |
| 1419 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 1420 | datetime.date(2006, 10, 25) |
| 1421 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 1422 | datetime.date(2006, 10, 25) |
| 1423 | >>> f.clean('2006 10 25') |
| 1424 | datetime.date(2006, 10, 25) |
| 1425 | |
| 1426 | The input_formats parameter overrides all default input formats, |
| 1427 | so the default formats won't work unless you specify them: |
| 1428 | >>> f.clean('2006-10-25') |
| 1429 | Traceback (most recent call last): |
| 1430 | ... |
| 1431 | ValidationError: [u'Enter a valid date.'] |
| 1432 | >>> f.clean('25/10/2006') |
| 1433 | Traceback (most recent call last): |
| 1434 | ... |
| 1435 | ValidationError: [u'Enter a valid date.'] |
| 1436 | >>> f.clean('25/10/06') |
| 1437 | Traceback (most recent call last): |
| 1438 | ... |
| 1439 | ValidationError: [u'Enter a valid date.'] |
| 1440 | |
| 1441 | ## Generic DateTimeField ###################################################### |
| 1442 | |
| 1443 | A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where |
| 1444 | appropriate. |
| 1445 | |
| 1446 | >>> import datetime |
| 1447 | >>> f = DateTimeField() |
| 1448 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 1449 | datetime.datetime(2006, 10, 25, 0, 0) |
| 1450 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 1451 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1452 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) |
| 1453 | datetime.datetime(2006, 10, 25, 14, 30, 59) |
| 1454 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) |
| 1455 | datetime.datetime(2006, 10, 25, 14, 30, 59, 200) |
| 1456 | >>> f.clean('2006-10-25 14:30:45') |
| 1457 | datetime.datetime(2006, 10, 25, 14, 30, 45) |
| 1458 | >>> f.clean('2006-10-25 14:30:00') |
| 1459 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1460 | >>> f.clean('2006-10-25 14:30') |
| 1461 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1462 | >>> f.clean('2006-10-25') |
| 1463 | datetime.datetime(2006, 10, 25, 0, 0) |
| 1464 | >>> f.clean('25/10/2006 14:30:45') |
| 1465 | datetime.datetime(2006, 10, 25, 14, 30, 45) |
| 1466 | >>> f.clean('25/10/2006 14:30:00') |
| 1467 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1468 | >>> f.clean('25/10/2006 14:30') |
| 1469 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1470 | >>> f.clean('25/10/2006') |
| 1471 | datetime.datetime(2006, 10, 25, 0, 0) |
| 1472 | >>> f.clean('25/10/06 14:30:45') |
| 1473 | datetime.datetime(2006, 10, 25, 14, 30, 45) |
| 1474 | >>> f.clean('25/10/06 14:30:00') |
| 1475 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1476 | >>> f.clean('25/10/06 14:30') |
| 1477 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1478 | >>> f.clean('25/10/06') |
| 1479 | datetime.datetime(2006, 10, 25, 0, 0) |
| 1480 | >>> f.clean('hello') |
| 1481 | Traceback (most recent call last): |
| 1482 | ... |
| 1483 | ValidationError: [u'Enter a valid date/time.'] |
| 1484 | >>> f.clean('2006-10-25 4:30 p.m.') |
| 1485 | Traceback (most recent call last): |
| 1486 | ... |
| 1487 | ValidationError: [u'Enter a valid date/time.'] |
| 1488 | |
| 1489 | DateField accepts an optional input_formats parameter: |
| 1490 | >>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) |
| 1491 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 1492 | datetime.datetime(2006, 10, 25, 0, 0) |
| 1493 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 1494 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1495 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) |
| 1496 | datetime.datetime(2006, 10, 25, 14, 30, 59) |
| 1497 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) |
| 1498 | datetime.datetime(2006, 10, 25, 14, 30, 59, 200) |
| 1499 | >>> f.clean('2006 10 25 2:30 PM') |
| 1500 | datetime.datetime(2006, 10, 25, 14, 30) |
| 1501 | |
| 1502 | The input_formats parameter overrides all default input formats, |
| 1503 | so the default formats won't work unless you specify them: |
| 1504 | >>> f.clean('2006-10-25 14:30:45') |
| 1505 | Traceback (most recent call last): |
| 1506 | ... |
| 1507 | ValidationError: [u'Enter a valid date/time.'] |
| 1508 | |
| 1509 | >>> f = DateTimeField(required=False) |
| 1510 | >>> f.clean(None) |
| 1511 | >>> repr(f.clean(None)) |
| 1512 | 'None' |
| 1513 | >>> f.clean('') |
| 1514 | >>> repr(f.clean('')) |
| 1515 | 'None' |
| 1516 | |