summaryrefslogtreecommitdiffstats
path: root/content/renderer/date_time_formatter.h
blob: 40d02d65c4e0b64ee82b9e8f4c3ac4110f1a2094 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_DATE_TIME_FORMATTER_H_
#define CONTENT_RENDERER_DATE_TIME_FORMATTER_H_

#include <string>

#include "base/basictypes.h"
#include "content/common/content_export.h"
#include "third_party/icu/public/common/unicode/unistr.h"
#include "third_party/icu/public/i18n/unicode/gregocal.h"
#include "ui/base/ime/text_input_type.h"

namespace WebKit {
struct WebDateTimeChooserParams;
}  // namespace WebKit

namespace content {

// Converts between a text string representing a date/time and
// a set of year/month/day/hour/minute/second and vice versa.
// It is timezone agnostic.
class CONTENT_EXPORT DateTimeFormatter {
 public:
  explicit DateTimeFormatter(const WebKit::WebDateTimeChooserParams& source);
  DateTimeFormatter(
      ui::TextInputType type,
      int year, int month, int day, int hour, int minute, int second);
  ~DateTimeFormatter();

  int GetYear() const;
  int GetMonth() const;
  int GetDay() const;
  int GetHour() const;
  int GetMinute() const;
  int GetSecond() const;
  ui::TextInputType GetType() const;
  const std::string& GetFormattedValue() const;

 private:
  void CreatePatternMap();
  bool ParseValues();
  const std::string FormatString() const;
  int ExtractValue(
      const icu::Calendar* calendar, UCalendarDateFields value) const;
  void ExtractType(const WebKit::WebDateTimeChooserParams& source);
  void ClearAll();

  ui::TextInputType type_;
  icu::UnicodeString patterns_[ui::TEXT_INPUT_TYPE_MAX + 1];
  int year_;
  int month_;
  int day_;
  int hour_;
  int minute_;
  int second_;
  const icu::UnicodeString* pattern_;
  std::string formatted_string_;

  DISALLOW_COPY_AND_ASSIGN(DateTimeFormatter);
};

}  // namespace content

#endif  // CONTENT_RENDERER_DATE_TIME_FORMATTER_H_