473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

writing DateTime/TimeSpan values to a stream. Is there a shortcut?

Ive been writing DateTime variables to streams the long way
binarywriter.write((int)Day); binarywriter.write((int)Hour) etc
Is there a conversion routine or some other way to shorten the process of
doing this please (eg in Delphi you just cast to a Double value and wrote
that)
I looked at DateTime.IConvertable.ToDouble, but help file said you shouldn't
use this.
I don't want to write it as a string.
thanks
Nov 17 '05 #1
3 3160
Claire,

I assume that this can help you,

\\\
DateTime dt1 = new DateTime();
dt1 = DateTime.Now;
long lDt = (long) dt1.Ticks;
DateTime dt2 = new DateTime();
dt2 = dt2.AddTicks(lDt);
MessageBox.Show(dt2.ToString());
///

Cor
Nov 17 '05 #2
Some unwritten rule says that you should use type converters. There is a
DateTimeConverter and a TimeSpanConverter, unfortunately these converters
can only convert to string and and are very useful when you write
international apps with different cultures.

The reason that you shouldn't use the DateTime.Iconvertable.ToDouble() is
because you will simply get an InvalidCastException.

By the way you don't need to explicitly cast the Days en Hours properties to
an int because the data type is already int32.

Why not format your datetime as follows and write the output?
BinaryWriter.Write(<DateTimeVariable>.ToString("dd mmyyyy"));

It is true that you will save cpu cycles when you call the write method of
the binarywriter passing an int32 data type but behind the scenes it will
all be converted to byte arrays.

Gabriel Lozano-Morán

"Claire" <as*******@ntlworld.com> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
Ive been writing DateTime variables to streams the long way
binarywriter.write((int)Day); binarywriter.write((int)Hour) etc
Is there a conversion routine or some other way to shorten the process of
doing this please (eg in Delphi you just cast to a Double value and wrote
that)
I looked at DateTime.IConvertable.ToDouble, but help file said you
shouldn't use this.
I don't want to write it as a string.
thanks



Nov 17 '05 #3
thanks both of you for your help
Ive gone with Cor's solution as there's no unparsing involved.
The reason I always use casting when calling the Write function is that I've
learned not to trust compilers or my memory ;) If I can visually match a
write((int)x) with a x = readint32() then Im happy.

writer.Write((Int64)LastSampled.Ticks);

Int64 i = reader.ReadInt64();
LastSampled = DateTime.MinValue.AddTicks(i);

Claire
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that...
4
by: John | last post by:
Hi How can I separate a datetime value into date & time values? Conversely how can I combine date & time values into a single datetime value? Thanks Regards
8
by: ajs | last post by:
I have a process that verifies that a server is running. If the server stops responding I send an email to a support person. Now I want to add a downtime indicator so that my code does not send...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.