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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*
* Date and time functions of libewf .net managed wrapper
*
* Copyright (C) 2006-2017, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* This software is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma managed( push, off )
#include <common.h>
#include <types.h>
#pragma managed( pop )
#include "ewf.net_datetime.h"
#using <mscorlib.dll>
using namespace System;
namespace EWF {
/* Creates a new datetime object from a filetime
* Returns a DateTime object if successful or nullptr on error
*/
System::DateTime^ DateTime::FromFiletime( System::UInt64 filetime )
{
System::DateTime^ date_time_object = nullptr;
System::String^ error_string = nullptr;
System::String^ function = "DateTime::FromFiletime";
System::Int32 days_in_century = 0;
System::Int32 days_in_year = 0;
System::Int32 year = 0;
System::Int32 day = 0;
System::Int32 days_in_month = 0;
System::Int32 hours = 0;
System::Int32 micro_seconds = 0;
System::Int32 minutes = 0;
System::Int32 month = 0;
System::Int32 seconds = 0;
/* The timestamp is in units of 100 nano seconds correct the value to seconds
*/
micro_seconds = ( filetime % 10000000 ) / 10;
filetime /= 10000000;
/* There are 60 seconds in a minute correct the value to minutes
*/
seconds = filetime % 60;
filetime /= 60;
/* There are 60 minutes in an hour correct the value to hours
*/
minutes = filetime % 60;
filetime /= 60;
/* There are 24 hours in a day correct the value to days
*/
hours = filetime % 24;
filetime /= 24;
/* Add 1 day to compensate that Jan 1 1601 is represented as 0
*/
filetime += 1;
/* Determine the number of years starting at '1 Jan 1601 00:00:00'
* correct the value to days within the year
*/
year = 1601;
if( filetime >= 36159 )
{
year = 1700;
filetime -= 36159;
}
while( filetime > 0 )
{
if( ( year % 400 ) == 0 )
{
days_in_century = 36525;
}
else
{
days_in_century = 36524;
}
if( filetime <= days_in_century )
{
break;
}
filetime -= days_in_century;
year += 100;
}
while( filetime > 0 )
{
/* Check for a leap year
* The year is ( ( dividable by 4 ) and ( not dividable by 100 ) ) or ( dividable by 400 )
*/
if( ( ( ( year % 4 ) == 0 )
&& ( ( year % 100 ) != 0 ) )
|| ( ( year % 400 ) == 0 ) )
{
days_in_year = 366;
}
else
{
days_in_year = 365;
}
if( filetime <= (System::UInt64) days_in_year )
{
break;
}
filetime -= days_in_year;
year += 1;
}
/* Determine the month correct the value to days within the month
*/
month = 1;
while( filetime > 0 )
{
/* February (2)
*/
if( month == 2 )
{
if( ( ( ( year % 4 ) == 0 )
&& ( ( year % 100 ) != 0 ) )
|| ( ( year % 400 ) == 0 ) )
{
days_in_month = 29;
}
else
{
days_in_month = 28;
}
}
/* April (4), June (6), September (9), November (11)
*/
else if( ( month == 4 )
|| ( month == 6 )
|| ( month == 9 )
|| ( month == 11 ) )
{
days_in_month = 30;
}
/* Januari (1), March (3), May (5), July (7), August (8), October (10), December (12)
*/
else if( ( month == 1 )
|| ( month == 3 )
|| ( month == 5 )
|| ( month == 7 )
|| ( month == 8 )
|| ( month == 10 )
|| ( month == 12 ) )
{
days_in_month = 31;
}
/* This should never happen, but just in case
*/
else
{
error_string = gcnew System::String(
"ewf.net " + function + ": unsupported month: " + month + "." );
throw gcnew System::Exception(
error_string );
}
if( filetime <= (System::UInt64) days_in_month )
{
break;
}
filetime -= days_in_month;
month += 1;
}
/* Determine the day
*/
day = (System::Int32) filetime;
date_time_object = gcnew System::DateTime(
year,
month,
day,
hours,
minutes,
seconds,
micro_seconds );
return( date_time_object );
}
/* Creates a new datetime object from a POSIX time
* Returns a DateTime object if successful or nullptr on error
*/
System::DateTime^ DateTime::FromPOSIXTime( System::UInt32 posix_time )
{
System::DateTime^ date_time_object = nullptr;
System::String^ error_string = nullptr;
System::String^ function = "DateTime::FromPOSIXTime";
System::Int32 days_in_year = 0;
System::Int32 year = 0;
System::Int32 day = 0;
System::Int32 days_in_month = 0;
System::Int32 hours = 0;
System::Int32 minutes = 0;
System::Int32 month = 0;
System::Int32 seconds = 0;
/* There are 60 seconds in a minute correct the value to minutes
*/
seconds = posix_time % 60;
posix_time /= 60;
/* There are 60 minutes in an hour correct the value to hours
*/
minutes = posix_time % 60;
posix_time /= 60;
/* There are 24 hours in a day correct the value to days
*/
hours = posix_time % 24;
posix_time /= 24;
/* Add 1 day to compensate that Jan 1 1601 is represented as 0
*/
posix_time += 1;
/* Determine the number of years starting at '1 Jan 1970 00:00:00'
* correct the value to days within the year
*/
year = 1970;
if( posix_time >= 10957 )
{
year = 2000;
posix_time -= 10957;
}
while( posix_time > 0 )
{
/* Check for a leap year
* The year is ( ( dividable by 4 ) and ( not dividable by 100 ) ) or ( dividable by 400 )
*/
if( ( ( ( year % 4 ) == 0 )
&& ( ( year % 100 ) != 0 ) )
|| ( ( year % 400 ) == 0 ) )
{
days_in_year = 366;
}
else
{
days_in_year = 365;
}
if( posix_time <= (System::UInt64) days_in_year )
{
break;
}
posix_time -= days_in_year;
year += 1;
}
/* Determine the month correct the value to days within the month
*/
month = 1;
while( posix_time > 0 )
{
/* February (2)
*/
if( month == 2 )
{
if( ( ( ( year % 4 ) == 0 )
&& ( ( year % 100 ) != 0 ) )
|| ( ( year % 400 ) == 0 ) )
{
days_in_month = 29;
}
else
{
days_in_month = 28;
}
}
/* April (4), June (6), September (9), November (11)
*/
else if( ( month == 4 )
|| ( month == 6 )
|| ( month == 9 )
|| ( month == 11 ) )
{
days_in_month = 30;
}
/* Januari (1), March (3), May (5), July (7), August (8), October (10), December (12)
*/
else if( ( month == 1 )
|| ( month == 3 )
|| ( month == 5 )
|| ( month == 7 )
|| ( month == 8 )
|| ( month == 10 )
|| ( month == 12 ) )
{
days_in_month = 31;
}
/* This should never happen, but just in case
*/
else
{
error_string = gcnew System::String(
"ewf.net " + function + ": unsupported month: " + month + "." );
throw gcnew System::Exception(
error_string );
}
if( posix_time <= (System::UInt64) days_in_month )
{
break;
}
posix_time -= days_in_month;
month += 1;
}
/* Determine the day
*/
day = (System::Int32) posix_time;
date_time_object = gcnew System::DateTime(
year,
month,
day,
hours,
minutes,
seconds,
0 );
return( date_time_object );
}
} // namespace EWF
|