1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
19 #include "timetypes.h"
20 #include "elementarytypes.h"
21 #include "environment.h"
22 #include "classtypes.h"
24 using namespace Shapes
;
27 Lang::ChronologicalTime::ChronologicalTime( time_t t
)
31 Lang::ChronologicalTime::~ChronologicalTime( )
34 RefCountPtr
< const Lang::Class
> Lang::ChronologicalTime::TypeID( new Lang::SystemFinalClass( strrefdup( "Time" ) ) );
35 TYPEINFOIMPL( ChronologicalTime
);
38 Lang::ChronologicalTime::temporary_localtime( ) const
40 return localtime( & t_
);
43 Kernel::VariableHandle
44 Lang::ChronologicalTime::getField( const char * fieldID
, const RefCountPtr
< const Lang::Value
> & selfRef
) const
46 const struct tm
* tmp
= localtime( & t_
);
47 if( strcmp( fieldID
, "year" ) == 0 )
49 return Helpers::newValHandle( new Lang::Integer( 1900 + tmp
->tm_year
) );
51 if( strcmp( fieldID
, "month" ) == 0 )
53 return Helpers::newValHandle( new Lang::Integer( 1 + tmp
->tm_mon
) );
55 if( strcmp( fieldID
, "dayOfMonth" ) == 0 )
57 return Helpers::newValHandle( new Lang::Integer( 1 + tmp
->tm_mday
) );
59 if( strcmp( fieldID
, "hour" ) == 0 )
61 return Helpers::newValHandle( new Lang::Integer( tmp
->tm_hour
) );
63 if( strcmp( fieldID
, "minute" ) == 0 )
65 return Helpers::newValHandle( new Lang::Integer( tmp
->tm_min
) );
67 if( strcmp( fieldID
, "second" ) == 0 )
69 return Helpers::newValHandle( new Lang::Integer( tmp
->tm_sec
) );
71 if( strcmp( fieldID
, "dayOfWeek" ) == 0 )
73 return Helpers::newValHandle( new Lang::Integer( tmp
->tm_wday
) );
75 throw Exceptions::NonExistentMember( getTypeName( ), fieldID
);
79 Lang::ChronologicalTime::show( std::ostream
& os
) const
81 const struct tm
* tmp
= localtime( & t_
);
82 const size_t BUF_SIZE
= 30;
84 strftime( reinterpret_cast< char * >( & buf
), BUF_SIZE
, "%Y-%m-%d %H:%M:%S", tmp
);