datetime.h

来自「Shorthand是一个强大的脚本语言」· C头文件 代码 · 共 166 行

H
166
字号
#ifndef __datetime_h
#define __datetime_h
/////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/datetime.h 3     8/28/02 6:27a Arm $
//---------------------------------------------------------------------------
// This file is part of "libAndrix" library - a collection of classes
// and functions developed by Andrei Remenchuk.
//---------------------------------------------------------------------------
// While you may own complete copyright on the project with which you have
// received this file, the author reserves the right to use code contained
// in this very file for any purposes, including publishing and usage in
// any free or commercial software.
//
// You may re-distribute this file or re-use it in your own free or
// commercial software provided that this text is included in the file.
// If you change this file you must include clear notice stating that
// you changed this file and the date of change.
//
// This statement doesn't apply to other files that are part of the same
// package unless otherwise noted.
//---------------------------------------------------------------------------
// (c) 1998-2002 Andrei Remenchuk <andrei@remenchuk.com>
//---------------------------------------------------------------------------
// datetime.h - portable datetime class
/////////////////////////////////////////////////////////////////////////////

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <time.h>

#ifdef WIN32
#include <windows.h>
#else
typedef struct _SYSTEMTIME {
    short wYear;
    short wMonth;
    short wDayOfWeek;
    short wDay;
    short wHour;
    short wMinute;
    short wSecond;
    short wMilliseconds;
} SYSTEMTIME;
#endif


#include "cstring.h"

/**
 * generic datetime class.
 * 
 * On Windows platform datetime is stored as SYSTEMTIME structure which is 
 * supported natively by Windows.
 * On Unix, this structure is emulated.
 */
class datetime  
{
private:
    SYSTEMTIME m_time;

    // normalizes time 
    bool normalize();

public:
	// initializes datetime from current local time
    datetime();

    // initializes datetime from SYSTEMTIME structure
    datetime(const SYSTEMTIME* t);

    // initializes from another datetime object
    datetime(const datetime& t);

    // initializes from Unix time
    datetime(time_t unixtime);

    void import_ydm(int year, int month, int day);
    
    void import_stm(const struct tm* stm);
    void export_stm(struct tm* stm) const;
    
    // initializes datetime from current local time
    void local();

    // returns true if time is not zero
    bool is_valid() const;

    // exports value to "YYYY-MM-DD HH:MM:SS" format
    void export_common(string& s) const;
    
    // imports value from "YYYY-MM-DD HH:MM:SS" format
    bool import(const char* s);

    // exports value into "extended Julian" format which is 
    // array of two integer number {JDN,seconds_since_midnight}
    void export_jdx(unsigned int* jdx) const;

    // imports value from "extended Julian" format which is 
    // array of two integer number {JDN,seconds_since_midnight}
    void import_jdx(const unsigned int* jdx);

    // imports JDN 
    void import_jdn(int jdn);

    // returns JDN of the date
    unsigned int jdn() const;

    void add_seconds(int seconds);

    void add_days(int days);

    // nullifies date object making it invalid
    void invalidate();


    // returns number of seconds since midnight on the specified date
    unsigned int seconds_since_midnight() const;

    // imports value from "e-mail" format which is
    // "Wed, 24 Jul 2002 22:04:50 -0700" and converts to local time
    bool import_rfc(const char* s);

    void export_rfc(string& result);

    // imports value from variety of string formats by trying 
    // to guess fields automatically
    bool import_any(const char* s);

    // imports date/time from any of formats returned by MySQL
    bool import_mysql(const char* s);

    // prints datetime in "common" format: "22 Jul 2002, 6:17 AM"
    void common_print(string& s) const;

    // formats datetime using strftime()-compatible format
    void format(const char* format, string& result) const;

    // converts month number to 3-letter abbreviation 
    static const char* month3(int month);

    const datetime& operator = (const datetime& other);



    unsigned long dwhigh();
    ~datetime();
};


/** returns difference in days between two dates, disregarding time-of-day parts */
int day_difference(const datetime& one, const datetime& two);

/** 
 * returns -1, 0, or +1 depending on whether date one is less, equals, or
 * greater than date two.
 * all components are considered, up to seconds.
 */
int compare_dates(const datetime& one, const datetime& two);




#endif // __datetime_h

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?