I am in the process of "re-developing" an application that was started in PHP sometime ago. The developer involved has left the company quite awhile ago and all efforts to contact him have fallen to the wayside.
I do not need help converting the whole site as its quite a simple application. However we are integrating with an API service (who's documentation is rather poor) and I am having problems understanding their dateTime strings.
Here is the excerpt (the only bit) on how they use there datetimes.
" The datetime is expressed as a number derived from an algorithm using the day month year hour minutes and seconds as follows:
date=year<<20;
date|=month<<16;
date|=day<<11;
date|=hour<<6;
date|=minute;
To unpack the date from a given number is as follows:
year=(date & 0xfff00000) >> 20;
month=(date & 0x000f0000) >> 16;
day=(date & 0x0000f800) >> 11;
hour=(date & 0x000007c0) >> 6;
minute=(date & 0x0000003f);
"
Now comes my question. The developer (and this is a working example) has created the following PHP Function that converts a timestamp to the required format. I am unsure if bitwise timestamp is a "generic algorithm".
I have tried looking around and couldnt find anything.
/**
* Converts a unixtime stamp to a bitwise timestamp
*
* @access public
* @param mixed $unixtime
* @return void
*/
function convert_unixtime_to_bitwise($unixtime)
{
/*$year = date('Y', $unixtime);
$month = date('m', $unixtime);
$day = date('j', $unixtime);
$hour = date('G', $unixtime);
$min = date('i', $unixtime);*/
$year = date('Y', $unixtime);
$month = date('m', $unixtime);
$day = date('j', $unixtime);
$hour = date('G', $unixtime);
$min = date('i', $unixtime);
$date = 0;
$date = $year << 20;
$date |= $month << 16;
$date |= $day <<11;
$date |= $hour <<6;
$date |= $min;
return $date;
}
/**
* Converts a bitwise datestamp to a standard unixtime stamp
*
* @access public
* @param mixed $timestamp
* @return void
*/
function convert_bitwise_to_unixtime($timestamp)
{
$dateline = array();
$dateline['year'] = ($timestamp & 0xfff00000) >> 20;
$dateline['month'] =($timestamp & 0x000f0000) >> 16;
$dateline['day'] = ($timestamp & 0x0000f800) >> 11;
$dateline['hour'] = ($timestamp & 0x000007c0) >> 6;
$dateline['min'] = ($timestamp & 0x0000003f);
return mktime($dateline['hour'], $dateline['min'], 0, $dateline['month'], $dateline['day'], $dateline['year']);
}
Can anyone help me convert this to .Net in the simplest of fashions. Ideally i would write an extension method to the datetime object to return a "bitwise?" object and an extension methods to turn a bitwise timestamp into a datetime timestamp. I would ideally end up with something similar to bel..
public static class Extensions
{
public static BitWiseDateStamp ToBitWiseDateStamp(this DateTime timeStamp)
{
return BitWiseDateStamp.New(timeStamp);
}
}
public class BitWiseDateStamp
{
private string _value;
public string Value
{
get { return _value; }
set { _value = value; }
}
public static BitWiseDateStamp New(DateTime date)
{
var b = new BitWiseDateStamp();
var bitWiseStr = string.Empty;
//= Convert to bitwise string format...
b.Value = bitWiseStr;
return b;
}
public override string ToString()
{
return this._value;
}
public DateTime ToDateTime()
{
var date = DateTime.MinValue;
var dateStr = string.Empty;
//== Convert bitwise string to date string respresenation
DateTime.TryParse(dateStr, out date);
return date;
}
}
I really appreciate any help anyone can provide.
Cheers,
Nico
Perhaps I'm missing something, but won't this do?
public class BitwiseDateStamp
{
private readonly int _value;
public BitwiseDateStamp(DateTime dt)
{
this._value = dt.Year << 20;
this._value |= dt.Month << 16;
this._value |= dt.Day << 11;
this._value |= dt.Hour << 6;
this._value |= dt.Minute;
}
public BitwiseDateStamp() : this(DateTime.Now)
{
}
public DateTime ToDateTime()
{
int year = this._value >> 20;
int month = (this._value & 0x000f0000) >> 16;
int day = (this._value & 0x0000f800) >> 11;
int hour = (this._value & 0x000007c0) >> 6;
int minute = this._value & 0x0000003f;
return new DateTime(year, month, day, hour, minute, 0);
}
public override string ToString()
{
return this._value.ToString();
}
}
and
public static class DateTimeExtensions
{
public static BitwiseDateStamp ToBitwiseDateStamp(this DateTime dt)
{
return new BitwiseDateStamp(dt);
}
}
I don't see a need in BitWiseDateStamp class. That could be implemented like that:
public static class Extensions
{
public static ulong ToDateStamp(this DateTime dt)
{
return (dt.Year << 20 | dt.Month << 16 | dt.Day << 11 | dt.Hour << 6 | dt.Minute);
}
public static DateTime FromDateStamp(this ulong stamp)
{
return new DateTime((stamp & 0xfff00000) >> 20,
(stamp & 0x000f0000) >> 16,
(stamp & 0x0000f800) >> 11,
(stamp & 0x000007c0) >> 6,
(stamp & 0x0000003f),
0 // 0 seconds
);
}
}
User contributions licensed under CC BY-SA 3.0