File: date_time_utils.cpp

package info (click to toggle)
bibledit-gtk 4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,668 kB
  • ctags: 11,053
  • sloc: xml: 289,607; sql: 160,978; cpp: 86,450; sh: 3,316; makefile: 609; ansic: 398; perl: 143; python: 36
file content (256 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
** Copyright (©) 2003-2012 Teus Benschop.
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**  
*/

#include "libraries.h"
#include "utilities.h"
#include <glib.h>
#include "date_time_utils.h"
#include "tiny_utilities.h"

int date_time_seconds_get_current()
// Get the seconds since the Epoch, modified with the timezone information.
// Special code is used that works not only on Linux but also on BSD.
{
  // Get seconds elapsed since Epoch: 00:00:00 1 January 1970 UTC.
  GTimeVal gtimeval;
  g_get_current_time(&gtimeval);

  // Get both local and UCT time data.  
  time_t t_t = time(0);
  tm *tm = localtime(&t_t);
  int local_year = tm->tm_year;
  int local_day = tm->tm_yday;
  int local_hour = tm->tm_hour;
  int local_minute = tm->tm_min;
  tm = gmtime(&t_t);
  int gm_year = tm->tm_year;
  int gm_day = tm->tm_yday;
  int gm_hour = tm->tm_hour;
  int gm_minute = tm->tm_min;

  // Get the local minutes, since 1900.
  // Handle leap years.
  int local_time = local_year * 365;
  if (g_date_is_leap_year(local_year + 1900))
    local_time++;
  local_time += local_day;
  local_time *= 24;
  local_time += local_hour;
  local_time *= 60;
  local_time += local_minute;

  // Get the minutes according to UCT, which is Greenwich Mean Time.
  // Handle leap years.
  int gm_time = gm_year * 365;
  if (g_date_is_leap_year(gm_year + 1900))
    gm_time++;
  gm_time += gm_day;
  gm_time *= 24;
  gm_time += gm_hour;
  gm_time *= 60;
  gm_time += gm_minute;

  // Get the difference, in seconds;
  int difference = gm_time - local_time;
  difference *= 60;

  // Modify the seconds with this difference.
  gtimeval.tv_sec += (0 - difference);

  // Result. 
  return gtimeval.tv_sec;
}

int date_time_julian_day_get_current()
// Returns the julian day for today.
{
  // New GDate using the current date.
  GDate *date = g_date_new();
  g_date_set_time(date, date_time_seconds_get_current());
  // Get the Julian day out of it.
  int julian_day = g_date_get_julian(date);
  // Free memory.
  g_date_free(date);
  // Return Julian day.
  return julian_day;
}

ustring date_time_julian_human_readable(int julian_day, bool weekday)
// Returns a Julian day in human readable form.
{
  ustring returnvalue;
  GDate *date;
  date = g_date_new_julian(julian_day);
  gchar buf[1024];
  if (weekday)
    g_date_strftime(buf, 1000, "%A, %e %B %Y", date);
  else
    g_date_strftime(buf, 1000, "%e %B %Y", date);
  returnvalue = buf;
  g_date_free(date);
  return returnvalue;
}

ustring date_time_julian_get_month_and_year(int julian_day)
// Returns the month of a Julian day in human readable form.
{
  ustring returnvalue;
  GDate *date;
  date = g_date_new_julian(julian_day);
  gchar buf[1024];
  g_date_strftime(buf, 1000, "%B %Y", date);
  returnvalue = buf;
  g_date_free(date);
  return returnvalue;
}

ustring date_time_seconds_human_readable(int seconds, bool weekday)
// Returns the seconds in human readable format.
{
  // The day, monty and year.
  int julian_day = date_time_seconds_to_julian(seconds);
  ustring returnvalue = date_time_julian_human_readable(julian_day, weekday);
  int hour, minute, second;
  date_time_normal_get_hour_minute_second(seconds, hour, minute, second);
  ustring uhour, uminute, usecond;
  uhour = convert_to_string(hour);
  if (uhour.length() == 1)
    uhour.insert(0, "0");
  uminute = convert_to_string(minute);
  if (uminute.length() == 1)
    uminute.insert(0, "0");
  usecond = convert_to_string(second);
  if (usecond.length() == 1)
    usecond.insert(0, "0");
  returnvalue.append(", " + uhour + ":" + uminute + ":" + usecond);
  return returnvalue;
}

int date_time_julian_day_get_parse(const ustring & date)
// This takes a human readable date, and tries to get the Julian day out of it.
{
  // Trim superfluous characters off.
  ustring cleaned_date = trim(date);
  // If the date has a time part, cut that off.
  size_t offposition = cleaned_date.find(":");
  if (offposition != string::npos) {
    cleaned_date.erase(offposition, cleaned_date.length() - offposition);
    offposition = cleaned_date.find_last_of(" ");
    if (offposition != string::npos) {
      cleaned_date.erase(offposition, cleaned_date.length() - offposition);
    }
  }
  // New GDate object.
  GDate *gdate;
  gdate = g_date_new();
  // Parse the date.
  g_date_set_parse(gdate, cleaned_date.c_str());
  // Julian day to return.
  int julian_day;
  // Validate it. 
  if (g_date_valid(gdate)) {
    julian_day = g_date_get_julian(gdate);
  } else {
    // Not valid, so take today's Julian day.
    julian_day = date_time_julian_day_get_current();
  }
  // Free memory.
  g_date_free(gdate);
  // Return result
  return julian_day;
}

int date_time_julian_day_get_parse(guint year, guint month, guint day)
{
  // New GDate object.
  GDate *gdate;
  gdate = g_date_new();
  // Parse the date.
  g_date_set_day(gdate, day);
  g_date_set_month(gdate, GDateMonth(month));
  g_date_set_year(gdate, year);
  // Julian day to return.
  int julian_day;
  // Validate it. 
  if (g_date_valid(gdate)) {
    julian_day = g_date_get_julian(gdate);
  } else {
    // Not valid, so take today's Julian day.
    julian_day = date_time_julian_day_get_current();
  }
  // Free memory.
  g_date_free(gdate);
  // Return result
  return julian_day;
}

void date_time_normal_get_year_month_day(guint32 julian_day, guint & year, guint & month, guint & day)
{
  // New GDate object.
  GDate *gdate;
  gdate = g_date_new();
  // Set Julian day.
  g_date_set_julian(gdate, julian_day);
  // Return results.
  year = g_date_get_year(gdate);
  month = g_date_get_month(gdate);
  day = g_date_get_day(gdate);
  // Free memory.
  g_date_free(gdate);
}

void date_time_normal_get_hour_minute_second(guint32 seconds, int &hour, int &minute, int &second)
{
  unsigned int seconds_in_day = seconds % 86400;
  hour = seconds_in_day / 3600;
  unsigned int remaining_seconds = seconds_in_day - (3600 * hour);
  minute = remaining_seconds / 60;
  second = remaining_seconds % 60;
}

int date_time_seconds_to_julian(int seconds)
// Converts seconds since Epoch to a Julian day.
{
  GTime time = seconds;
  GDate *date;
  date = g_date_new();
  g_date_set_time(date, time);
  int julian_day = g_date_get_julian(date);
  g_date_free(date);
  return julian_day;
}

int date_time_julian_to_seconds(int day)
// Converts a Julian day to seconds since Epoch.
{
  // Our date, from the Julian days.
  GDate *date;
  date = g_date_new_julian(day);
  // Date of the Epoch.
  GDate *epoch;
  epoch = g_date_new_dmy(1, G_DATE_JANUARY, 1970);
  // Days in between.
  int days = g_date_days_between(epoch, date);
  // Free memory.
  g_date_free(date);
  g_date_free(epoch);
  // Seconds since Epoch.  
  int seconds = days * 86400;
  return seconds;
}