Functions
dostime.c File Reference

Functions to convert times between Unix and MS-DOS times. More...

#include "dostime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for dostime.c:

Go to the source code of this file.

Functions

time_t dos2unixtime (dostime_t dostime)
 Convert a DOS time to a Unix time.
 
dostime_t dostime (int year, int month, int day, int hour, int minute, int second)
 
dostime_t maxdostime ()
 Return the maximum possible value .
 
dostime_t mindostime ()
 Return the minimum possible value.
 
dostime_t unix2dostime (time_t unix_time)
 Convert a Unix date to a DOS date.
 

Detailed Description

These functions make use of the old date and time format defined to implement the FAT file system. This is defined on the Microsoft website here:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247%28v=vs.85%29.aspx

As a quick recap, we have:

Date (actually we view those as bit 16 to 31)

0-4 Day of the month (1-31)
5-8 Month (1 = January, 2 = February, and so on)
9-15 Year offset from 1980 (add 1980 to get actual year)

Time

0-4 Second divided by 2
5-10 Minute (0-59)
11-15 Hour (0-23 on a 24-hour clock)
Note
This implementation uses C code.

Definition in file dostime.c.

Function Documentation

◆ dos2unixtime()

time_t dos2unixtime ( dostime_t  dostime)

This function returns the Unix time_t value (GMT/UTC time) from the DOS format (local) time dostime, where dostime is a four byte value (date in most significant word, time in least significant word), see dostime() function.

If the input DOS time is invalid, then the function returns -1.

Note
If the dostime is not valid (one of the parameters is out of range) then the function returns -1.
Parameters
[in]dostimeA DOS time value as found in a zip file.
Returns
The DOS time converted to a Unix time or -1.
See also
dostime()
Todo:
Maximum for tm_mday depends on month/year.

Definition at line 131 of file dostime.c.

References dostime().

Referenced by main(), zipios::ZipCentralDirectoryEntry::read(), zipios::ZipLocalEntry::read(), and zipios::FileEntry::setTime().

◆ dostime()

dostime_t dostime ( int  year,
int  month,
int  day,
int  hour,
int  minute,
int  second 
)

◆ maxdostime()

dostime_t maxdostime ( )

This function returns the maximum DOS time that can be represented in a dostime_t parameter.

At this time we use a 32 bit field (like the Zip archive) so the maximum is Dec 31, 2107 23:59:59.

Note
To get the corresponding Unix time, use the dos2unixtime() as in:
time_t max(dos2unixtime(maxdostime()));
time_t dos2unixtime(dostime_t dostime)
Convert a DOS time to a Unix time.
Definition: dostime.c:131
dostime_t maxdostime()
Return the maximum possible value .
Definition: dostime.c:105
Returns
The largest possible DOS time.

Definition at line 105 of file dostime.c.

◆ mindostime()

dostime_t mindostime ( )

This function returns the minimum DOS time that can be represented in a dostime_t parameter.

At this time we use a 32 bit field (like the Zip archive) so the maximum is Dec 31, 2107 23:59:59.

Note
To get the corresponding Unix time, use the dos2unixtime() as in:
time_t min(dos2unixtime(mindostime()));
dostime_t mindostime()
Return the minimum possible value.
Definition: dostime.c:81
Returns
The smallest possible DOS time.

Definition at line 81 of file dostime.c.

◆ unix2dostime()

dostime_t unix2dostime ( time_t  unix_time)

This function return the Unix time_t converted in DOS format, rounded up to the next even second.

Parameters
[in]unix_timeA Unix time_t value.
Returns
The Unix date in DOS format unless it is out of range for a DOS time and date in which case zero (0) is returned.

Definition at line 219 of file dostime.c.

References dostime().

Referenced by zipios::FileEntry::getTime(), main(), zipios::ZipCentralDirectoryEntry::write(), and zipios::ZipLocalEntry::write().