time_get<> : I/P from cin is OK; I/P from an istringstream gives an error

I have used the time_get<> facet's get_date() method to read a date from the following sources:
(i) cin and
(ii) an istringstream based on a string s whose value has been obtained from getline(cin, s).

The I/P from cin works fine; the date is read properly and printed fine.
However, the I/P from the istringstream gives an error; the date isn't read properly; an error occurs.

Following is the code for the istringstream source: http://coliru.stacked-crooked.com/a/31704818a11d5629

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
vector<string> locales {"C"};


/// Get I/P from a string.
void IPFromStr()
{
    cout << "\nI/P from a string ... " << endl;
    
    /// For each locale name
    for (const auto& locs : locales)
    {
        cout << "locale name: " << locs << endl;

        try
        {
            /// Create the locale.
            locale loc {locs};

            /// Read date/time parts from a string.
            ReadDtPartFromStr(loc);
        }
        catch (const exception& e)
        {
            cerr << " Exception: " << e.what()
                 << endl << endl;
        }
    }
}


/// Read date/time parts from a string.
void ReadDtPartFromStr(locale& loc)
{
    /// Get the time_get<> facet.
    const time_get<char>& tg =
              use_facet<time_get<char>> (loc);

    /// I/P string variable for the read date part.
    string dtpart {};
    
    /// output arguments for the time_get<> facet
    struct tm d {};                             /// time
    ios_base::iostate err = ios_base::goodbit;  /// good
    
    getline(cin, dtpart);
    
    cout << " dtpart: " << dtpart << endl;
        
    /// Get an istringstream for the read date part
    istringstream isdtpart {dtpart};
        
    isdtpart.imbue(loc);
        
    istreambuf_iterator<char> frm(isdtpart), end;
    
    /// Read the date part.
    tg.get_date(frm, end,
                isdtpart,
                err,
                &d);

    /// Print the date read.            
    Print(err, d);
}



The Print() function is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// Print the date read.
void Print(ios_base::iostate& err, tm& d)
{
    if (err)
       cout << " error while reading input" << endl;
    else
       cout << " yyyy/mm/dd hh:mm:ss  : " 
            << d.tm_year + 1900 << '/'
            << d.tm_mon + 1     << '/'
            << d.tm_mday        << ' '
            << d.tm_hour        << ':'
            << d.tm_min         << ':'
            << d.tm_sec
            << endl;
}



Given the following cin input:

01/26/2018


, I get the following output:

I/P from a string ... 

locale name: C
 dtpart: 01/26/2018
 error while reading input


The same I/P works properly for similar functions that read directly from cin.

Why does this error occur when we read from an istringstream? Thanks.
Registered users can post here. Sign in or register to post.