473,765 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary Date Time decode

4 New Member
All,

I was wondering if someone could point me in the right direction to decode the following information.
I am working on a tool writen in C and one of the items in the file I am decoding is a timestamp in this format.
I had started looking at a bitfield structure, but I am wondering if there is a better / more portable solution.

Hex Value in the file: 9D AF 79 C0
Binary breakdown:
1001(9) 1101(D) 1010(A) 1111(F) 0111(7) 1001(9) 1100(C) 0000(0)

1001(9) 11011(27) 01011(11) 110111(55) 1(+) 00111(7) 000000(0)
Decoded Value: 09/27 11:55 +07:00

Format:

These parameters indicate the time when the file was opened, according to the following binary format:
  • The first four binary bits indicate the month (1 .. 12), according to the CGF"s local time zone;
  • The next five binary bits contain the date (1 :: 31), according to the CGF"s local time zone;
  • The next five binary bits contain the hour (0 .. 23), according to the CGF"s local time zone;
  • The next six binary bits contain the minute (0 .. 59), according to the CGF"s local time zone;
  • The next bit indicates the sign of the local time differential from UTC (bit set to '1' expresses '+' or bit set to
  • The next five binary bits contain the hour (0 .. 23) deviation of the local time towards UTC, according to
  • The next six binary bits contain the minute (0 .. 59) deviation of the local time towards UTC, according to
Feb 26 '07 #1
9 7444
DeMan
1,806 Top Contributor
Have you gotten anywhere with it yourself so far? Please post your progress.

This looks like it might need to use some bitshifts (which means fun for all the family), or at least some masking.
Feb 26 '07 #2
RedSon
5,000 Recognized Expert Expert
I would agree with the masking but I'm not sure what you are hoping to accomplish in terms of portability.
Feb 26 '07 #3
flanagak
4 New Member
Wow you guys are fast ^_^.

As for portability I was just concerned, after reading about bitfiled structurs that I may run into problems using this tool on other machines. All the systems that would use it should be x86 intel based systems so maybe it is not an issue.

As for where I have gotten so far.
Expand|Select|Wrap|Line Numbers
  1. struct{
  2.     unsigned month : 4;
  3.     unsigned day : 5;
  4.     unsigned hour : 5;
  5.     unsigned minute : 6;
  6.     unsigned diff : 1;
  7.     unsigned dhour : 5;
  8.     unsigned dmin : 6;
  9. } fTimestamp;
  10.  
Is the Structure I created in the headder file.

Expand|Select|Wrap|Line Numbers
  1. int memread(int len, FILE *in){
  2.     unsigned char *buf; /* buffer */
  3.     size_t result; /* Number of bytes read */
  4.     int i;
  5.     unsigned int val;
  6.  
  7.     if (len != 0){
  8.         /* Allocate memory to store bytes read*/
  9.         buf = ( unsigned char*) malloc (len);
  10.         if (buf == NULL) {fputs ("Memory error",stderr); exit (2);}
  11.  
  12.         /* Read bytes into the buffer. Result holds number of bytes read */
  13.         result = fread(buf,1,len,in);
  14.  
  15.         /* Display the value of each bit in hex and return 4 byte int value */
  16.         for (i=0;i<result;i++){
  17.             printf("The value of byte %d = %02X \n",i+1, buf[i]);
  18.             if (result == 1) {
  19.                 val = 1677721600;
  20.                 val = (val * 256);
  21.                 val += buf[i];
  22.             } else if (result == 2){
  23.                 if (i == 0){
  24.                     val = 3429105664;
  25.                 }
  26.                 val = (val * 256);
  27.                 val += buf[i];
  28.             } else {
  29.                 val = (val * 256);
  30.                 val += buf[i];
  31.             }
  32.         }
  33.  
  34.         /* Cleanup */
  35.         free(buf);
  36.     } else {
  37.         val = 0;
  38.     }
  39.  
  40.     return val;
  41. }
  42.  
Is the function I wotre to read the file in chucks based on specs for the file I was given.

Expand|Select|Wrap|Line Numbers
  1.     file_length = memread(fLength,fp);
  2.     printf("\nFile Length: %d \n", file_length);
  3.     header_length = memread(hLength,fp);
  4.     printf("Header Length: %d \n", header_length);
  5.     high_version_id = memread(hVersion,fp);
  6.     printf("High Version: %d \n", high_version_id);
  7.     low_version_id = memread(lVersion,fp);
  8.     printf("Low Version: %d \n", low_version_id);
  9.     file_open_timestamp = memread(fOpenTime,fp);
  10.     printf("File Open Timestamp: %u \n", file_open_timestamp);
  11.  
This is a few of the calling statemnts to the memread function.
The length of each item is defined as a constatnt in the header file.
Currently I am just printing the returned value to the screen.
In the end I will be writing all the values out to a MySQL load file.

The test I have done so far with the bitfiled values have showed me that whatever I pass to it it returs the binary value of the last n binary bits.
So for fTimestamp.mont h if I assign it 22 (10110) fTimestamp returns a value of 6 (0110). Showing me that whatever I pass it assigns the last 4 bits.

The reason for that test is that Im a bit stupmed how to break up the data to pass to the structure. Like fTimestamp.day is 5 bits binary, which from my 1st post would be the 4 bits from D (1101) and the 1st bit from A (1010) for a result of 27 (11011).

So I guess the big roadblock for me is how to read the stream in a way to produce the correct values.


Thanks for everyones input and suggestions
Feb 26 '07 #4
DeMan
1,806 Top Contributor
You can mask certain values away....
If you have a bit string of length 16, say,: 1100 1101 0101 1100
and you AND (&) it with 0000 0000 1111 0000, say, you get rid of the values you have 0 in your mask, and keep those that are one. You could do this for 5 bits int your time stamp, so that you isolate a particular 5.

Once you have (and because we know the position int the bitstring), you can right (?) shift the value back as far as it needs. If you store it in a character variable (8 bits) you can easily get the value you require......
Feb 26 '07 #5
DeMan
1,806 Top Contributor
I probably should have added, that none of this requires you to explicitly work at the bit level......

0000 0000 1111 0000 is an int with value 240(i think), and likewise any mask that you make, can be expressed as an integer......
Feb 26 '07 #6
flanagak
4 New Member
Ok so with the data from above.
The int value of the timestamp is 2645522880 or 100111011010111 101111001110000 00 in binary.

So to get the first part of the timestamp ( the month) I would replace the last 28 bits with 0's, blanking out eveything but the 1st 4 and the shift it right to make those the last 4 bits?

Also for the second element ( the day) I would mask out the 1st 4 and the last 23 with 0's, then shift right to amke those the last 5 bits?
Feb 26 '07 #7
DeMan
1,806 Top Contributor
Spot on. (take care with the bitshift for the leftmost value.....)


(As a total Aside, FYI) You could actually do the shifting before the masking. Since you often need to use the same sizes eg 5, you may be able to create standardised masks to extract the 5 bits from 8. I would discourage you from taking this approach, though, as it involves casting big numbers into small numbers. whcih can have unexpected behaviour
Feb 26 '07 #8
flanagak
4 New Member
WORKS LIKE A CHARM..... Thank you
Here is what I ended up with and it works perfectly.

Expand|Select|Wrap|Line Numbers
  1.     file_open_timestamp = memread(fOpenTime,fp);
  2.     printf("File Open Timestamp: %u \n", file_open_timestamp);
  3.  
  4.     fTimestamp.month = ((file_open_timestamp & 0xf0000000) >> 28);
  5.     fTimestamp.day = ((file_open_timestamp & 0x0f800000) >> 23);
  6.     fTimestamp.hour = ((file_open_timestamp & 0x007C0000) >> 18);
  7.     fTimestamp.minute = ((file_open_timestamp & 0x0003f000) >> 12);
  8.     fTimestamp.diff = ((file_open_timestamp & 0x00000800) >> 11);
  9.     fTimestamp.dhour = ((file_open_timestamp & 0x000007C0) >> 6);
  10.     fTimestamp.dmin = (file_open_timestamp & 0x0000003f);
  11.  
  12.     printf(" Month: %d \n",fTimestamp.month);
  13.     printf(" Day: %d \n",fTimestamp.day);
  14.     printf(" Hour: %d \n",fTimestamp.hour);
  15.     printf(" Minute: %d \n",fTimestamp.minute);
  16.     if (fTimestamp.diff == 1){
  17.         printf(" Diff: + \n");
  18.     } else {
  19.         printf(" Diff: - \n");
  20.     }
  21.     printf(" dHour: %d \n",fTimestamp.dhour);
  22.     printf(" dMin: %d \n",fTimestamp.dmin);
  23.  
Here is the ouput from the 1st post.
File Open Timestamp: 2645522880
Month: 9
Day: 27
Hour: 11
Minute: 55
Diff: +
dHour: 7
dMin: 0
Feb 26 '07 #9
DeMan
1,806 Top Contributor
Beautiful....
Feb 27 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

14
3779
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with databases, arrays, etc. but have not worked with binary data files. http://www.performancecentral.net/C...3Loop/3Loop.php click on "Download Performance".
1
1948
by: fishboy | last post by:
Howdy, I'm in middle of a personal project. Eventually it will download multipart binary attachments and look for missing parts on other servers. And so far I've got it to walk a newsgroup and download and decode single part binaries. I thought I'd post the code and see what people think. I'd appreciate any feedback. It's my first program with generators and I'm worried I'm making this twice and hard as it needs to be.
0
1868
by: fishboy | last post by:
Howdy, Sorry if this is a double post. First try seemed to go into hyperspace. I'm working on a personal project. It's going to be a multipart binary attachment downloader that will search alternate servers for missing pieces. This is the working code so far. It will walk a newsgroup and download and decode all the single part attachments. Just change server,user,password,group at the bottom to something less
5
2310
by: Gord | last post by:
Many scripts and calendars call client side system time in order to make presentations. However, the client's time may be improperly set, if set at all, and/or the relevant time may be from another time zone. It would be of great value if the time of some central source could be drawn, in the form of a ".js" file, into the script to avoid dependence on the client's date/time.
11
1812
by: Andrew Neiderer | last post by:
I am sort of new to XML and SVG. So if this has been addressed before please be patient and/or just point me to a URL. I have written a Java application which generates lots (150 x 150) of Scaleable Vector Graphics (SVG), which is a 2D XML application, <rect>s. The problem is that it takes about 10 seconds to display the frame. I need to approach real-time display if possible. So I thought a binary representation of the data would...
8
9532
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that contains the following three floating-point numbers: 1.0 2.0 3.0
1
4919
by: Andrey | last post by:
Hi I donno if this is the right place to ask for this question, anyway.... Is it possible to pack binary data into simplejson? d={} d = xxxxxx d = xxxxxx d = xxxxx
18
9013
by: saykenari | last post by:
Hello Friends I have a problem date format written as DD/MM/YYYY. Date already save into a file as binary. I don't know which technique should be used. Sample date as following a. 31/09/2009 in binary in the file written as 4f 75 25 00 b. 31/05/2008 in binary in the file written as 5a 74 25 00 My problem is how the date would be written as above in binary. Also, how encode or decode.
0
2884
by: Carl Forsman | last post by:
I have some binary data encoded as Base64 encoding in an XML element like this: <data>TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx</data> how can I extract this data - TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx and save to a file say test2.xml
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.