BVB Source Codes

mars Show greg_facet.hpp Source code

Return Download mars: download greg_facet.hpp Source code - Download mars Source code - Type:.hpp
  1. #ifndef GREGORIAN_FACET_HPP___
  2. #define GREGORIAN_FACET_HPP___
  3.  
  4. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  5.  * Use, modification and distribution is subject to the
  6.  * Boost Software License, Version 1.0. (See accompanying
  7.  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  8.  * Author: Jeff Garland, Bart Garst
  9.  * $Date$
  10.  */
  11.  
  12. #include "boost/date_time/gregorian/gregorian_types.hpp"
  13. #include "boost/date_time/date_formatting_locales.hpp" // sets BOOST_DATE_TIME_NO_LOCALE
  14. #include "boost/date_time/gregorian/parsers.hpp"
  15. #include "boost/core/no_exceptions_support.hpp"
  16.  
  17. //This file is basically commented out if locales are not supported
  18. #ifndef BOOST_DATE_TIME_NO_LOCALE
  19.  
  20. #include <string>
  21. #include <memory>
  22. #include <locale>
  23. #include <iostream>
  24. #include <exception>
  25.  
  26. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost {
  27. namespace gregorian {
  28.  
  29.   //! Configuration of the output facet template
  30.   struct greg_facet_config
  31.   {
  32.     typedef mars_boost::gregorian::greg_month month_type;
  33.     typedef mars_boost::date_time::special_values special_value_enum;
  34.     typedef mars_boost::gregorian::months_of_year month_enum;
  35.     typedef mars_boost::date_time::weekdays weekday_enum;
  36.   };
  37.  
  38. #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
  39.   //! Create the base facet type for gregorian::date
  40.   typedef mars_boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
  41.  
  42.   //! ostream operator for gregorian::date
  43.   /*! Uses the date facet to determine various output parameters including:
  44.    *  - string values for the month (eg: Jan, Feb, Mar) (default: English)
  45.    *  - string values for special values (eg: not-a-date-time) (default: English)
  46.    *  - selection of long, short strings, or numerical month representation (default: short string)
  47.    *  - month day year order (default yyyy-mmm-dd)
  48.    */
  49.   template <class charT, class traits>
  50.   inline
  51.   std::basic_ostream<charT, traits>&
  52.   operator<<(std::basic_ostream<charT, traits>& os, const date& d)
  53.   {
  54.     typedef mars_boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
  55.     typedef mars_boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
  56.     greg_ostream_formatter::date_put(d, os);
  57.     return os;
  58.   }
  59.  
  60.   //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
  61.   /*! Uses the date facet to determine output string as well as selection of long or short strings.
  62.    *  Default if no facet is installed is to output a 2 wide numeric value for the month
  63.    *  eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
  64.    */
  65.   template <class charT, class traits>
  66.   inline
  67.   std::basic_ostream<charT, traits>&
  68.   operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
  69.   {
  70.     typedef mars_boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
  71.     typedef mars_boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
  72.     std::locale locale = os.getloc();
  73.     if (std::has_facet<facet_def>(locale)) {
  74.       const facet_def& f = std::use_facet<facet_def>(locale);
  75.       greg_month_formatter::format_month(m, os, f);
  76.  
  77.     }
  78.     else { //default to numeric
  79.       charT fill_char = '0';
  80.       os  << std::setw(2) << std::setfill(fill_char) << m.as_number();
  81.     }
  82.  
  83.     return os;
  84.   }
  85.  
  86.   //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
  87.   /*! Uses the date facet to determine output string as well as selection of long or short string.
  88.    *  Default if no facet is installed is to output a 3 char english string for the
  89.    *  day of the week.
  90.    */
  91.   template <class charT, class traits>
  92.   inline
  93.   std::basic_ostream<charT, traits>&
  94.   operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
  95.   {
  96.     typedef mars_boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
  97.     typedef mars_boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
  98.     std::locale locale = os.getloc();
  99.     if (std::has_facet<facet_def>(locale)) {
  100.       const facet_def& f = std::use_facet<facet_def>(locale);
  101.       greg_weekday_formatter::format_weekday(wd, os, f, true);
  102.     }
  103.     else { //default to short English string eg: Sun, Mon, Tue, Wed...
  104.       os  << wd.as_short_string();
  105.     }
  106.  
  107.     return os;
  108.   }
  109.  
  110.   //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
  111.   /*! Uses the date facet to determine output string as well as selection of long
  112.    *  or short string fr dates.
  113.    *  Default if no facet is installed is to output a 3 char english string for the
  114.    *  day of the week.
  115.    */
  116.   template <class charT, class traits>
  117.   inline
  118.   std::basic_ostream<charT, traits>&
  119.   operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
  120.   {
  121.     os << '['; //TODO: facet or manipulator for periods?
  122.     os << dp.begin();
  123.     os << '/'; //TODO: facet or manipulator for periods?
  124.     os << dp.last();
  125.     os << ']';
  126.     return os;
  127.   }
  128.  
  129.   template <class charT, class traits>
  130.   inline
  131.   std::basic_ostream<charT, traits>&
  132.   operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
  133.   {
  134.     //os << dd.days();
  135.     os << dd.get_rep();
  136.     return os;
  137.   }
  138.  
  139.   //! operator<< for gregorian::partial_date. Output: "Jan 1"
  140.   template <class charT, class traits>
  141.   inline
  142.   std::basic_ostream<charT, traits>&
  143.   operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
  144.   {
  145.     os << std::setw(2) << std::setfill('0') << pd.day() << ' '
  146.        << pd.month().as_short_string() ;
  147.     return os;
  148.   }
  149.  
  150.   //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
  151.   template <class charT, class traits>
  152.   inline
  153.   std::basic_ostream<charT, traits>&
  154.   operator<<(std::basic_ostream<charT, traits>& os,
  155.              const nth_kday_of_month& nkd)
  156.   {
  157.     os << nkd.nth_week_as_str() << ' '
  158.        << nkd.day_of_week() << " of "
  159.        << nkd.month().as_short_string() ;
  160.     return os;
  161.   }
  162.  
  163.   //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
  164.   template <class charT, class traits>
  165.   inline
  166.   std::basic_ostream<charT, traits>&
  167.   operator<<(std::basic_ostream<charT, traits>& os,
  168.              const first_kday_of_month& fkd)
  169.   {
  170.     os << "first " << fkd.day_of_week() << " of "
  171.        << fkd.month().as_short_string() ;
  172.     return os;
  173.   }
  174.  
  175.   //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
  176.   template <class charT, class traits>
  177.   inline
  178.   std::basic_ostream<charT, traits>&
  179.   operator<<(std::basic_ostream<charT, traits>& os,
  180.              const last_kday_of_month& lkd)
  181.   {
  182.     os << "last " << lkd.day_of_week() << " of "
  183.        << lkd.month().as_short_string() ;
  184.     return os;
  185.   }
  186.  
  187.   //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
  188.   template <class charT, class traits>
  189.   inline
  190.   std::basic_ostream<charT, traits>&
  191.   operator<<(std::basic_ostream<charT, traits>& os,
  192.              const first_kday_after& fka)
  193.   {
  194.     os << fka.day_of_week() << " after";
  195.     return os;
  196.   }
  197.  
  198.   //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
  199.   template <class charT, class traits>
  200.   inline
  201.   std::basic_ostream<charT, traits>&
  202.   operator<<(std::basic_ostream<charT, traits>& os,
  203.              const first_kday_before& fkb)
  204.   {
  205.     os << fkb.day_of_week() << " before";
  206.     return os;
  207.   }
  208. #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
  209.   /**************** Input Streaming ******************/
  210.  
  211. #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
  212.   //! operator>> for gregorian::date
  213.   template<class charT>
  214.   inline
  215.   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
  216.   {
  217.     std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
  218.     d = from_stream(beg, eos);
  219.     return is;
  220.   }
  221. #endif // BOOST_NO_STD_ITERATOR_TRAITS
  222.  
  223.   //! operator>> for gregorian::date_duration
  224.   template<class charT>
  225.   inline
  226.   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
  227.                                         date_duration& dd)
  228.   {
  229.     long v;
  230.     is >> v;
  231.     dd = date_duration(v);
  232.     return is;
  233.   }
  234.  
  235.   //! operator>> for gregorian::date_period
  236.   template<class charT>
  237.   inline
  238.   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
  239.                                         date_period& dp)
  240.   {
  241.     std::basic_string<charT> s;
  242.     is >> s;
  243.     dp = date_time::from_simple_string_type<date>(s);
  244.     return is;
  245.   }
  246.  
  247.   //! generates a locale with the set of gregorian name-strings of type char*
  248.   BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
  249.  
  250.   //! Returns a pointer to a facet with a default set of names (English)
  251.   /* Necessary in the event an exception is thrown from op>> for
  252.    * weekday or month. See comments in those functions for more info */
  253.   BOOST_DATE_TIME_DECL mars_boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
  254.  
  255. #ifndef BOOST_NO_STD_WSTRING
  256.   //! generates a locale with the set of gregorian name-strings of type wchar_t*
  257.   BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
  258.   //! Returns a pointer to a facet with a default set of names (English)
  259.   /* Necessary in the event an exception is thrown from op>> for
  260.    * weekday or month. See comments in those functions for more info */
  261.   BOOST_DATE_TIME_DECL mars_boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
  262. #endif // BOOST_NO_STD_WSTRING
  263.  
  264.   //! operator>> for gregorian::greg_month - throws exception if invalid month given
  265.   template<class charT>
  266.   inline
  267.   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
  268.   {
  269.     typedef mars_boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
  270.  
  271.     std::basic_string<charT> s;
  272.     is >> s;
  273.    
  274.     if(!std::has_facet<facet_def>(is.getloc())) {
  275.       std::locale loc = is.getloc();
  276.       charT a = '\0';
  277.       is.imbue(generate_locale(loc, a));
  278.     }
  279.  
  280.     short num = 0;
  281.  
  282.     BOOST_TRY{
  283.       const facet_def& f = std::use_facet<facet_def>(is.getloc());
  284.       num = date_time::find_match(f.get_short_month_names(),
  285.                                   f.get_long_month_names(),
  286.                                   (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
  287.                                                            // which is needed by find_match
  288.     }
  289.     /* bad_cast will be thrown if the desired facet is not accessible
  290.      * so we can generate the facet. This has the drawback of using english
  291.      * names as a default. */
  292.     BOOST_CATCH(std::bad_cast&){
  293.       charT a = '\0';
  294.       std::auto_ptr< const facet_def > f(create_facet_def(a));
  295.       num = date_time::find_match(f->get_short_month_names(),
  296.                                   f->get_long_month_names(),
  297.                                   (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
  298.                                                            // which is needed by find_match
  299.     }BOOST_CATCH_END
  300.    
  301.     ++num; // months numbered 1-12
  302.     m = greg_month(num);
  303.  
  304.     return is;
  305.   }
  306.  
  307.   //! operator>> for gregorian::greg_weekday  - throws exception if invalid weekday given
  308.   template<class charT>
  309.   inline
  310.   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
  311.   {
  312.     typedef mars_boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
  313.  
  314.     std::basic_string<charT> s;
  315.     is >> s;
  316.  
  317.     if(!std::has_facet<facet_def>(is.getloc())) {
  318.       std::locale loc = is.getloc();
  319.       charT a = '\0';
  320.       is.imbue(generate_locale(loc, a));
  321.     }
  322.  
  323.     short num = 0;
  324.     BOOST_TRY{
  325.       const facet_def& f = std::use_facet<facet_def>(is.getloc());
  326.       num = date_time::find_match(f.get_short_weekday_names(),
  327.                                   f.get_long_weekday_names(),
  328.                                   (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
  329.                                                                  // to form the array size which is needed by find_match
  330.     }
  331.     /* bad_cast will be thrown if the desired facet is not accessible
  332.      * so we can generate the facet. This has the drawback of using english
  333.      * names as a default. */
  334.     BOOST_CATCH(std::bad_cast&){
  335.       charT a = '\0';
  336.       std::auto_ptr< const facet_def > f(create_facet_def(a));
  337.       num = date_time::find_match(f->get_short_weekday_names(),
  338.                                   f->get_long_weekday_names(),
  339.                                   (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
  340.                                                                  // to form the array size which is needed by find_match
  341.     } BOOST_CATCH_END
  342.    
  343.     wd = greg_weekday(num); // weekdays numbered 0-6
  344.     return is;
  345.   }
  346.  
  347. } } //namespace gregorian
  348.  
  349. #endif  
  350.    
  351.    
  352. #endif
  353.  
  354.  
downloadgreg_facet.hpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top