No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

81 líneas
2.6 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. import datetime
  25. # global values -- used for caching
  26. user_date_format = None
  27. dateformats = {
  28. 'yyyy-mm-dd': '%Y-%m-%d',
  29. 'mm/dd/yyyy': '%m/%d/%Y',
  30. 'mm-dd-yyyy': '%m-%d-%Y',
  31. "mm/dd/yy": "%m/%d/%y",
  32. 'dd-mmm-yyyy': '%d-%b-%Y', # numbers app format
  33. 'dd/mm/yyyy': '%d/%m/%Y',
  34. 'dd-mm-yyyy': '%d-%m-%Y',
  35. "dd/mm/yy": "%d/%m/%y",
  36. }
  37. def user_to_str(date, date_format=None):
  38. if not date: return date
  39. if not date_format:
  40. date_format = get_user_date_format()
  41. try:
  42. return datetime.datetime.strptime(date,
  43. dateformats[date_format]).strftime('%Y-%m-%d')
  44. except ValueError, e:
  45. raise ValueError, "Date %s must be in format %s" % (date, date_format)
  46. def parse_date(date):
  47. """tries to parse given date to system's format i.e. yyyy-mm-dd. returns a string"""
  48. parsed_date = None
  49. # why the sorting? checking should be done in a predictable order
  50. check_formats = [None] + sorted(dateformats.keys(),
  51. reverse=not get_user_date_format().startswith("dd"))
  52. for f in check_formats:
  53. try:
  54. parsed_date = user_to_str(date, f)
  55. if parsed_date:
  56. break
  57. except ValueError, e:
  58. pass
  59. if not parsed_date:
  60. raise Exception, """Cannot understand date - '%s'.
  61. Try formatting it like your default format - '%s'""" % \
  62. (date, get_user_date_format())
  63. return parsed_date
  64. def get_user_date_format():
  65. if not user_date_format:
  66. global user_date_format
  67. user_date_format = webnotes.conn.get_value("Control Panel", None, "date_format")
  68. return user_date_format