25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

90 lines
2.9 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 webnotes.defaults
  25. import datetime
  26. from webnotes.utils import get_datetime
  27. # global values -- used for caching
  28. user_date_format = None
  29. dateformats = {
  30. 'yyyy-mm-dd': '%Y-%m-%d',
  31. 'mm/dd/yyyy': '%m/%d/%Y',
  32. 'mm-dd-yyyy': '%m-%d-%Y',
  33. "mm/dd/yy": "%m/%d/%y",
  34. 'dd-mmm-yyyy': '%d-%b-%Y', # numbers app format
  35. 'dd/mm/yyyy': '%d/%m/%Y',
  36. 'dd-mm-yyyy': '%d-%m-%Y',
  37. "dd/mm/yy": "%d/%m/%y",
  38. }
  39. def user_to_str(date, date_format=None):
  40. if not date: return date
  41. if not date_format:
  42. date_format = get_user_date_format()
  43. try:
  44. return datetime.datetime.strptime(date,
  45. dateformats[date_format]).strftime('%Y-%m-%d')
  46. except ValueError, e:
  47. raise ValueError, "Date %s must be in format %s" % (date, date_format)
  48. def parse_date(date):
  49. """tries to parse given date to system's format i.e. yyyy-mm-dd. returns a string"""
  50. parsed_date = None
  51. # why the sorting? checking should be done in a predictable order
  52. check_formats = [None] + sorted(dateformats.keys(),
  53. reverse=not get_user_date_format().startswith("dd"))
  54. for f in check_formats:
  55. try:
  56. parsed_date = user_to_str(date, f)
  57. if parsed_date:
  58. break
  59. except ValueError, e:
  60. pass
  61. if not parsed_date:
  62. raise Exception, """Cannot understand date - '%s'.
  63. Try formatting it like your default format - '%s'""" % \
  64. (date, get_user_date_format())
  65. return parsed_date
  66. def get_user_date_format():
  67. global user_date_format
  68. if not user_date_format:
  69. user_date_format = webnotes.defaults.get_global_default("date_format")
  70. return user_date_format
  71. def datetime_in_user_format(date_time):
  72. if isinstance(date_time, basestring):
  73. date_time = get_datetime(date_time)
  74. from webnotes.utils import formatdate
  75. return formatdate(date_time.date()) + " " + date_time.strftime("%H:%M")