473,385 Members | 1,326 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,385 software developers and data experts.

Getting a string date from a number of seconds from01/01/1904

Hi All,

I'm trying to parse a file which has some timestamps embedded. These're
expressed as a number of seconds elapsed since 01/01/1904 (macintosh
time).

I know OS X provides some APIs to handle these but fact is I'm not
coding on a mac. Standard C functions that I've been looking at ( for
example char *ctime(const time_t *time); ) take a time_t based on
01/01/1970.

What would be the best aproach for this problem?

As a side question, how could I represent a date previous to 01/01/1970
in standard C?

TIA,
David

Nov 14 '05 #1
5 5490
"David Requena" <dr******@gmail.com> writes:
I'm trying to parse a file which has some timestamps embedded. These're
expressed as a number of seconds elapsed since 01/01/1904 (macintosh
time).

I know OS X provides some APIs to handle these but fact is I'm not
coding on a mac. Standard C functions that I've been looking at ( for
example char *ctime(const time_t *time); ) take a time_t based on
01/01/1970.

What would be the best aproach for this problem?
The C standard says only that time_t is an arithmetic type capable of
representing times. It could be integer or floating-point, its
resolution isn't necessarily 1 second, and the numeric value needn't
even be linearly related to time. The number of seconds since
1970-01-01 is probably the most common representation, but it's not
guaranteed by the C standard (though it is by the POSIX standard).

If you have a timestamp representing the number of seconds since
1904-01-01, *and* you're willing to assume that time_t is an integer
representing seconds since 1970-01-01, you can probably just subtract
2082844800 from your 1904-based timestamp to get a 1970-based
timestamp.
As a side question, how could I represent a date previous to 01/01/1970
in standard C?


See the caveats above, but if time_t is a signed type you can just use
a negative value (other than -1, which is reserved as an error
indicator).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2

In article <11**********************@z14g2000cwz.googlegroups .com>, "David Requena" <dr******@gmail.com> writes:

I'm trying to parse a file which has some timestamps embedded. These're
expressed as a number of seconds elapsed since 01/01/1904 (macintosh
time).

I know OS X provides some APIs to handle these but fact is I'm not
coding on a mac. Standard C functions that I've been looking at ( for
example char *ctime(const time_t *time); ) take a time_t based on
01/01/1970.
Some of them do, yes. Others work with a (hint) struct tm.
What would be the best aproach for this problem?
I'd recommend creating a struct tm with your timestamp, then using
mktime to normalize it. That ought to work for any time in the
interval where time_t is defined in your implementation, if it
supports calendar time. (The standard doesn't require that it do so,
but I don't know of an implementation that doesn't.)

This method is somewhat complicated by the fact that struct tm's
tm_sec is an int, and so the Mac timestamp may overflow it; so we
can't just slap the Mac time in as tm_sec and set the rest of the
fields to 1904-01-01. I'll use Keith's suggestion of subtracting
2082844800 to create a time from 1970-01-01. On typical 32-bit
systems that still leaves us with a Y2038 problem; fixing that is
left as an exercise for someone else.

Here's some UNTESTED sample code. It assumes you and your timestamps
are in GMT (and that they're the time since 00:00 on 1904-01-01); if
not, that should be easy to correct for by setting tm_hour to the
offset from GMT.

{
/* Seconds between 00:00 1904-01-01 and 00:00 1970-01-01 */
static const unsigned long SecsUntil1970 = 2082844800;

struct tm MacTime = {0};
unsigned long MacTimestamp;
time_t CTime;

MacTimestamp = ...; /* get timestamp: secs since 00:00 1904-01-01 GMT */
if (MacTimestamp < SecsUntil1970)
/* handle separately... */;

/* Create unnormalized struct tm representing Macintosh timestamp */
MacTime.tm_sec = MacTimestamp - SecsUntil1970;
MacTime.tm_hour = 0; /* adjust this for your timezone */
MacTime.tm_year = 70;

/* Convert to time_t */
CTime = mktime(&MacTime);

/* Now convert to string with ctime or (better) strftime ... */
}
As a side question, how could I represent a date previous to 01/01/1970
in standard C?


With a struct tm, as above. Only the implementation can create
time_t values in a portable way, so in fully portable code you can't
manually create a time_t for any date. But the contents of struct tm
are standard and you can create those for arbitrary dates, within the
range the member types support.

(I *think* all of that is right, and I checked the relevant sections
of the standard. No doubt someone here will point out any errors.)

--
Michael Wojcik mi************@microfocus.com

This is a "rubbering action game," a 2D platformer where you control a
girl equipped with an elastic rope with a fishing hook at the end.
-- review of _Umihara Kawase Shun_ for the Sony Playstation
Nov 14 '05 #3
In article <11**********************@z14g2000cwz.googlegroups .com>,
"David Requena" <dr******@gmail.com> wrote:
Hi All,

I'm trying to parse a file which has some timestamps embedded. These're
expressed as a number of seconds elapsed since 01/01/1904 (macintosh
time).

I know OS X provides some APIs to handle these but fact is I'm not
coding on a mac. Standard C functions that I've been looking at ( for
example char *ctime(const time_t *time); ) take a time_t based on
01/01/1970.

What would be the best aproach for this problem?

As a side question, how could I represent a date previous to 01/01/1970
in standard C?


Easy. Express it as the number of seconds from any fixed date that you
choose, and store it as a value of type double, or type long long. You
can of course choose the data 01/01/1970 as your starting date. In many
implementations, the type time_t won't do it. And storing values of type
time_t in a file is asking for trouble, because different
implementations will have different values.
Nov 14 '05 #4
Thanks for your help, Michael.

I'm just curious as to how did you calculate SecsUntil1970 =
2082844800. I had been thinking about a similar solution but didn't
come up with a proper way to take leap years and such into account.

TIA,
David

Nov 14 '05 #5
"David Requena" <dr******@gmail.com> writes:
Thanks for your help, Michael.

I'm just curious as to how did you calculate SecsUntil1970 =
2082844800. I had been thinking about a similar solution but didn't
come up with a proper way to take leap years and such into account.


That was me. I didn't post the method because it's Unix-specific.

% date +%s -d '1904-01-01 00:00:00 UTC'
-2082844800
% date +%s -d '1970-01-01 00:00:00 UTC'
0

This depends on the GNU date command (and older versions of it may not
handle dates before 1970).

More portably, you can compute

(1970-1904)*365*86400 + 17*86400

where 17 is the number of leap days between 1904-01-01 and 1970-01-01
(1904, 1908, ..., 1968).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

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

Similar topics

5
by: dark.ryder | last post by:
I must be doing something wrong, but for the life of me, I can't figure out what. Here's the code snippet which is giving me grief: print type(number), type(name), type(seconds // 60),...
12
by: Mary Catherine | last post by:
I have 2 scipts that I am trying to get to run on the same page. One is a time/date script, the other is a countdown script (i.e. countdown days, hours, mins until a given date). They both work...
16
by: Tim Davidge | last post by:
Hi folks, been a while since I have posted a plea for help and I think I have forgotten everything I learnt from the helpful contributors to this newsgroup, that said however : I'm trying to...
12
by: Rob T | last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be precise so the value is stored to the 1000th of a second. ie "insert into myTable mydate values ('08/05/2005...
7
by: jack | last post by:
Hello, I need to get the time down the the 0.1 second in a number string. Example 09-06-03 13:45 23.4 Seconds To "0906031345234"
1
by: UJ | last post by:
I want to add to my about box a message of when the system was last compiled. Is there any way to get this? UJ.
2
by: jessica6_2000 | last post by:
I have a page where I need to display the unix time (seconds) upon page load. I've found many complex examples that auto update, count live, etc., but I need to just display the time at the time...
16
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: 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...

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.