~ubuntu-branches/ubuntu/trusty/znc/trusty

« back to all changes in this revision

Viewing changes to src/Utils.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-11-08 13:13:58 UTC
  • mfrom: (34.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131108131358-o90mlo1evvu27z8h
Tags: 1.2-1
* New upstream release.
  Closes: #728786
  - Remove merged patch 01-spelling-error.
  - Remove merged patch 02-CVE-2013-2130.
  - License has been changed to Apache-2.0.
  - Disable new tests, because they require an internet connection.
  - Add new znc-extra module modules_online.
  - Remove AUTHORS file.
* Bump Standards-Version to 3.9.5 (no changes needed).
* Don't explicitly request xz compression - dpkg 1.17 does this by default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004-2012  See the AUTHORS file for details.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 2 as published
6
 
 * by the Free Software Foundation.
 
2
 * Copyright (C) 2004-2013 ZNC, see the NOTICE file for details.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
7
15
 */
8
16
 
9
17
#include <znc/Utils.h>
10
 
#include <znc/MD5.h>
11
 
#include <znc/main.h>
12
18
#include <znc/ZNCDebug.h>
13
19
#include <znc/FileUtils.h>
14
 
#include <errno.h>
15
20
#ifdef HAVE_LIBSSL
16
21
#include <openssl/ssl.h>
17
22
#endif /* HAVE_LIBSSL */
18
 
#include <sstream>
19
 
#include <sys/stat.h>
20
 
#include <sys/types.h>
21
23
#include <unistd.h>
22
24
 
23
25
// Required with GCC 4.3+ if openssl is disabled
343
345
        fflush(stdout);
344
346
}
345
347
 
346
 
CString CUtils::CTime(time_t t, const CString& sTZ) {
 
348
namespace {
 
349
        /* Switch GMT-X and GMT+X
 
350
         *
 
351
         * See https://en.wikipedia.org/wiki/Tz_database#Area
 
352
         *
 
353
         * "In order to conform with the POSIX style, those zone names beginning
 
354
         * with "Etc/GMT" have their sign reversed from what most people expect.
 
355
         * In this style, zones west of GMT have a positive sign and those east
 
356
         * have a negative sign in their name (e.g "Etc/GMT-14" is 14 hours
 
357
         * ahead/east of GMT.)"
 
358
         */
 
359
        inline CString FixGMT(CString sTZ) {
 
360
                if (sTZ.length() >= 4 && sTZ.Left(3) == "GMT") {
 
361
                        if (sTZ[3] == '+') {
 
362
                                sTZ[3] = '-';
 
363
                        } else if (sTZ[3] == '-') {
 
364
                                sTZ[3] = '+';
 
365
                        }
 
366
                }
 
367
                return sTZ;
 
368
        }
 
369
}
 
370
 
 
371
CString CUtils::CTime(time_t t, const CString& sTimezone) {
347
372
        char s[30] = {}; // should have at least 26 bytes
348
 
        if (sTZ.empty()) {
 
373
        if (sTimezone.empty()) {
349
374
                ctime_r(&t, s);
350
375
                // ctime() adds a trailing newline
351
376
                return CString(s).Trim_n();
352
377
        }
 
378
        CString sTZ = FixGMT(sTimezone);
353
379
 
354
380
        // backup old value
355
381
        char* oldTZ = getenv("TZ");
371
397
        return CString(s).Trim_n();
372
398
}
373
399
 
374
 
CString CUtils::FormatTime(time_t t, const CString& sFormat, const CString& sTZ) {
 
400
CString CUtils::FormatTime(time_t t, const CString& sFormat, const CString& sTimezone) {
375
401
        char s[1024] = {};
376
402
        tm m;
377
 
        if (sTZ.empty()) {
 
403
        if (sTimezone.empty()) {
378
404
                localtime_r(&t, &m);
379
405
                strftime(s, sizeof(s), sFormat.c_str(), &m);
380
406
                return s;
381
407
        }
 
408
        CString sTZ = FixGMT(sTimezone);
382
409
 
383
410
        // backup old value
384
411
        char* oldTZ = getenv("TZ");