1 /* date.c - handle the highscore list's date entries
2 *
3 * Copyright (C) 2000 Jochen Voss. */
4
5 static const char rcsid[] = "$Id: date.c 4839 2003-04-13 16:50:02Z voss $";
6
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13
14 #include "moon-buggy.h"
15
16
17 time_t
parse_date(const char * str)18 parse_date (const char *str)
19 /* Read the date from STR and convert it to a `time_t'.
20 * This is used to decode highscore entries. */
21 {
22 struct tm broken;
23
24 sscanf (str, "%d-%d-%d %d:%d:%d",
25 &broken.tm_year, &broken.tm_mon, &broken.tm_mday,
26 &broken.tm_hour, &broken.tm_min, &broken.tm_sec);
27 broken.tm_year -= 1900;
28 broken.tm_mon -= 1;
29 broken.tm_isdst = -1;
30
31 return mktime (&broken);
32 }
33
34 time_t
convert_old_date(int day,int month,int year)35 convert_old_date (int day, int month, int year)
36 {
37 struct tm broken;
38
39 broken.tm_year = year-1900;
40 broken.tm_mon = month-1;
41 broken.tm_mday = day;
42 broken.tm_hour = 12;
43 broken.tm_min = 0;
44 broken.tm_sec = 0;
45
46 return mktime (&broken);
47 }
48
49 void
format_date(char * buffer,time_t date)50 format_date (char *buffer, time_t date)
51 /* Into the BUFFER format a textual representation of DATE.
52 * Buffer must contain at least MAX_DATE_CHARS characters.
53 * The filled-in string is feasible for parsing with `parse_date'. */
54 {
55 struct tm *loctime;
56
57 loctime = localtime (&date);
58 sprintf (buffer, "%d-%d-%d %d:%d:%d",
59 loctime->tm_year+1900, loctime->tm_mon+1, loctime->tm_mday,
60 loctime->tm_hour, loctime->tm_min, loctime->tm_sec);
61 }
62
63 void
format_display_date(char * buffer,time_t date)64 format_display_date (char *buffer, time_t date)
65 /* Into the BUFFER format a textual representation of DATE.
66 * Buffer must contain at least 11 characters. The filled-in string
67 * is meant to be part of the displayed highscore list. */
68 {
69 struct tm *loctime;
70
71 loctime = localtime (&date);
72 sprintf (buffer, "%4d-%02d-%02d",
73 loctime->tm_year+1900, loctime->tm_mon+1, loctime->tm_mday);
74 }
75
76 void
format_relative_time(char * buffer,double dt)77 format_relative_time (char *buffer, double dt)
78 /* Into the BUFFER format a textual representation of time period DT.
79 * Buffer must contain at least 5 characters. The filled-in string
80 * is meant to be part of the displayed highscore list. */
81 {
82 double hour = 60*60;
83 double day = 24*hour;
84
85 if (dt <= 0) {
86 sprintf (buffer, "soon");
87 } else if (dt > 999*day) {
88 sprintf (buffer, " -- ");
89 } else if (dt >= day) {
90 sprintf (buffer, "%3dd", (int)(dt/day+0.5));
91 } else if (dt >= hour) {
92 sprintf (buffer, "%3dh", (int)(dt/hour+0.5));
93 } else {
94 sprintf (buffer, "%3dm", (int)(dt/60+0.5));
95 }
96 }
97